🎨 Built-in Pandoc export .docx template https://github.com/siyuan-note/siyuan/issues/16861

Signed-off-by: Daniel <845765@qq.com>
This commit is contained in:
Daniel 2026-01-18 20:50:41 +08:00
parent 1f02650b38
commit 1012d8b486
No known key found for this signature in database
GPG key ID: 86211BA83DF03017
11 changed files with 44 additions and 4 deletions

View file

@ -64,3 +64,5 @@ extraResources:
filter: "!**/{.DS_Store}"
- from: "pandoc/pandoc-darwin-arm64.zip"
to: "pandoc.zip"
- from: "pandoc/pandoc-template.docx"
to: "pandoc-template.docx"

View file

@ -64,3 +64,5 @@ extraResources:
filter: "!**/{.DS_Store}"
- from: "pandoc/pandoc-darwin-amd64.zip"
to: "pandoc.zip"
- from: "pandoc/pandoc-template.docx"
to: "pandoc-template.docx"

View file

@ -67,4 +67,6 @@ extraResources:
to: "appearance/fonts"
filter: "!**/{.DS_Store}"
- from: "pandoc/pandoc-linux-arm64.zip"
to: "pandoc.zip"
to: "pandoc.zip"
- from: "pandoc/pandoc-template.docx"
to: "pandoc-template.docx"

View file

@ -64,4 +64,6 @@ extraResources:
to: "appearance/fonts"
filter: "!**/{.DS_Store}"
- from: "pandoc/pandoc-linux-amd64.zip"
to: "pandoc.zip"
to: "pandoc.zip"
- from: "pandoc/pandoc-template.docx"
to: "pandoc-template.docx"

View file

@ -71,3 +71,5 @@ extraResources:
filter: "!**/{.DS_Store}"
- from: "pandoc/pandoc-windows-amd64.zip"
to: "pandoc.zip"
- from: "pandoc/pandoc-template.docx"
to: "pandoc-template.docx"

Binary file not shown.

View file

@ -44,6 +44,7 @@ require (
github.com/jaypipes/ghw v0.21.2
github.com/jinzhu/copier v0.4.0
github.com/json-iterator/go v1.1.12
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51
github.com/klippa-app/go-pdfium v1.17.2
github.com/mattn/go-sqlite3 v2.0.3+incompatible
github.com/mitchellh/go-ps v1.0.0

View file

@ -262,6 +262,8 @@ github.com/juju/errors v1.0.0 h1:yiq7kjCLll1BiaRuNY53MGI0+EQ3rF6GB+wvboZDefM=
github.com/juju/errors v1.0.0/go.mod h1:B5x9thDqx0wIMH3+aLIMP9HjItInYWObRovoCFM5Qe8=
github.com/juju/loggo v0.0.0-20190526231331-6e530bcce5d8/go.mod h1:vgyd7OREkbtVEN/8IXZe5Ooef3LQePvuBm9UWj6ZL8U=
github.com/juju/testing v0.0.0-20191001232224-ce9dec17d28b/go.mod h1:63prj8cnj0tU0S9OHjGJn+b1h0ZghCndfnbQolrYTwA=
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51 h1:Z9n2FFNUXsshfwJMBgNA0RU6/i7WVaAegv3PtuIHPMs=
github.com/kballard/go-shellquote v0.0.0-20180428030007-95032a82bc51/go.mod h1:CzGEWj7cYgsdH8dAjBGEr58BoE7ScuLd+fwFZ44+/x8=
github.com/klauspost/compress v1.4.1/go.mod h1:RyIbtBH6LamlWaDj8nUwkbUhJ87Yi3uG0guNDohfE1A=
github.com/klauspost/compress v1.18.2 h1:iiPHWW0YrcFgpBYhsA6D1+fqHssJscY/Tm/y2Uqnapk=
github.com/klauspost/compress v1.18.2/go.mod h1:R0h/fSBs8DE4ENlcrlib3PsXS61voFxhIs2DeRhCvJ4=

View file

@ -1262,6 +1262,14 @@ func subscribeConfEvents() {
eventbus.Subscribe(util.EvtConfPandocInitialized, func() {
logging.LogInfof("pandoc initialized, set pandoc bin to [%s]", util.PandocBinPath)
Conf.Export.PandocBin = util.PandocBinPath
params := util.RemoveInvalid(Conf.Export.PandocParams)
if !strings.Contains(params, "--reference-doc") && "" != util.PandocTemplatePath {
params += " --reference-doc"
params += " \"" + util.PandocTemplatePath + "\""
Conf.Export.PandocParams = strings.TrimSpace(params)
}
Conf.Save()
})
}

View file

@ -45,6 +45,7 @@ import (
"github.com/emirpasic/gods/sets/hashset"
"github.com/emirpasic/gods/stacks/linkedliststack"
"github.com/imroc/req/v3"
shellquote "github.com/kballard/go-shellquote"
"github.com/pdfcpu/pdfcpu/pkg/api"
"github.com/pdfcpu/pdfcpu/pkg/font"
"github.com/pdfcpu/pdfcpu/pkg/pdfcpu"
@ -773,7 +774,12 @@ func ExportDocx(id, savePath string, removeAssets, merge bool) (fullPath string,
params := util.RemoveInvalid(Conf.Export.PandocParams)
if "" != params {
args = append(args, strings.Split(params, " ")...)
customArgs, parseErr := shellquote.Split(params)
if nil != parseErr {
logging.LogErrorf("parse pandoc custom params [%s] failed: %s", params, parseErr)
} else {
args = append(args, customArgs...)
}
}
pandoc := exec.Command(Conf.Export.PandocBin, args...)

View file

@ -101,7 +101,8 @@ func Pandoc(from, to, o, content string) (err error) {
}
var (
PandocBinPath string // Pandoc 可执行文件路径
PandocBinPath string // Pandoc 可执行文件路径
PandocTemplatePath string // Pandoc Docx 模板文件路径
)
func InitPandoc() {
@ -128,6 +129,18 @@ func InitPandoc() {
}
}
PandocTemplatePath = filepath.Join(pandocDir, "pandoc-template.docx")
if !gulu.File.IsExist(PandocTemplatePath) {
PandocTemplatePath = filepath.Join(WorkingDir, "pandoc-template.docx")
if "dev" == Mode || !gulu.File.IsExist(PandocTemplatePath) {
PandocTemplatePath = filepath.Join(WorkingDir, "pandoc/pandoc-template.docx")
}
}
if !gulu.File.IsExist(PandocTemplatePath) {
PandocTemplatePath = ""
logging.LogWarnf("pandoc template file [%s] not found", PandocTemplatePath)
}
defer eventbus.Publish(EvtConfPandocInitialized)
if gulu.OS.IsWindows() {