mirror of
https://mau.dev/mautrix/go.git
synced 2026-03-14 14:25:53 +01:00
format/htmlparser: fix generating markdown for code blocks with backticks
This commit is contained in:
parent
b42ac0e83d
commit
ef6de851a2
1 changed files with 25 additions and 4 deletions
|
|
@ -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)
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue