@@ -243,6 +243,19 @@ func escapeAngleBracketPlaceholders(line string) string {
243243 result := ""
244244 i := 0
245245 for i < len (line ) {
246+ // Check for HTML comment pattern <!--
247+ if i + 3 < len (line ) && line [i :i + 4 ] == "<!--" {
248+ // Find the closing -->
249+ end := strings .Index (line [i :], "-->" )
250+ if end != - 1 {
251+ // Extract comment content and convert to MDX format
252+ commentContent := line [i + 4 : i + end ] // Extract text between <!-- and -->
253+ result += "{/*" + commentContent + "*/}"
254+ i = i + end + 3 // +3 for "-->"
255+ continue
256+ }
257+ }
258+
246259 // Check for Hugo shortcode start pattern {{<
247260 if i + 2 < len (line ) && line [i :i + 3 ] == "{{<" {
248261 // Find the closing >}}
@@ -266,16 +279,18 @@ func escapeAngleBracketPlaceholders(line string) string {
266279 // Extract the content between < and >
267280 content := line [i + 1 : end ]
268281 // Check if it's likely a placeholder or text that should not be parsed as HTML
269- // Known HTML tags we want to preserve: a, br, Accordion, details, summary, pre, code and their closing tags
282+ // Known HTML tags we want to preserve: a, br, p, Accordion, details, summary, pre, code and their closing tags
270283 isKnownTag := strings .HasPrefix (content , "a " ) ||
271284 strings .HasPrefix (content , "br" ) ||
285+ strings .HasPrefix (content , "p" ) ||
272286 strings .HasPrefix (content , "Accordion" ) ||
273287 strings .HasPrefix (content , "details" ) ||
274288 strings .HasPrefix (content , "summary" ) ||
275289 strings .HasPrefix (content , "pre" ) ||
276290 strings .HasPrefix (content , "code" ) ||
277291 content == "/a" ||
278292 content == "/br" ||
293+ content == "/p" ||
279294 content == "/Accordion" ||
280295 content == "/details" ||
281296 content == "/summary" ||
@@ -457,6 +472,10 @@ func processCellContent(content string) string {
457472 // Escape angle bracket placeholders
458473 content = escapeAngleBracketPlaceholders (content )
459474
475+ // Collapse all whitespace (including newlines) into single spaces for MDX compatibility
476+ // This prevents parsing errors with multi-line content in <td> tags
477+ content = strings .Join (strings .Fields (content ), " " )
478+
460479 return content
461480}
462481
@@ -561,12 +580,71 @@ func convertTableToHTML(lines []string, startIdx int) (string, int) {
561580
562581func main () {
563582 if len (os .Args ) < 3 {
564- fmt .Println ("Usage: convert-docs <source_dir> <dest_dir>" )
583+ fmt .Println ("Usage: convert-docs <source_file_or_dir> <dest_file_or_dir>" )
584+ os .Exit (1 )
585+ }
586+
587+ src := os .Args [1 ]
588+ dst := os .Args [2 ]
589+
590+ // Check if source is a file or directory
591+ srcInfo , err := os .Stat (src )
592+ if err != nil {
593+ fmt .Fprintf (os .Stderr , "Error accessing source: %v\n " , err )
565594 os .Exit (1 )
566595 }
567596
568- srcDir := os .Args [1 ]
569- dstDir := os .Args [2 ]
597+ // Single file conversion
598+ if ! srcInfo .IsDir () {
599+ if ! strings .HasSuffix (src , ".md" ) {
600+ fmt .Fprintf (os .Stderr , "Error: source file must be a .md file\n " )
601+ os .Exit (1 )
602+ }
603+
604+ // Skip _index.md files
605+ if strings .Contains (src , "_index.md" ) {
606+ fmt .Printf ("Skipping _index.md file\n " )
607+ os .Exit (0 )
608+ }
609+
610+ // Determine the destination file path
611+ dstPath := dst
612+
613+ // Check if destination is a directory or should be treated as one
614+ dstInfo , err := os .Stat (dst )
615+ if err == nil && dstInfo .IsDir () {
616+ // Destination exists and is a directory - derive filename from source
617+ srcBase := filepath .Base (src )
618+ dstFilename := strings .TrimSuffix (srcBase , ".md" ) + ".mdx"
619+ dstPath = filepath .Join (dst , dstFilename )
620+ } else if err != nil && (strings .HasSuffix (dst , "/" ) || strings .HasSuffix (dst , "\\ " )) {
621+ // Destination doesn't exist but ends with slash - treat as directory
622+ srcBase := filepath .Base (src )
623+ dstFilename := strings .TrimSuffix (srcBase , ".md" ) + ".mdx"
624+ dstPath = filepath .Join (dst , dstFilename )
625+ }
626+
627+ fmt .Printf ("Converting single file: %s -> %s\n " , src , dstPath )
628+
629+ // Create destination directory if it doesn't exist
630+ if err := os .MkdirAll (filepath .Dir (dstPath ), 0755 ); err != nil {
631+ fmt .Fprintf (os .Stderr , "Error creating destination directory: %v\n " , err )
632+ os .Exit (1 )
633+ }
634+
635+ // Convert the file
636+ if err := convertFile (src , dstPath ); err != nil {
637+ fmt .Fprintf (os .Stderr , "Error converting file: %v\n " , err )
638+ os .Exit (1 )
639+ }
640+
641+ fmt .Println ("Conversion complete!" )
642+ return
643+ }
644+
645+ // Directory conversion (existing behavior)
646+ srcDir := src
647+ dstDir := dst
570648
571649 fmt .Printf ("Converting docs from %s to %s\n " , srcDir , dstDir )
572650
@@ -575,7 +653,7 @@ func main() {
575653 os .MkdirAll (dstDir , 0755 )
576654
577655 // Walk through source directory
578- err : = filepath .Walk (srcDir , func (path string , info os.FileInfo , err error ) error {
656+ err = filepath .Walk (srcDir , func (path string , info os.FileInfo , err error ) error {
579657 if err != nil {
580658 return err
581659 }
0 commit comments