package grammaratur import ( "bytes" "encoding/json" "io" "regexp" "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. var nestedListGap = regexp.MustCompile(`\n[\t ]*\n([\t ]+(?:[-+*]|[0-9]+\.) )`) func authoringMarkdown(source string) (string, error) { md, err := HTMLToMarkdown(source) if err != nil { return md, err } // The converter inserts a loose-list gap before nested lists. Only accept // tightening when the resulting DOM matches; code and real paragraphs stay protected. tight := nestedListGap.ReplaceAllString(md, "\n$1") if tight != md && htmlStructure(source) == htmlStructure(MarkdownToHTML(tight)) { return tight, nil } return md, nil } func HTMLToMarkdownAuthoring(source string) (string, error) { converted, err := authoringMarkdown(source) if err != nil { return "", err } if htmlStructure(source) != htmlStructure(MarkdownToHTML(converted)) { // Protect only the blocks which cannot round-trip. One custom component // must not turn the headings, lists and paragraphs around it into HTML. blocks, ok := authoringBlocks(source) if !ok { return rawHTMLBlock(source), nil } var parts []string for _, raw := range blocks { if strings.TrimSpace(raw) == "" { continue } md, e := authoringMarkdown(raw) if e != nil || htmlStructure(raw) != htmlStructure(MarkdownToHTML(md)) { md = rawHTMLBlock(raw) } parts = append(parts, md) } mixed := strings.Join(parts, "\n\n") // Includes head content, comments outside body and fragment edge cases. if htmlStructure(source) != htmlStructure(MarkdownAuthoringHTML(mixed)) { return rawHTMLBlock(source), nil } return mixed, nil } return converted, nil } // Token boundaries retain the exact source of protected components (including // attribute quoting and void-element syntax). Never serialize the parsed DOM // back over their source. Malformed/implicit nesting uses whole-document fallback. func authoringBlocks(source string) ([]string, bool) { z := html.NewTokenizer(strings.NewReader(source)) var stack, blocks []string start, offset := 0, 0 for { kind := z.Next() if kind == html.ErrorToken { return blocks, z.Err() == io.EOF && len(stack) == 0 && offset == len(source) } offset += len(z.Raw()) switch kind { case html.StartTagToken: name, _ := z.TagName() switch string(name) { case "area", "base", "br", "col", "embed", "hr", "img", "input", "link", "meta", "param", "source", "track", "wbr": default: stack = append(stack, string(name)) } case html.EndTagToken: name, _ := z.TagName() if len(stack) == 0 || stack[len(stack)-1] != string(name) { return nil, false } stack = stack[:len(stack)-1] } if len(stack) == 0 { blocks = append(blocks, source[start:offset]) start = offset if len(blocks) > 1024 { return nil, false } } } } // {=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 { // Formatting whitespace beside a nested block is not an inline word // separator. Keep all whitespace between inline siblings significant. if n.Parent != nil && n.Parent.Data == "li" { isList := func(x *html.Node) bool { return x != nil && x.Type == html.ElementNode && (x.Data == "ul" || x.Data == "ol") } if isList(n.NextSibling) { data = strings.TrimRight(data, " \t\r\n") } if isList(n.PrevSibling) { data = strings.TrimLeft(data, " \t\r\n") } if data == "" { return } } if strings.TrimSpace(data) == "" && n.Parent != nil { switch n.Parent.Data { case "ul", "ol", "table", "thead", "tbody", "tfoot", "tr": return } } 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() }