{"record":{"id":"67a849237dfa89cf","repo":"siyuan-note/siyuan","slug":"parse-template-tree-failed","errorCode":null,"errorMessage":"parse template tree failed","messagePattern":"parse template tree failed","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"kernel/model/template_export_attrs.go","lineNumber":34,"sourceCode":"\npackage model\n\nimport (\n\t\"bytes\"\n\t\"errors\"\n\t\"strings\"\n\n\t\"github.com/88250/lute/ast\"\n\t\"github.com/88250/lute/parse\"\n)\n\nconst templateDocumentAttributeMarker = \"siyuan-template-doc-attrs-v1\"\n\nfunc parseTemplateKTree(markdown []byte) (*parse.Tree, error) {\n\tengine := NewLute()\n\ttree := parse.Parse(\"\", markdown, engine.ParseOptions)\n\tif tree == nil {\n\t\treturn nil, errors.New(\"parse template tree failed\")\n\t}\n\tnormalizeTree(tree)\n\tif err := applyTemplateDocumentAttributes(tree); err != nil {\n\t\treturn nil, err\n\t}\n\treturn tree, nil\n}\n\n// 独立文档属性保留在原位置求值，渲染完成后再合并到根节点。\nfunc applyTemplateDocumentAttributes(tree *parse.Tree) error {\n\tfor node := tree.Root.FirstChild; node != nil; {\n\t\tnext := node.Next\n\t\tinfo := node.ChildByType(ast.NodeCodeBlockFenceInfoMarker)\n\t\tif node.Type == ast.NodeCodeBlock && info != nil && string(info.CodeBlockInfo) == templateDocumentAttributeMarker {\n\t\t\tcode := node.ChildByType(ast.NodeCodeBlockCode)\n\t\t\tif code == nil {\n\t\t\t\treturn errors.New(\"invalid template document attributes\")\n\t\t\t}","sourceCodeStart":16,"sourceCodeEnd":52,"githubUrl":"https://github.com/siyuan-note/siyuan/blob/9f775e8a12daef8255556097396f9b2739078892/kernel/model/template_export_attrs.go#L16-L52","documentation":"parseTemplateKTree parses rendered template markdown with Lute and fails when the parser returns a nil tree. This is a defensive invariant check: Lute's parse.Parse virtually always returns a tree, so a nil result indicates an unexpected parser failure on the rendered markdown bytes.","triggerScenarios":"Raised from parseTemplateKTree when parse.Parse(\"\", markdown, engine.ParseOptions) returns nil, i.e. the markdown produced by a createDocTree template render could not be turned into a parse tree by the Lute engine.","commonSituations":"Extremely malformed template output after .action{} expansion (e.g. output that truncates mid-block), or a Lute engine/ParseOptions misconfiguration introduced by a kernel change.","solutions":["Dump the rendered markdown that was passed to parseTemplateKTree and inspect it for corruption or truncation","Fix the template so its output is well-formed Markdown/Kramdown","Check output size limits; a truncated render (e.g. output-size cap cutting mid construct) can yield unparseable bytes","If it reproduces with valid markdown, report it as a Lute/kernel bug since nil tree is unexpected"],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":"if tree == nil { return nil, errors.New(\"parse template tree failed\") }","tryCatchPattern":"tree, err := parseTemplateKTree(md); if err != nil { return fmt.Errorf(\"render failed: %w\", err) }","preventionTips":["Verify template output is well-formed Markdown before packaging","Avoid templates whose output can truncate mid-construct","Reproduce with a minimal template if the parser unexpectedly returns nil and report upstream"],"tags":["markdown","parse","lute","internal"],"backgroundTag":"internal-invariant-violation","analyzedSha":"9f775e8a12daef8255556097396f9b2739078892","analyzedAt":"2026-09-19T03:17:15.984Z","contentChangedAt":"2026-09-19T03:17:15.984Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}