Skip to content

Commit 6fad789

Browse files
rothgarIheanacho-ai
authored andcommitted
fix: formatting for angle brackets in code blocks
fixes #260 Signed-off-by: Justin Garrison <justin.garrison@siderolabs.com>
1 parent 13ce600 commit 6fad789

6 files changed

Lines changed: 1244 additions & 4340 deletions

File tree

Makefile

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ CONTAINER_NAME := docs-preview
66
PORT := 3000
77
DOCS_GEN_IMAGE := ghcr.io/siderolabs/docs-gen:latest
88
DOCS_CONVERT_IMAGE := ghcr.io/siderolabs/docs-convert:latest
9-
TALOSCTL_IMAGE := ghcr.io/siderolabs/talosctl:v1.11.3
10-
TALOS_VERSION := v1.11
9+
TALOSCTL_IMAGE := ghcr.io/siderolabs/talosctl:v1.12.1
10+
TALOS_VERSION := v1.12
1111
VALE_IMAGE := jdkato/vale:latest
1212
VALE_CONFIG ?= .vale.ini
1313

docs-convert/main.go

Lines changed: 42 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,15 @@ func convertFile(srcPath, dstPath string) error {
4646
// Process lines
4747
i := 0
4848
inFrontmatter := false
49+
inCodeBlock := false
4950
for i < len(lines) {
5051
line := lines[i]
5152

53+
// Track code block boundaries (triple backticks)
54+
if strings.HasPrefix(strings.TrimSpace(line), "```") {
55+
inCodeBlock = !inCodeBlock
56+
}
57+
5258
// Track frontmatter boundaries
5359
if line == "---" {
5460
if !inFrontmatter {
@@ -176,8 +182,10 @@ func convertFile(srcPath, dstPath string) error {
176182

177183
// Escape placeholder-like angle brackets (e.g., <src-path>, <dest-path>)
178184
// but preserve actual HTML tags (a, br, Accordion)
179-
// Simple heuristic: if it contains a hyphen or underscore, it's likely a placeholder
180-
line = escapeAngleBracketPlaceholders(line)
185+
// Skip escaping inside code blocks (triple backticks) where angle brackets are literal
186+
if !inCodeBlock {
187+
line = escapeAngleBracketPlaceholders(line)
188+
}
181189

182190
fmt.Fprintln(writer, line)
183191
i++
@@ -241,7 +249,17 @@ func fixAnchorLinks(line string) string {
241249
func escapeAngleBracketPlaceholders(line string) string {
242250
result := ""
243251
i := 0
252+
inBackticks := false
253+
244254
for i < len(line) {
255+
// Track backtick state
256+
if line[i] == '`' {
257+
inBackticks = !inBackticks
258+
result += string(line[i])
259+
i++
260+
continue
261+
}
262+
245263
// Check for HTML comment pattern <!--
246264
if i+3 < len(line) && line[i:i+4] == "<!--" {
247265
// Find the closing -->
@@ -315,9 +333,15 @@ func escapeAngleBracketPlaceholders(line string) string {
315333
content == "/thead" ||
316334
content == "/tbody"
317335

318-
// If it's not a known HTML tag, escape using JSX expressions
336+
// If it's not a known HTML tag, escape it
319337
if !isKnownTag {
320-
result += `{"<"}` + content + `{">"}`
338+
// Inside backticks, leave as-is (no escaping needed in code context)
339+
// Outside backticks, use JSX expressions
340+
if inBackticks {
341+
result += line[i : end+1]
342+
} else {
343+
result += `{"<"}` + content + `{">"}`
344+
}
321345
i = end + 1
322346
continue
323347
} else {
@@ -854,16 +878,26 @@ func main() {
854878
return nil
855879
}
856880

857-
// Convert .md to .mdx
858-
dstPath := filepath.Join(dstDir, strings.TrimSuffix(relPath, ".md")+".mdx")
881+
// Special handling for cli.md at root - move it to parent directory
882+
var dstPath string
883+
if relPath == "cli.md" {
884+
// Output to parent directory of dstDir
885+
// Clean the path first to remove trailing slashes
886+
cleanDstDir := filepath.Clean(dstDir)
887+
parentDir := filepath.Dir(cleanDstDir)
888+
dstPath = filepath.Join(parentDir, "cli.mdx")
889+
fmt.Printf("Converting %s -> cli.mdx (moved to parent directory: %s)\n", relPath, dstPath)
890+
} else {
891+
// Convert .md to .mdx in normal location
892+
dstPath = filepath.Join(dstDir, strings.TrimSuffix(relPath, ".md")+".mdx")
893+
fmt.Printf("Converting %s -> %s\n", relPath, strings.TrimSuffix(relPath, ".md")+".mdx")
894+
}
859895

860896
// Create destination directory
861897
if err := os.MkdirAll(filepath.Dir(dstPath), 0755); err != nil {
862898
return err
863899
}
864900

865-
fmt.Printf("Converting %s -> %s\n", relPath, strings.TrimSuffix(relPath, ".md")+".mdx")
866-
867901
return convertFile(path, dstPath)
868902
})
869903

0 commit comments

Comments
 (0)