{"record":{"id":"459079fe9290c9d3","repo":"siyuan-note/siyuan","slug":"createdoctree-is-only-available-when-manually-inse","errorCode":null,"errorMessage":"createDocTree is only available when manually inserting a template in the editor","messagePattern":"createDocTree is only available when manually inserting a template in the editor","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"kernel/model/template_doc_tree.go","lineNumber":218,"sourceCode":"func templateDocTreeStringField(definition map[string]any, key string) (string, error) {\n\tvalue, exists := definition[key]\n\tif !exists {\n\t\treturn \"\", nil\n\t}\n\tret, ok := value.(string)\n\tif !ok {\n\t\treturn \"\", fmt.Errorf(\"createDocTree document %s must be a string\", key)\n\t}\n\tret = strings.TrimSpace(ret)\n\tif \"\" == ret {\n\t\treturn \"\", fmt.Errorf(\"createDocTree document %s must not be empty\", key)\n\t}\n\treturn ret, nil\n}\n\nfunc (collector *templateDocTreeCollector) create(def any) (string, error) {\n\tif !collector.enabled || !collector.allowCreation {\n\t\treturn \"\", errors.New(\"createDocTree is only available when manually inserting a template in the editor\")\n\t}\n\tnodes, err := parseTemplateDocTreeDefinition(def)\n\tif nil != err {\n\t\treturn \"\", err\n\t}\n\tif maxTemplateDocTreeDocs < len(flattenTemplateDocTreeNodes0(collector.nodes))+len(flattenTemplateDocTreeNodes0(nodes)) {\n\t\treturn \"\", fmt.Errorf(\"createDocTree exceeds the maximum document count of %d\", maxTemplateDocTreeDocs)\n\t}\n\tcollector.bindNodes(nodes, collector.rootID, collector.rootPath, collector.rootHPath)\n\tcollector.nodes = append(collector.nodes, nodes...)\n\treturn \"\", nil\n}\n\nfunc (collector *templateDocTreeCollector) bindNodes(nodes []*TemplateDocTreeNode, parentID, parentPath, parentHPath string) {\n\tfor _, node := range nodes {\n\t\tnode.ParentID = parentID\n\t\tnode.path = strings.TrimSuffix(parentPath, \".sy\") + \"/\" + node.ID + \".sy\"\n\t\tnode.HPath = path.Join(parentHPath, node.Title)","sourceCodeStart":200,"sourceCodeEnd":236,"githubUrl":"https://github.com/siyuan-note/siyuan/blob/9f775e8a12daef8255556097396f9b2739078892/kernel/model/template_doc_tree.go#L200-L236","documentation":"The createDocTree action exposed to templates is a privileged collector side effect: it only works while a template is being manually inserted into the editor (render mode editorInsert), where enabled and allowCreation are both true. Any other context, such as preview rendering or other template invocations, refuses document creation to prevent templates from mutating the workspace unexpectedly.","triggerScenarios":"Calling .createDocTree from a template rendered outside manual editor insertion — e.g. preview render, content insertion mode, or an automated/background template render — where collector.enabled or collector.allowCreation is false.","commonSituations":"Testing templates via preview instead of inserting them in the editor; invoking the template action from a script or plugin render path; the test TestTemplateDocTreeCollectorLimitsDocumentsAcrossCallS exercises create with the guard disabled.","solutions":["Insert the template manually in the editor (template insertion dialog / editorInsert mode) so createDocTree is allowed.","Do not call createDocTree from preview or content-mode renders; restructure the template to only create documents during manual insertion.","If documents must be created programmatically, use the kernel's document-creation API instead of the template action.","For tests, enable the collector's allowCreation flag before invoking create."],"exampleFix":"// before (preview render context)\n// template calls createDocTree -> refused\n// after\n// open the template insertion dialog in the editor and insert manually\n// so the collector runs with enabled=true, allowCreation=true","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"try {\n  createDocTree(def);\n} catch (e) {\n  if (String(e).includes(\"only available when manually inserting\")) {\n    // fall back: create documents via the kernel API instead\n  }\n}","preventionTips":["Only use createDocTree in templates intended for manual editor insertion.","Never rely on it in preview or content-render modes.","Use the kernel document-creation API for programmatic creation.","Enable allowCreation explicitly in test setups."],"tags":["template","permissions","feature-gate"],"backgroundTag":"feature-not-enabled","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"}