diff --git a/API.md b/API.md
index d812967e6..a059a4f38 100644
--- a/API.md
+++ b/API.md
@@ -44,6 +44,8 @@
* [List files](#List-files)
* [Export](#Export)
* [Export Markdown](#Export-Markdown)
+* [Conversion](#Conversion)
+ * [Pandoc](#Pandoc)
* [Notification](#Notification)
* [Push message](#Push-message)
* [Push error message](#Push-error-message)
@@ -710,9 +712,10 @@ View API token in Settings - About, request header: `Authorization: T
}
```
- * `id`: Block ID to move
- * `previousID`: The ID of the previous block, used to anchor the insertion position
- * `parentID`: The ID of the parent block, used to anchor the insertion position, `previousID` and `parentID` cannot be empty at the same time, if they exist at the same time, `previousID` will be used first
+ * `id`: Block ID to move
+ * `previousID`: The ID of the previous block, used to anchor the insertion position
+ * `parentID`: The ID of the parent block, used to anchor the insertion position, `previousID` and `parentID` cannot
+ be empty at the same time, if they exist at the same time, `previousID` will be used first
* Return value
```json
@@ -1014,6 +1017,41 @@ View API token in Settings - About, request header: `Authorization: T
* `hPath`: human-readable path
* `content`: Markdown content
+## Conversion
+
+### Pandoc
+
+* `/api/convert/pandoc`
+* Working directory
+ * Executing the pandoc command will set the working directory to `workspace/temp/convert/pandoc/`
+ * API [`Put file`](#put-file) can be used to write the file to be converted to this directory first
+ * Then call the API for conversion, and the converted file will also be written to this directory
+ * Finally, call the API [`Get file`](#get-file) to get the converted file
+ * Or call the API [Create a document with Markdown](#Create-a-document-with-Markdown)
+ * Or call the internal API `importStdMd` to import the converted folder directly
+* Parameters
+
+ ```json
+ {
+ "args": [
+ "--to", "markdown_strict-raw_html",
+ "foo.epub",
+ "-o", "foo.md"
+ ]
+ }
+ ```
+
+ * `args`: Pandoc command line parameters
+* Return value
+
+ ```json
+ {
+ "code": 0,
+ "msg": "",
+ "data": null
+ }
+ ```
+
## Notification
### Push message
diff --git a/API_zh_CN.md b/API_zh_CN.md
index 340f155ad..971356882 100644
--- a/API_zh_CN.md
+++ b/API_zh_CN.md
@@ -44,6 +44,8 @@
* [列出文件](#列出文件)
* [导出](#导出)
* [导出 Markdown 文本](#导出-markdown-文本)
+* [转换](#转换)
+ * [Pandoc](#Pandoc)
* [通知](#通知)
* [推送消息](#推送消息)
* [推送报错消息](#推送报错消息)
@@ -1008,6 +1010,41 @@
* `hPath`:人类可读的路径
* `content`:Markdown 内容
+## 转换
+
+### Pandoc
+
+* `/api/convert/pandoc`
+* 工作目录
+ * 执行调用 pandoc 命令时工作目录会被设置在 `工作空间/temp/convert/pandoc/` 下
+ * 可先通过 API [`写入文件`](#写入文件) 将待转换文件写入该目录
+ * 然后再调用该 API 进行转换,转换后的文件也会被写入该目录
+ * 最后调用 API [`获取文件`](#获取文件) 获取转换后的文件内容
+ * 或者调用 API [`通过 Markdown 创建文档`](#通过-markdown-创建文档)
+ * 或者调用内部 API `importStdMd` 将转换后的文件夹直接导入
+* 参数
+
+ ```json
+ {
+ "args": [
+ "--to", "markdown_strict-raw_html",
+ "foo.epub",
+ "-o", "foo.md"
+ ]
+ }
+ ```
+
+ * `args`:Pandoc 命令行参数
+* 返回值
+
+ ```json
+ {
+ "code": 0,
+ "msg": "",
+ "data": null
+ }
+ ```
+
## 通知
### 推送消息
diff --git a/kernel/api/pandoc.go b/kernel/api/pandoc.go
new file mode 100644
index 000000000..bd672b1bd
--- /dev/null
+++ b/kernel/api/pandoc.go
@@ -0,0 +1,49 @@
+// SiYuan - Build Your Eternal Digital Garden
+// Copyright (c) 2020-present, b3log.org
+//
+// This program is free software: you can redistribute it and/or modify
+// it under the terms of the GNU Affero General Public License as published by
+// the Free Software Foundation, either version 3 of the License, or
+// (at your option) any later version.
+//
+// This program is distributed in the hope that it will be useful,
+// but WITHOUT ANY WARRANTY; without even the implied warranty of
+// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+// GNU Affero General Public License for more details.
+//
+// You should have received a copy of the GNU Affero General Public License
+// along with this program. If not, see .
+
+package api
+
+import (
+ "net/http"
+
+ "github.com/88250/gulu"
+ "github.com/gin-gonic/gin"
+ "github.com/siyuan-note/siyuan/kernel/util"
+)
+
+func pandoc(c *gin.Context) {
+ ret := gulu.Ret.NewResult()
+ defer c.JSON(http.StatusOK, ret)
+
+ arg, ok := util.JsonArg(c, ret)
+ if !ok {
+ return
+ }
+
+ pandocArgs := arg["args"].([]interface{})
+ var args []string
+ for _, v := range pandocArgs {
+ args = append(args, v.(string))
+ }
+
+ err := util.ConvertPandoc(args...)
+ if nil != err {
+ ret.Code = -1
+ ret.Msg = err.Error()
+ return
+ }
+ return
+}
diff --git a/kernel/api/router.go b/kernel/api/router.go
index ffd934bd5..0a0f22cc3 100644
--- a/kernel/api/router.go
+++ b/kernel/api/router.go
@@ -259,6 +259,8 @@ func ServeAPI(ginServer *gin.Engine) {
ginServer.Handle("POST", "/api/import/importData", model.CheckAuth, model.CheckReadonly, importData)
ginServer.Handle("POST", "/api/import/importSY", model.CheckAuth, model.CheckReadonly, importSY)
+ ginServer.Handle("POST", "/api/convert/pandoc", model.CheckAuth, model.CheckReadonly, pandoc)
+
ginServer.Handle("POST", "/api/template/render", model.CheckAuth, renderTemplate)
ginServer.Handle("POST", "/api/template/docSaveAsTemplate", model.CheckAuth, model.CheckReadonly, docSaveAsTemplate)
ginServer.Handle("POST", "/api/template/renderSprig", model.CheckAuth, model.CheckReadonly, renderSprig)
diff --git a/kernel/util/pandoc.go b/kernel/util/pandoc.go
index c9d68cf4e..f46d6208d 100644
--- a/kernel/util/pandoc.go
+++ b/kernel/util/pandoc.go
@@ -18,6 +18,8 @@ package util
import (
"bytes"
+ "errors"
+ "os"
"os/exec"
"path/filepath"
"strings"
@@ -26,6 +28,27 @@ import (
"github.com/siyuan-note/logging"
)
+func ConvertPandoc(args ...string) (err error) {
+ if "" == PandocBinPath || ContainerStd != Container {
+ return errors.New("not found executable pandoc")
+ }
+
+ pandoc := exec.Command(PandocBinPath, args...)
+ gulu.CmdAttr(pandoc)
+ dir := filepath.Join(WorkspaceDir, "temp", "convert", "pandoc")
+ if err = os.MkdirAll(dir, 0755); nil != err {
+ logging.LogErrorf("mkdir [%s] failed: [%s]", dir, err)
+ return
+ }
+ pandoc.Dir = dir
+ output, err := pandoc.CombinedOutput()
+ if nil != err {
+ logging.LogErrorf("pandoc convert output [%s]", string(output))
+ return
+ }
+ return
+}
+
func Pandoc(from, to, o, content string) (ret string, err error) {
if "" == from || "" == to || "md" == to {
ret = content