package classes import ( "bytes" "strings" xhtml "golang.org/x/net/html" ) // ConvertH1 turns every h1 element of html into an h2 and returns the // result with the number of headings it converted. It works on the // x/net/html tokenizer's raw tokens: only the tag name of an h1 start, end // or self-closing tag changes, and every other byte (attributes, text, // comments, the content of script or style) is copied as it is. Text that // merely looks like a tag inside raw-text elements or comments is not a // tag and is not touched. html without an h1 comes back unchanged with 0, // and the bytes of an unfinished tag at the end of html are kept. func ConvertH1(html string) (string, int) { if !containsFold(html, "= 3 { b.WriteString("= 4 { b.WriteString(" keeps it, and only its rendered content_html is converted // (every render path derives content_html from the markdown again and // converts it again). func ConvertContentH1(content, contentHTML string) (string, string, int) { contentHTML, nHTML := ConvertH1(contentHTML) if !LooksLikeHTML(content) { return content, contentHTML, nHTML } content, nContent := ConvertH1(content) if nHTML > 0 { return content, contentHTML, nHTML } return content, contentHTML, nContent } // ConvertTranslationH1 applies ConvertContentH1 to the string content and // content_html of one locale's translated attributes, in place, and returns // the number of headings converted. A markdown content is never rewritten // (ConvertContentH1). Other attributes and values that are not strings are // left alone. func ConvertTranslationH1(attrs map[string]any) int { if attrs == nil { return 0 } content, hasContent := attrs["content"].(string) html, hasHTML := attrs["content_html"].(string) if !hasContent && !hasHTML { return 0 } content, html, n := ConvertContentH1(content, html) if hasContent { attrs["content"] = content } if hasHTML { attrs["content_html"] = html } return n }