File tree Expand file tree Collapse file tree 1 file changed +22
-3
lines changed
docs/book/utils/markerdocs Expand file tree Collapse file tree 1 file changed +22
-3
lines changed Original file line number Diff line number Diff line change @@ -39,10 +39,29 @@ type toHTML interface {
39
39
// Text is a chunk of text in an HTML doc.
40
40
type Text string
41
41
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.
43
47
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
46
65
}
47
66
48
67
// Tag is some tag with contents and attributes in an HTML doc.
You can’t perform that action at this time.
0 commit comments