{"record":{"id":"a7e5617e19adf05b","repo":"siyuan-note/siyuan","slug":"template-source-is-not-utf-8","errorCode":null,"errorMessage":"template source is not UTF-8","messagePattern":"template source is not UTF-8","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"kernel/model/template_manage.go","lineNumber":163,"sourceCode":"\t\th.Write(content)\n\t}\n\treturn fmt.Sprintf(\"%x\", h.Sum(nil)), err\n}\n\nfunc readTemplateSource(root *os.Root, p string) (string, error) {\n\tinfo, err := root.Stat(p)\n\tif err != nil {\n\t\treturn \"\", err\n\t}\n\tif !info.Mode().IsRegular() || info.Size() > maxTemplateSourceSize {\n\t\treturn \"\", errors.New(\"invalid template source file\")\n\t}\n\tcontent, err := root.ReadFile(p)\n\tif err != nil {\n\t\treturn \"\", err\n\t}\n\tif !utf8.Valid(content) {\n\t\treturn \"\", errors.New(\"template source is not UTF-8\")\n\t}\n\treturn string(content), nil\n}\n\n// 同目录临时文件写入完成后替换，写入失败时保留原模板。\nfunc writeTemplateSource(root *os.Root, p, content string, create bool) error {\n\tif !utf8.ValidString(content) {\n\t\treturn errors.New(\"invalid template source\")\n\t}\n\tif create {\n\t\tfile, err := root.OpenFile(p, os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0644)\n\t\tif err != nil {\n\t\t\treturn err\n\t\t}\n\t\t_, err = file.WriteString(content)\n\t\tif err == nil {\n\t\t\terr = file.Sync()\n\t\t}","sourceCodeStart":145,"sourceCodeEnd":181,"githubUrl":"https://github.com/siyuan-note/siyuan/blob/9f775e8a12daef8255556097396f9b2739078892/kernel/model/template_manage.go#L145-L181","documentation":"readTemplateSource requires template file content to be valid UTF-8 and throws this error otherwise. Template sources are rendered as UTF-8 text end to end, so non-UTF-8 bytes (e.g. legacy encodings) cannot be handled.","triggerScenarios":"ManageTemplateFiles reading a template file encoded in GBK/Shift-JIS/Latin-1 or containing binary bytes.","commonSituations":"Templates imported from Windows editors saving legacy codepages, files downloaded from sources without encoding metadata, or binary data accidentally saved as .md.","solutions":["Re-encode the file to UTF-8 (iconv -f GBK -t UTF-8, or editor 'Save with encoding: UTF-8')","Re-save the template from a text editor with UTF-8 selected","Remove binary content from the file"],"exampleFix":"# before: templates/old.md is GBK-encoded\n# after\niconv -f GBK -t UTF-8 templates/old.md > templates/old-utf8.md","handlingStrategy":"validation","validationCode":"data, err := os.ReadFile(p)\nif err != nil || !utf8.Valid(data) { /* re-encode to UTF-8 before calling */ }","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Save all template files as UTF-8","Convert legacy-encoded imports with iconv before placing them in the templates folder"],"tags":["encoding","utf-8","validation"],"backgroundTag":"invalid-encoding","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"}