🎨 Code content in templates is not properly escaped https://github.com/siyuan-note/siyuan/issues/9649

This commit is contained in:
Daniel 2023-11-14 22:35:46 +08:00
parent 9dc68a28e4
commit ea2a2bbf03
No known key found for this signature in database
GPG key ID: 86211BA83DF03017

View file

@ -20,7 +20,6 @@ import (
"bytes"
"errors"
"fmt"
"github.com/siyuan-note/siyuan/kernel/av"
"io/fs"
"os"
"path/filepath"
@ -33,10 +32,11 @@ import (
"github.com/88250/lute/ast"
"github.com/88250/lute/parse"
"github.com/88250/lute/render"
sprig "github.com/Masterminds/sprig/v3"
"github.com/Masterminds/sprig/v3"
"github.com/araddon/dateparse"
"github.com/siyuan-note/filelock"
"github.com/siyuan-note/logging"
"github.com/siyuan-note/siyuan/kernel/av"
"github.com/siyuan-note/siyuan/kernel/search"
"github.com/siyuan-note/siyuan/kernel/sql"
"github.com/siyuan-note/siyuan/kernel/treenode"
@ -147,6 +147,25 @@ func DocSaveAsTemplate(id, name string, overwrite bool) (code int, err error) {
tree := prepareExportTree(bt)
addBlockIALNodes(tree, true)
ast.Walk(tree.Root, func(n *ast.Node, entering bool) ast.WalkStatus {
if !entering {
return ast.WalkContinue
}
// Code content in templates is not properly escaped https://github.com/siyuan-note/siyuan/issues/9649
switch n.Type {
case ast.NodeCodeBlockCode:
n.Tokens = bytes.ReplaceAll(n.Tokens, []byte("""), []byte("\""))
case ast.NodeCodeSpanContent:
n.Tokens = bytes.ReplaceAll(n.Tokens, []byte("""), []byte("\""))
case ast.NodeTextMark:
if n.IsTextMarkType("code") {
n.TextMarkTextContent = strings.ReplaceAll(n.TextMarkTextContent, """, "\"")
}
}
return ast.WalkContinue
})
luteEngine := NewLute()
formatRenderer := render.NewFormatRenderer(tree, luteEngine.RenderOptions)
md := formatRenderer.Render()