Skip to content

Commit

Permalink
🎨 Improve HTML code block clipping for https://ld246.com #12678
Browse files Browse the repository at this point in the history
  • Loading branch information
88250 committed Oct 2, 2024
1 parent 655b6bc commit 169e6a5
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions kernel/api/extension.go
Original file line number Diff line number Diff line change
Expand Up @@ -143,14 +143,33 @@ func extensionCopy(c *gin.Context) {
}

md = string(bodyData)
luteEngine.SetIndentCodeBlock(true) // 链滴支持缩进代码块,因此需要开启
tree := parse.Parse("", []byte(md), luteEngine.ParseOptions)
ast.Walk(tree.Root, func(n *ast.Node, entering bool) ast.WalkStatus {
if ast.NodeInlineMath == n.Type {
withMath = true
return ast.WalkStop
} else if ast.NodeCodeBlock == n.Type {
if !n.IsFencedCodeBlock {
// 将缩进代码块转换为围栏代码块
n.IsFencedCodeBlock = true
n.CodeBlockFenceChar = '`'
n.PrependChild(&ast.Node{Type: ast.NodeCodeBlockFenceInfoMarker})
n.PrependChild(&ast.Node{Type: ast.NodeCodeBlockFenceOpenMarker, Tokens: []byte("```"), CodeBlockFenceLen: 3})
n.LastChild.InsertAfter(&ast.Node{Type: ast.NodeCodeBlockFenceCloseMarker, Tokens: []byte("```"), CodeBlockFenceLen: 3})
code := n.ChildByType(ast.NodeCodeBlockCode)
if nil != code {
code.Tokens = bytes.TrimPrefix(code.Tokens, []byte(" "))
code.Tokens = bytes.ReplaceAll(code.Tokens, []byte("\n "), []byte("\n"))
code.Tokens = bytes.TrimPrefix(code.Tokens, []byte("\t"))
code.Tokens = bytes.ReplaceAll(code.Tokens, []byte("\n\t"), []byte("\n"))
}
}
}
return ast.WalkContinue
})

md, _ = lute.FormatNodeSync(tree.Root, luteEngine.ParseOptions, luteEngine.RenderOptions)
}
}
}
Expand Down

0 comments on commit 169e6a5

Please sign in to comment.