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(" inside a code block or code span keeps its tag // whatever the body's first character, so a markdown post that opens with // an HTML block keeps its code samples (33-REVIEW-GAPFIX WR-01), and a raw //

outside code becomes

, as in the rendered HTML. func ConvertContentH1(content, contentHTML string) (string, string, int) { contentHTML, nHTML := ConvertH1(contentHTML) content, nContent := convertH1OutsideCode(content) if nHTML > 0 { return content, contentHTML, nHTML } return content, contentHTML, nContent } // convertH1OutsideCode applies ConvertH1 to the parts of markdown md // outside its fenced and indented code blocks and code spans (goldmark's // AST, the engine of FormatHTML): an h1 shown in a code sample keeps its // tag. HTML blocks are not code, so an HTML body is converted whole. md // without an h1 tag comes back unchanged with 0 without being parsed. func convertH1OutsideCode(md string) (string, int) { if !containsFold(md, "