Skip to content

Commit

Permalink
Merge pull request #3541 from typeid/fix_docsquotes_displayed_as_esca…
Browse files Browse the repository at this point in the history
…ped_html

📖  fix escaped html in markdown code segments
  • Loading branch information
k8s-ci-robot committed Aug 18, 2023
2 parents dde2f21 + 1d9d477 commit 4d43972
Showing 1 changed file with 22 additions and 3 deletions.
25 changes: 22 additions & 3 deletions docs/book/utils/markerdocs/html.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,29 @@ type toHTML interface {
// Text is a chunk of text in an HTML doc.
type Text string

// WriteHTML writes the string as HTML to the given Writer
// WriteHTML writes the string as HTML to the given Writer while accounting for mdBook's handling
// of backticks. Given mdBook's behavior of treating backticked content as raw text, this function
// ensures proper rendering by preventing unnecessary HTML escaping within code snippets. Chunks
// outside of backticks are HTML-escaped for security, while chunks inside backticks remain as raw
// text, preserving mdBook's intended rendering of code content.
func (t Text) WriteHTML(w io.Writer) error {
_, err := io.WriteString(w, html.EscapeString(string(t)))
return err
textChunks := strings.Split(string(t), "`")

for i, chunk := range textChunks {
if i%2 == 0 { // Outside backticks, escape and write HTML
_, err := io.WriteString(w, html.EscapeString(chunk))
if err != nil {
return err
}
} else { // Inside backticks, write raw HTML
_, err := io.WriteString(w, "`"+chunk+"`")
if err != nil {
return err
}
}
}

return nil
}

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

0 comments on commit 4d43972

Please sign in to comment.