package grammaratur import ( "bytes" "encoding/json" "sort" "strings" "github.com/google/uuid" "golang.org/x/net/html" ) // HTMLToMarkdownAuthoring keeps source HTML when conversion loses structure, // attributes, comments or text. This is not a sanitization boundary. func HTMLToMarkdownAuthoring(source string) (string, error) { converted, err := HTMLToMarkdown(source) if err != nil { return "", err } if htmlStructure(source) != htmlStructure(MarkdownToHTML(converted)) { return rawHTMLBlock(source), nil } return converted, nil } // {=html} is an explicit authoring fence, not an ordinary code sample. The // closing delimiter is longer than any run in the source, so arbitrary HTML // (including Markdown examples) cannot accidentally terminate it. func rawHTMLBlock(source string) string { longest, run := 2, 0 for _, c := range source { if c == '`' { run++ if run > longest { longest = run } } else { run = 0 } } fence := strings.Repeat("`", longest+1) return fence + "{=html}\n" + source + "\n" + fence } // MarkdownAuthoringHTML preserves protected HTML byte-for-byte while rendering // ordinary Markdown. Callers must still apply projection sanitization for UI. func MarkdownAuthoringHTML(source string) string { lines := strings.SplitAfter(source, "\n") var markdown strings.Builder type rawBlock struct{ token, html string } var blocks []rawBlock var ordinary byte ordinaryLen := 0 for i := 0; i < len(lines); i++ { line := strings.TrimSuffix(strings.TrimSuffix(lines[i], "\n"), "\r") char, n := byte(0), 0 if len(line) > 0 && (line[0] == '`' || line[0] == '~') { char = line[0] for n < len(line) && line[n] == char { n++ } } if ordinary != 0 { markdown.WriteString(lines[i]) if char == ordinary && n >= ordinaryLen && strings.TrimSpace(line[n:]) == "" { ordinary = 0 } continue } if char == '`' && n >= 3 && strings.TrimSpace(line[n:]) == "{=html}" { end := i + 1 for ; end < len(lines); end++ { close := strings.TrimSpace(lines[end]) if len(close) >= n && strings.Trim(close, "`") == "" { break } } if end < len(lines) { raw := strings.TrimSuffix(strings.Join(lines[i+1:end], ""), "\n") token := "PAGESWIKIRAW" + strings.ReplaceAll(uuid.NewString(), "-", "") blocks = append(blocks, rawBlock{token, raw}) markdown.WriteString("\n\n" + token + "\n\n") i = end continue } } if n >= 3 { ordinary, ordinaryLen = char, n } markdown.WriteString(lines[i]) } if len(blocks) == 1 && strings.TrimSpace(markdown.String()) == blocks[0].token { return blocks[0].html } out := MarkdownToHTML(markdown.String()) for _, block := range blocks { out = strings.Replace(out, "

"+block.token+"

", block.html, 1) } return out } func htmlStructure(source string) string { n, err := html.Parse(strings.NewReader(source)) if err != nil { return source } var out bytes.Buffer var visit func(*html.Node, bool) visit = func(n *html.Node, literal bool) { literal = literal || n.Data == "pre" || n.Data == "code" || n.Data == "script" || n.Data == "style" data := n.Data if n.Type == html.TextNode && !literal { if strings.TrimSpace(data) == "" && (n.Parent == nil || n.Parent.Data == "body" || n.Parent.Data == "html") { return } // Inline whitespace can separate words (including around an inline // element). Never normalize it away when deciding conversion safety. } attrs := append([]html.Attribute(nil), n.Attr...) sort.Slice(attrs, func(i, j int) bool { return attrs[i].Namespace+":"+attrs[i].Key < attrs[j].Namespace+":"+attrs[j].Key }) b, _ := json.Marshal([]any{n.Type, n.Namespace, data, attrs}) out.Write(b) for c := n.FirstChild; c != nil; c = c.NextSibling { visit(c, literal) } out.WriteByte(')') } visit(n, false) return out.String() }