Skip to content

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

:bug: Fix built-in Pandoc initialization and packaging checks https://github.com/siyuan-note/siyuan/issues/18437

Danielcfc146e5846b
diff --git a/app/scripts/afterPack.js b/app/scripts/afterPack.js
index 3262d89c4b..5cd57f1d23 100644
--- a/app/scripts/afterPack.js
+++ b/app/scripts/afterPack.js
@@ -1,25 +1,29 @@
const fs = require("fs");
const fsPromises = fs.promises;
const path = require("path");
+const {Arch} = require("electron-builder");
const unzipper = require("unzipper");
const { trimChangelogs } = require("./trimChangelogs");
module.exports = async function afterPack(context) {
- const {appOutDir, electronPlatformName, packager} = context;
- await extractPackagedPandoc(appOutDir, packager, electronPlatformName);
+ const {appOutDir, arch, electronPlatformName, packager} = context;
+ await extractPackagedPandoc(appOutDir, packager, electronPlatformName, arch);
await removeLanguagePacks(appOutDir, packager, electronPlatformName);
await trimPackagedChangelogs(appOutDir, packager, electronPlatformName);
};
-async function extractPackagedPandoc(appOutDir, packager, platform) {
+async function extractPackagedPandoc(appOutDir, packager, platform, arch) {
const resourcePath = getPackagedResourcePath(appOutDir, packager, platform);
const archivePath = path.join(resourcePath, "pandoc.zip");
try {
await fsPromises.access(archivePath);
} catch (error) {
- if (error.code === "ENOENT") {
+ if (error.code === "ENOENT" && platform === "win32" && arch === Arch.arm64) {
return;
}
+ if (error.code === "ENOENT") {
+ throw new Error(`Packaged Pandoc archive not found: ${archivePath}`);
+ }
throw error;
}
diff --git a/app/scripts/afterPack.test.js b/app/scripts/afterPack.test.js
index 84eea4b4fa..74334ffca5 100644
--- a/app/scripts/afterPack.test.js
+++ b/app/scripts/afterPack.test.js
@@ -3,6 +3,7 @@ const fs = require("node:fs").promises;
const os = require("node:os");
const path = require("node:path");
const test = require("node:test");
+const {Arch} = require("electron-builder");
const {extractPackagedPandoc} = require("./afterPack");
const PANDOC_ZIP = "UEsDBBQAAAAIAFxK/lz1kGVQDQAAAAsAAAAOAAAAYmluL3BhbmRvYy5leGUrSS0u0S1IzEvJTwYAUEsDBBQAAAAIAFxK/lzzFYE4CwAAAAkAAAANAAAAQ09QWVJJR0hULnR4dEvOL6gsykzPKAEAUEsBAhQAFAAAAAgAXEr+XPWQZVANAAAACwAAAA4AAAAAAAAAAAAAAIABAAAAAGJpbi9wYW5kb2MuZXhlUEsBAhQAFAAAAAgAXEr+XPMVgTgLAAAACQAAAA0AAAAAAAAAAAAAAIABOQAAAENPUFlSSUdIVC50eHRQSwUGAAAAAAIAAgB3AAAAbwAAAAAA";
@@ -14,7 +15,7 @@ test("extractPackagedPandoc extracts the executable and removes the archive", as
await fs.mkdir(resourcesDir, {recursive: true});
await fs.writeFile(path.join(resourcesDir, "pandoc.zip"), Buffer.from(PANDOC_ZIP, "base64"));
- await extractPackagedPandoc(appOutDir, {appInfo: {productFilename: "SiYuan"}}, "win32");
+ await extractPackagedPandoc(appOutDir, {appInfo: {productFilename: "SiYuan"}}, "win32", Arch.x64);
assert.equal(await fs.readFile(path.join(resourcesDir, "pandoc", "bin", "pandoc.exe"), "utf8"), "test-pandoc");
assert.equal(await fs.readFile(path.join(resourcesDir, "pandoc", "COPYRIGHT.txt"), "utf8"), "copyright");
@@ -24,11 +25,24 @@ test("extractPackagedPandoc extracts the executable and removes the archive", as
}
});
-test("extractPackagedPandoc skips platforms without an archive", async () => {
+test("extractPackagedPandoc skips Windows ARM64 without an archive", async () => {
const appOutDir = await fs.mkdtemp(path.join(os.tmpdir(), "siyuan-after-pack-"));
try {
await fs.mkdir(path.join(appOutDir, "resources"), {recursive: true});
- await extractPackagedPandoc(appOutDir, {appInfo: {productFilename: "SiYuan"}}, "win32");
+ await extractPackagedPandoc(appOutDir, {appInfo: {productFilename: "SiYuan"}}, "win32", Arch.arm64);
+ } finally {
+ await fs.rm(appOutDir, {force: true, recursive: true});
+ }
+});
+
+test("extractPackagedPandoc rejects a missing archive for supported targets", async () => {
+ const appOutDir = await fs.mkdtemp(path.join(os.tmpdir(), "siyuan-after-pack-"));
+ try {
+ await fs.mkdir(path.join(appOutDir, "resources"), {recursive: true});
+ await assert.rejects(
+ extractPackagedPandoc(appOutDir, {appInfo: {productFilename: "SiYuan"}}, "win32", Arch.x64),
+ /Packaged Pandoc archive not found/
+ );
} finally {
await fs.rm(appOutDir, {force: true, recursive: true});
}
diff --git a/kernel/api/setting.go b/kernel/api/setting.go
index e1a79f6f99..80c6ce001c 100644
--- a/kernel/api/setting.go
+++ b/kernel/api/setting.go
@@ -482,16 +482,19 @@ func setExport(c *gin.Context) {
return
}
+ previousPandocBin := model.Conf.Export.PandocBin
if "" != export.PandocBin {
if !util.IsValidPandocBin(export.PandocBin) {
util.PushErrMsg(fmt.Sprintf(model.Conf.Language(117), export.PandocBin), 5000)
- export.PandocBin = model.Conf.Export.PandocBin
+ export.PandocBin = previousPandocBin
}
}
model.Conf.Export = export
model.Conf.Save()
- util.InitPandoc(export.PandocBin)
+ if previousPandocBin != export.PandocBin {
+ util.InitPandoc(export.PandocBin)
+ }
ret.Data = model.Conf.Export
}
diff --git a/kernel/model/export.go b/kernel/model/export.go
index 6bb2e231cf..7ec7fbd647 100644
--- a/kernel/model/export.go
+++ b/kernel/model/export.go
@@ -990,11 +990,11 @@ func ExportPreview(id string, fillCSSVar bool) (retStdHTML string) {
func ExportDocx(id, savePath string, removeAssets, merge bool) (fullPath string, err error) {
err = withExportReadLockByBlockID(id, func() error {
- pandocBinPath := util.GetPandocBinPath()
- if !util.IsValidPandocBin(pandocBinPath) {
+ pandocRuntime := util.GetPandocRuntime()
+ if !util.IsValidPandocBin(pandocRuntime.BinPath) {
util.InitPandoc(Conf.Export.PandocBin)
- pandocBinPath = util.GetPandocBinPath()
- if !util.IsValidPandocBin(pandocBinPath) {
+ pandocRuntime = util.GetPandocRuntime()
+ if !util.IsValidPandocBin(pandocRuntime.BinPath) {
return errors.New(Conf.Language(115))
}
}
@@ -1031,15 +1031,15 @@ func ExportDocx(id, savePath string, removeAssets, merge bool) (fullPath string,
}
}
- if !hasPandocOption(args, "--lua-filter") && "" != util.PandocColorFilterPath {
- args = append(args, "--lua-filter", util.PandocColorFilterPath)
+ if !hasPandocOption(args, "--lua-filter") && "" != pandocRuntime.ColorFilterPath {
+ args = append(args, "--lua-filter", pandocRuntime.ColorFilterPath)
}
- if !hasPandocOption(args, "--reference-doc") && "" != util.PandocTemplatePath {
- args = append(args, "--reference-doc", util.PandocTemplatePath)
+ if !hasPandocOption(args, "--reference-doc") && "" != pandocRuntime.TemplatePath {
+ args = append(args, "--reference-doc", pandocRuntime.TemplatePath)
}
- pandoc := exec.Command(pandocBinPath, args...)
+ pandoc := exec.Command(pandocRuntime.BinPath, args...)
gulu.CmdAttr(pandoc)
pandoc.Stdin = bytes.NewBufferString(content)
output, pandocErr := pandoc.CombinedOutput()
diff --git a/kernel/util/pandoc.go b/kernel/util/pandoc.go
index 8280d8e4e7..fe2d1ebac5 100644
--- a/kernel/util/pandoc.go
+++ b/kernel/util/pandoc.go
@@ -33,7 +33,7 @@ import (
var ErrPandocNotFound = errors.New("not found executable pandoc")
func ConvertPandoc(dir string, args ...string) (path string, err error) {
- pandocBinPath := GetPandocBinPath()
+ pandocBinPath := GetPandocRuntime().BinPath
if "" == pandocBinPath || ContainerStd != Container {
err = ErrPandocNotFound
return
@@ -66,7 +66,7 @@ func Pandoc(from, to, o, content string) (err error) {
return
}
- pandocBinPath := GetPandocBinPath()
+ pandocBinPath := GetPandocRuntime().BinPath
if "" == pandocBinPath || ContainerStd != Container {
err = ErrPandocNotFound
return
@@ -102,17 +102,21 @@ func Pandoc(from, to, o, content string) (err error) {
return
}
+type PandocRuntime struct {
+ BinPath string
+ TemplatePath string
+ ColorFilterPath string
+}
+
var (
- activePandocBinPath string // 当前使用的 Pandoc 可执行文件路径
- PandocTemplatePath string // Pandoc Docx 模板文件路径
- PandocColorFilterPath string // Pandoc 颜色过滤器路径
- pandocInitMutex sync.Mutex
+ activePandocRuntime PandocRuntime
+ pandocInitMutex sync.Mutex
)
-func GetPandocBinPath() string {
+func GetPandocRuntime() PandocRuntime {
pandocInitMutex.Lock()
defer pandocInitMutex.Unlock()
- return activePandocBinPath
+ return activePandocRuntime
}
func InitPandoc(customPandocBinPath string) {
@@ -122,26 +126,29 @@ func InitPandoc(customPandocBinPath string) {
pandocInitMutex.Lock()
defer pandocInitMutex.Unlock()
- PandocTemplatePath = filepath.Join(WorkingDir, "pandoc-resources", "pandoc-template.docx")
- if !gulu.File.IsExist(PandocTemplatePath) {
- PandocTemplatePath = filepath.Join(WorkingDir, "pandoc", "pandoc-resources", "pandoc-template.docx")
+ runtimeState := PandocRuntime{}
+ defer func() {
+ activePandocRuntime = runtimeState
+ }()
+
+ runtimeState.TemplatePath = filepath.Join(WorkingDir, "pandoc-resources", "pandoc-template.docx")
+ if !gulu.File.IsExist(runtimeState.TemplatePath) {
+ runtimeState.TemplatePath = filepath.Join(WorkingDir, "pandoc", "pandoc-resources", "pandoc-template.docx")
}
- if !gulu.File.IsExist(PandocTemplatePath) {
- PandocTemplatePath = ""
- logging.LogWarnf("pandoc template file [%s] not found", PandocTemplatePath)
+ if !gulu.File.IsExist(runtimeState.TemplatePath) {
+ logging.LogWarnf("pandoc template file [%s] not found", runtimeState.TemplatePath)
+ runtimeState.TemplatePath = ""
}
- PandocColorFilterPath = filepath.Join(WorkingDir, "pandoc-resources", "pandoc_color_filter.lua")
- if !gulu.File.IsExist(PandocColorFilterPath) {
- PandocColorFilterPath = filepath.Join(WorkingDir, "pandoc", "pandoc-resources", "pandoc_color_filter.lua")
+ runtimeState.ColorFilterPath = filepath.Join(WorkingDir, "pandoc-resources", "pandoc_color_filter.lua")
+ if !gulu.File.IsExist(runtimeState.ColorFilterPath) {
+ runtimeState.ColorFilterPath = filepath.Join(WorkingDir, "pandoc", "pandoc-resources", "pandoc_color_filter.lua")
}
- if !gulu.File.IsExist(PandocColorFilterPath) {
- PandocColorFilterPath = ""
- logging.LogWarnf("pandoc color filter file [%s] not found", PandocColorFilterPath)
+ if !gulu.File.IsExist(runtimeState.ColorFilterPath) {
+ logging.LogWarnf("pandoc color filter file [%s] not found", runtimeState.ColorFilterPath)
+ runtimeState.ColorFilterPath = ""
}
- activePandocBinPath = ""
-
tempPandocDir := filepath.Join(TempDir, "pandoc")
installedPandocBinPath := pandocBinPath(filepath.Join(WorkingDir, "pandoc"))
installedPandocVer := getPandocVer(installedPandocBinPath)
@@ -149,8 +156,8 @@ func InitPandoc(customPandocBinPath string) {
customPandocBinPath = strings.TrimSpace(customPandocBinPath)
if "" != customPandocBinPath && !isPathWithin(tempPandocDir, customPandocBinPath) {
if pandocVer := getPandocVer(customPandocBinPath); "" != pandocVer {
- activePandocBinPath = customPandocBinPath
- logging.LogInfof("custom pandoc [ver=%s, bin=%s]", pandocVer, activePandocBinPath)
+ runtimeState.BinPath = customPandocBinPath
+ logging.LogInfof("custom pandoc [ver=%s, bin=%s]", pandocVer, runtimeState.BinPath)
if "" != installedPandocVer {
removeLegacyPandocDir(tempPandocDir)
}
@@ -159,16 +166,16 @@ func InitPandoc(customPandocBinPath string) {
}
if "" != installedPandocVer {
- activePandocBinPath = installedPandocBinPath
- logging.LogInfof("built-in pandoc [ver=%s, bin=%s]", installedPandocVer, activePandocBinPath)
+ runtimeState.BinPath = installedPandocBinPath
+ logging.LogInfof("built-in pandoc [ver=%s, bin=%s]", installedPandocVer, runtimeState.BinPath)
removeLegacyPandocDir(tempPandocDir)
return
}
- activePandocBinPath = pandocBinPath(tempPandocDir)
- pandocVer := getPandocVer(activePandocBinPath)
+ runtimeState.BinPath = pandocBinPath(tempPandocDir)
+ pandocVer := getPandocVer(runtimeState.BinPath)
if "" != pandocVer {
- logging.LogInfof("built-in pandoc [ver=%s, bin=%s]", pandocVer, activePandocBinPath)
+ logging.LogInfof("built-in pandoc [ver=%s, bin=%s]", pandocVer, runtimeState.BinPath)
return
}
@@ -194,27 +201,27 @@ func InitPandoc(customPandocBinPath string) {
}
if !gulu.File.IsExist(pandocZip) {
- activePandocBinPath = ""
+ runtimeState.BinPath = ""
logging.LogErrorf("pandoc zip [%s] not found", pandocZip)
return
}
if err := gulu.Zip.Unzip(pandocZip, tempPandocDir); err != nil {
- activePandocBinPath = ""
+ runtimeState.BinPath = ""
logging.LogErrorf("unzip pandoc failed: %s", err)
return
}
if gulu.OS.IsDarwin() || gulu.OS.IsLinux() {
- exec.Command("chmod", "+x", activePandocBinPath).CombinedOutput()
+ exec.Command("chmod", "+x", runtimeState.BinPath).CombinedOutput()
}
- pandocVer = getPandocVer(activePandocBinPath)
+ pandocVer = getPandocVer(runtimeState.BinPath)
if "" == pandocVer {
- activePandocBinPath = ""
+ runtimeState.BinPath = ""
logging.LogErrorf("initialized built-in pandoc is invalid")
return
}
- logging.LogInfof("initialized built-in pandoc [ver=%s, bin=%s]", pandocVer, activePandocBinPath)
+ logging.LogInfof("initialized built-in pandoc [ver=%s, bin=%s]", pandocVer, runtimeState.BinPath)
}
func pandocBinPath(pandocDir string) string {