format/htmlparser: fix generating markdown for code blocks with backticks
Some checks are pending
Go / Lint (latest) (push) Waiting to run
Go / Build (old, libolm) (push) Waiting to run
Go / Build (latest, libolm) (push) Waiting to run
Go / Build (old, goolm) (push) Waiting to run
Go / Build (latest, goolm) (push) Waiting to run

This commit is contained in:
Tulir Asokan 2026-03-13 18:33:22 +02:00
commit ef6de851a2

View file

@ -93,6 +93,30 @@ func DefaultPillConverter(displayname, mxid, eventID string, ctx Context) string
}
}
func onlyBacktickCount(line string) (count int) {
for i := 0; i < len(line); i++ {
if line[i] != '`' {
return -1
}
count++
}
return
}
func DefaultMonospaceBlockConverter(code, language string, ctx Context) string {
if len(code) == 0 || code[len(code)-1] != '\n' {
code += "\n"
}
fence := "```"
for line := range strings.SplitSeq(code, "\n") {
count := onlyBacktickCount(strings.TrimSpace(line))
if count >= len(fence) {
fence = strings.Repeat("`", count+1)
}
}
return fmt.Sprintf("%s%s\n%s%s", fence, language, code, fence)
}
// HTMLParser is a somewhat customizable Matrix HTML parser.
type HTMLParser struct {
PillConverter PillConverter
@ -348,10 +372,7 @@ func (parser *HTMLParser) tagToString(node *html.Node, ctx Context) string {
if parser.MonospaceBlockConverter != nil {
return parser.MonospaceBlockConverter(preStr, language, ctx)
}
if len(preStr) == 0 || preStr[len(preStr)-1] != '\n' {
preStr += "\n"
}
return fmt.Sprintf("```%s\n%s```", language, preStr)
return DefaultMonospaceBlockConverter(preStr, language, ctx)
default:
return parser.nodeToTagAwareString(node.FirstChild, ctx)
}