Skip to content

An open-source, privacy-first, self-hosted knowledge workspace where humans and AI agents work together 开源、隐私优先、自托管的知识工作空间,让人与智能体在此协作

:art: Limit KaTex macro maximum recursive parsing depth is 16 https://github.com/siyuan-note/siyuan/issues/10484

Daniel131d8b607cbc
diff --git a/kernel/model/export.go b/kernel/model/export.go
index ff6c4d1611..481d40e184 100644
--- a/kernel/model/export.go
+++ b/kernel/model/export.go
@@ -1933,7 +1933,7 @@ func processKaTexMacros(n *ast.Node) {
mathContent = escapeKaTexSupportedFunctions(mathContent)
usedMacros := extractUsedMacros(mathContent, &keys)
for _, usedMacro := range usedMacros {
- depth := 1 // Limit KaTex macro maximum recursive parsing depth is 16 https://github.com/siyuan-note/siyuan/issues/10484
+ depth := 1
expanded := resolveKaTexMacro(usedMacro, &macros, &keys, &depth)
expanded = unescapeKaTexSupportedFunctions(expanded)
mathContent = strings.ReplaceAll(mathContent, usedMacro, expanded)
diff --git a/kernel/model/export_katex.go b/kernel/model/export_katex.go
index a6ddc92a49..4859a0ea94 100644
--- a/kernel/model/export_katex.go
+++ b/kernel/model/export_katex.go
@@ -910,6 +910,11 @@ func init() {
func resolveKaTexMacro(macroName string, macros *map[string]string, keys *[]string, depth *int) string {
v := (*macros)[macroName]
+ if *depth > 16 {
+ // Limit KaTex macro maximum recursive parsing depth is 16 https://github.com/siyuan-note/siyuan/issues/10484
+ return v
+ }
+
sort.Slice(*keys, func(i, j int) bool { return len((*keys)[i]) > len((*keys)[j]) })
*depth++
for _, k := range *keys {
@@ -919,9 +924,6 @@ func resolveKaTexMacro(macroName string, macros *map[string]string, keys *[]stri
v = unescapeKaTexSupportedFunctions(escaped)
(*macros)[macroName] = v
}
- if *depth > 16 {
- return v
- }
}
return v
}