Browse Source

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

Daniel 1 year ago
parent
commit
ea2a2bbf03
1 changed files with 21 additions and 2 deletions
  1. 21 2
      kernel/model/template.go

+ 21 - 2
kernel/model/template.go

@@ -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()