Skip to content

Commit 2659950

Browse files
committed
🎨 Supports setting Pandoc parameters for export docx #16845 #14970 (comment)
Signed-off-by: Daniel <845765@qq.com> :art: Supports setting Pandoc parameters for export docx #16845 #14970 (comment) Signed-off-by: Daniel <845765@qq.com>
1 parent 9d1302a commit 2659950

1 file changed

Lines changed: 34 additions & 4 deletions

File tree

kernel/model/export.go

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ import (
6060
"github.com/siyuan-note/siyuan/kernel/sql"
6161
"github.com/siyuan-note/siyuan/kernel/treenode"
6262
"github.com/siyuan-note/siyuan/kernel/util"
63+
windows "golang.org/x/sys/windows"
64+
ianaindex "golang.org/x/text/encoding/ianaindex"
6365
)
6466

6567
func ExportCodeBlock(blockID string) (filePath string, err error) {
@@ -775,8 +777,7 @@ func ExportDocx(id, savePath string, removeAssets, merge bool) (fullPath string,
775777
"-o", tmpDocxPath,
776778
}
777779

778-
params := util.RemoveInvalid(Conf.Export.PandocParams)
779-
params = util.ReplaceNewline(params, " ")
780+
params := util.ReplaceNewline(Conf.Export.PandocParams, " ")
780781
if "" != params {
781782
customArgs, parseErr := shellquote.Split(params)
782783
if nil != parseErr {
@@ -802,8 +803,9 @@ func ExportDocx(id, savePath string, removeAssets, merge bool) (fullPath string,
802803
pandoc.Stdin = bytes.NewBufferString(content)
803804
output, err := pandoc.CombinedOutput()
804805
if err != nil {
805-
logging.LogErrorf("export docx failed: %s", gulu.Str.FromBytes(output))
806-
err = errors.New(fmt.Sprintf(Conf.Language(14), gulu.Str.FromBytes(output)))
806+
msg := DecodeCmdOutput(output)
807+
logging.LogErrorf("export docx failed: %s", msg)
808+
err = errors.New(fmt.Sprintf(Conf.Language(14), msg))
807809
return
808810
}
809811

@@ -825,6 +827,34 @@ func ExportDocx(id, savePath string, removeAssets, merge bool) (fullPath string,
825827
return
826828
}
827829

830+
func DecodeCmdOutput(output []byte) string {
831+
if !gulu.OS.IsWindows() {
832+
return string(output)
833+
}
834+
835+
// 1. 检查是否已经是 UTF-8
836+
if utf8.Valid(output) {
837+
return string(output)
838+
}
839+
840+
// 2. 获取当前系统 ANSI 代码页 (例如: 950 是 Big5, 932 是日文 Shift-JIS)
841+
acp := windows.GetACP()
842+
843+
// 3. 将代码页 ID 转为编码格式 (例如 "CP950")
844+
encodingName := fmt.Sprintf("CP%d", acp)
845+
e, err := ianaindex.MIB.Encoding(encodingName)
846+
if err != nil {
847+
return string(output) // 找不到对应编码则返回原始字节转换
848+
}
849+
850+
// 4. 执行解码
851+
decoded, err := e.NewDecoder().Bytes(output)
852+
if err != nil {
853+
return string(output)
854+
}
855+
return string(decoded)
856+
}
857+
828858
func ExportMarkdownHTML(id, savePath string, docx, merge bool) (name, dom string) {
829859
bt := treenode.GetBlockTree(id)
830860
if nil == bt {

0 commit comments

Comments
 (0)