🎨 导出为 .docx 时无法正常转换公式 Fix https://github.com/siyuan-note/siyuan/issues/4062

This commit is contained in:
Liang Ding 2022-09-01 15:16:17 +08:00
parent 70b3b0fc3b
commit d2624449a5
No known key found for this signature in database
GPG key ID: 136F30F901A2231D
3 changed files with 19 additions and 7 deletions

View file

@ -7,7 +7,7 @@ require (
github.com/88250/css v0.1.2
github.com/88250/flock v0.8.2
github.com/88250/gulu v1.2.3-0.20220720144315-065ef35ec583
github.com/88250/lute v1.7.5-0.20220829155050-5c5d032f7b68
github.com/88250/lute v1.7.5-0.20220901071335-8565d1e64571
github.com/88250/melody v0.0.0-20201115062536-c0b3394adcd1
github.com/88250/pdfcpu v0.3.13
github.com/88250/vitess-sqlparser v0.0.0-20210205111146-56a2ded2aba1

View file

@ -19,8 +19,8 @@ github.com/88250/go-sqlite3 v1.14.13-0.20220714142610-fbbda1ee84f5 h1:8HdZozCsXS
github.com/88250/go-sqlite3 v1.14.13-0.20220714142610-fbbda1ee84f5/go.mod h1:NyWgC/yNuGj7Q9rpYnZvas74GogHl5/Z4A/KQRfk6bU=
github.com/88250/gulu v1.2.3-0.20220720144315-065ef35ec583 h1:FhA/zJemLrbOYJpdMIMBezO5rGigQSdPR1kv+aztHfA=
github.com/88250/gulu v1.2.3-0.20220720144315-065ef35ec583/go.mod h1:I1qBzsksFL2ciGSuqDE7R3XW4BUMrfDgOvSXEk7FsAI=
github.com/88250/lute v1.7.5-0.20220829155050-5c5d032f7b68 h1:BSVqhd18YpBEECvek7lJu13qcmQGJgQwaiLrnsUyRvw=
github.com/88250/lute v1.7.5-0.20220829155050-5c5d032f7b68/go.mod h1:Bdu9LRNjQhtL3TftbtpjIWTwDVAXoS7AD8QsZQPk7zo=
github.com/88250/lute v1.7.5-0.20220901071335-8565d1e64571 h1:InniTKfj4OZjE0VFhLZmJT6PKGZQfqw6k06/Zvwh35c=
github.com/88250/lute v1.7.5-0.20220901071335-8565d1e64571/go.mod h1:Bdu9LRNjQhtL3TftbtpjIWTwDVAXoS7AD8QsZQPk7zo=
github.com/88250/melody v0.0.0-20201115062536-c0b3394adcd1 h1:9Cb+iN639vUI2OcIBc+4oGwml9/0J6bL6dWNb8Al+1s=
github.com/88250/melody v0.0.0-20201115062536-c0b3394adcd1/go.mod h1:jH6MMPr8G7AMzaVmWHXZQiB1DKO3giWbcWZ7UoJ1teI=
github.com/88250/pdfcpu v0.3.13 h1:touMWMZkCGalMIbEg9bxYp7rETM+zwb9hXjwhqi4I7Q=

View file

@ -210,18 +210,22 @@ func ExportDocx(id, savePath string, removeAssets bool) (err error) {
}
tmpDir := filepath.Join(util.TempDir, "export", gulu.Rand.String(7))
if err = os.MkdirAll(tmpDir, 0755); nil != err {
return
}
defer os.Remove(tmpDir)
name, dom := ExportMarkdownHTML(id, tmpDir, true)
name, content := ExportMarkdownHTML(id, tmpDir, true)
tmpDocxPath := filepath.Join(tmpDir, name+".docx")
args := []string{ // pandoc -f html --resource-path=请从这里开始 请从这里开始\index.html -o test.docx
"-f", "html",
"-f", "html+tex_math_dollars",
"--resource-path", tmpDir,
"-o", tmpDocxPath,
}
pandoc := exec.Command(Conf.Export.PandocBin, args...)
util.CmdAttr(pandoc)
pandoc.Stdin = bytes.NewBufferString(dom)
pandoc.Stdin = bytes.NewBufferString(content)
output, err := pandoc.CombinedOutput()
if nil != err {
logging.LogErrorf("export docx failed: %s", gulu.Str.FromBytes(output))
@ -323,7 +327,14 @@ func ExportMarkdownHTML(id, savePath string, docx bool) (name, dom string) {
}
return ast.WalkContinue
})
dom = luteEngine.ProtylePreview(tree, luteEngine.RenderOptions)
if docx {
renderer := render.NewProtyleExportDocxRenderer(tree, luteEngine.RenderOptions)
output := renderer.Render()
dom = gulu.Str.FromBytes(output)
} else {
dom = luteEngine.ProtylePreview(tree, luteEngine.RenderOptions)
}
return
}
@ -964,6 +975,7 @@ func renderExportMdMathBlockContent(r *render.FormatRenderer, node *ast.Node, en
func renderExportMdInlineMathContent(r *render.FormatRenderer, node *ast.Node, entering bool) ast.WalkStatus {
if entering {
tokens := html.UnescapeHTML(node.Tokens)
tokens = gulu.Str.ToBytes("a" + gulu.Str.FromBytes(tokens) + "b")
r.Write(tokens)
}
return ast.WalkContinue