Skip to content

Commit 4d43972

Browse files
authored
Merge pull request #3541 from typeid/fix_docsquotes_displayed_as_escaped_html
📖 fix escaped html in markdown code segments
2 parents dde2f21 + 1d9d477 commit 4d43972

File tree

1 file changed

+22
-3
lines changed

1 file changed

+22
-3
lines changed

docs/book/utils/markerdocs/html.go

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,10 +39,29 @@ type toHTML interface {
3939
// Text is a chunk of text in an HTML doc.
4040
type Text string
4141

42-
// WriteHTML writes the string as HTML to the given Writer
42+
// WriteHTML writes the string as HTML to the given Writer while accounting for mdBook's handling
43+
// of backticks. Given mdBook's behavior of treating backticked content as raw text, this function
44+
// ensures proper rendering by preventing unnecessary HTML escaping within code snippets. Chunks
45+
// outside of backticks are HTML-escaped for security, while chunks inside backticks remain as raw
46+
// text, preserving mdBook's intended rendering of code content.
4347
func (t Text) WriteHTML(w io.Writer) error {
44-
_, err := io.WriteString(w, html.EscapeString(string(t)))
45-
return err
48+
textChunks := strings.Split(string(t), "`")
49+
50+
for i, chunk := range textChunks {
51+
if i%2 == 0 { // Outside backticks, escape and write HTML
52+
_, err := io.WriteString(w, html.EscapeString(chunk))
53+
if err != nil {
54+
return err
55+
}
56+
} else { // Inside backticks, write raw HTML
57+
_, err := io.WriteString(w, "`"+chunk+"`")
58+
if err != nil {
59+
return err
60+
}
61+
}
62+
}
63+
64+
return nil
4665
}
4766

4867
// Tag is some tag with contents and attributes in an HTML doc.

0 commit comments

Comments
 (0)