Skip to content

Commit 258d2d6

Browse files
lennessyyclaude
andcommitted
Align JSON escaping regex and add cross-reference comments
Align reJSONInSingleQuotes with encodeJSONExample to use [^']* inside braces instead of [^}]*, so nested JSON like '{"a": {"b": "c"}}' is handled correctly. Add cross-reference comments between encodeJSONExample and escapeMDXDescription so maintainers understand why there are two escaping functions for the same class of issues. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 37512d9 commit 258d2d6

1 file changed

Lines changed: 8 additions & 5 deletions

File tree

internal/commandsgen/docs.go

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -254,10 +254,11 @@ func (w *docWriter) isLeafCommand(c *Command) bool {
254254
return true
255255
}
256256

257+
// encodeJSONExample wraps single-quoted JSON examples in backticks so MDX
258+
// does not parse curly braces as JSX expressions. This handles option
259+
// description table cells. See also escapeMDXDescription which handles
260+
// the same class of issues for command description body text.
257261
func encodeJSONExample(v string) string {
258-
// example: 'YourKey={"your": "value"}'
259-
// results in an mdx acorn rendering error
260-
// and wrapping in backticks lets it render
261262
re := regexp.MustCompile(`('[^']*\{[^']*\}[^']*')`)
262263
v = re.ReplaceAllString(v, "`$1`")
263264
return v
@@ -266,13 +267,15 @@ func encodeJSONExample(v string) string {
266267
var (
267268
reAngleBracketPlaceholder = regexp.MustCompile(`<([a-z][a-z0-9_:-]+)>`)
268269
reHeadingID = regexp.MustCompile(`^(#{1,6}\s+.+?)\s+\{#([\w-]+)\}\s*$`)
269-
reJSONInSingleQuotes = regexp.MustCompile(`'([^']*\{[^}]*\}[^']*)'`)
270+
reJSONInSingleQuotes = regexp.MustCompile(`'([^']*\{[^']*\}[^']*)'`)
270271
)
271272

272273
// escapeMDXDescription escapes patterns in command descriptions that are
273274
// valid Markdown but break MDX compilation: heading IDs ({#id}), bare
274275
// angle-bracket placeholders (<name>), and curly braces in JSON examples.
275-
// Code fences are left untouched.
276+
// Code fences are left untouched. Inline backtick spans are also preserved,
277+
// assuming balanced backticks. See also encodeJSONExample which handles
278+
// the same class of issues for option description table cells.
276279
func escapeMDXDescription(desc string) string {
277280
lines := strings.Split(desc, "\n")
278281
var result []string

0 commit comments

Comments
 (0)