2022-05-26 15:18:53 +08:00
|
|
|
|
// 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 <https://www.gnu.org/licenses/>.
|
|
|
|
|
|
|
|
|
|
package model
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"bytes"
|
|
|
|
|
"errors"
|
|
|
|
|
"fmt"
|
2023-01-02 21:28:50 +08:00
|
|
|
|
"github.com/imroc/req/v3"
|
|
|
|
|
"github.com/siyuan-note/httpclient"
|
|
|
|
|
"net/http"
|
2022-05-26 15:18:53 +08:00
|
|
|
|
"net/url"
|
|
|
|
|
"os"
|
|
|
|
|
"os/exec"
|
|
|
|
|
"path"
|
|
|
|
|
"path/filepath"
|
|
|
|
|
"sort"
|
|
|
|
|
"strconv"
|
|
|
|
|
"strings"
|
2022-12-17 11:53:11 +08:00
|
|
|
|
"time"
|
2022-05-26 15:18:53 +08:00
|
|
|
|
"unicode/utf8"
|
|
|
|
|
|
|
|
|
|
"github.com/88250/gulu"
|
|
|
|
|
"github.com/88250/lute/ast"
|
2022-09-14 00:35:13 +08:00
|
|
|
|
"github.com/88250/lute/editor"
|
2022-05-26 15:18:53 +08:00
|
|
|
|
"github.com/88250/lute/html"
|
|
|
|
|
"github.com/88250/lute/parse"
|
|
|
|
|
"github.com/88250/lute/render"
|
|
|
|
|
"github.com/88250/pdfcpu/pkg/api"
|
|
|
|
|
"github.com/88250/pdfcpu/pkg/pdfcpu"
|
|
|
|
|
"github.com/emirpasic/gods/sets/hashset"
|
|
|
|
|
"github.com/emirpasic/gods/stacks/linkedliststack"
|
2022-06-15 23:56:47 +08:00
|
|
|
|
"github.com/siyuan-note/filelock"
|
2022-07-17 12:22:32 +08:00
|
|
|
|
"github.com/siyuan-note/logging"
|
2022-05-26 15:18:53 +08:00
|
|
|
|
"github.com/siyuan-note/siyuan/kernel/sql"
|
|
|
|
|
"github.com/siyuan-note/siyuan/kernel/treenode"
|
|
|
|
|
"github.com/siyuan-note/siyuan/kernel/util"
|
|
|
|
|
)
|
|
|
|
|
|
2023-01-02 21:28:50 +08:00
|
|
|
|
func Export2Liandi(id string) (err error) {
|
|
|
|
|
tree, err := loadTreeByBlockID(id)
|
|
|
|
|
if nil != err {
|
|
|
|
|
logging.LogErrorf("load tree by block id [%s] failed: %s", id, err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-02 21:36:44 +08:00
|
|
|
|
sqlAssets := sql.QueryRootBlockAssets(id)
|
|
|
|
|
err = uploadCloud(sqlAssets)
|
|
|
|
|
|
2023-01-02 21:28:50 +08:00
|
|
|
|
// 判断帖子是否已经存在,存在则使用更新接口
|
|
|
|
|
foundArticle := false
|
|
|
|
|
articleId := tree.Root.IALAttr("liandiArticleId")
|
|
|
|
|
if "" != articleId {
|
|
|
|
|
request := httpclient.NewCloudRequest30s()
|
|
|
|
|
resp, getErr := request.
|
|
|
|
|
SetCookies(&http.Cookie{Name: "symphony", Value: Conf.User.UserToken}).
|
|
|
|
|
Get(util.LiandiServer + "/api/v2/article/update/" + articleId)
|
|
|
|
|
if nil != getErr {
|
|
|
|
|
logging.LogErrorf("get liandi article info failed: %s", getErr)
|
|
|
|
|
return getErr
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
switch resp.StatusCode {
|
|
|
|
|
case 200:
|
|
|
|
|
foundArticle = true
|
|
|
|
|
case 404:
|
|
|
|
|
foundArticle = false
|
|
|
|
|
default:
|
|
|
|
|
msg := fmt.Sprintf("get liandi article info failed [sc=%d]", resp.StatusCode)
|
|
|
|
|
err = errors.New(msg)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
apiURL := util.LiandiServer + "/api/v2/article"
|
|
|
|
|
if foundArticle {
|
|
|
|
|
apiURL += "/" + articleId
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
title := path.Base(tree.HPath)
|
|
|
|
|
tags := tree.Root.IALAttr("tags")
|
|
|
|
|
content := exportMarkdownContent0(tree)
|
2023-01-02 22:00:20 +08:00
|
|
|
|
var result = gulu.Ret.NewResult()
|
2023-01-02 21:28:50 +08:00
|
|
|
|
request := httpclient.NewCloudRequest30s()
|
|
|
|
|
request = request.
|
|
|
|
|
SetResult(result).
|
|
|
|
|
SetCookies(&http.Cookie{Name: "symphony", Value: Conf.User.UserToken}).
|
|
|
|
|
SetBody(map[string]interface{}{
|
|
|
|
|
"title": title,
|
|
|
|
|
"tags": tags,
|
|
|
|
|
"content": content})
|
|
|
|
|
var resp *req.Response
|
|
|
|
|
var sendErr error
|
|
|
|
|
if foundArticle {
|
|
|
|
|
resp, sendErr = request.Put(apiURL)
|
|
|
|
|
} else {
|
|
|
|
|
resp, sendErr = request.Post(apiURL)
|
|
|
|
|
}
|
|
|
|
|
if nil != sendErr {
|
|
|
|
|
logging.LogErrorf("send article to liandi failed: %s", err)
|
|
|
|
|
return err
|
|
|
|
|
}
|
|
|
|
|
if 200 != resp.StatusCode {
|
|
|
|
|
msg := fmt.Sprintf("send article to liandi failed [sc=%d]", resp.StatusCode)
|
|
|
|
|
logging.LogErrorf(msg)
|
|
|
|
|
return errors.New(msg)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if !foundArticle {
|
2023-01-02 22:00:20 +08:00
|
|
|
|
articleId = result.Data.(string)
|
2023-01-02 21:28:50 +08:00
|
|
|
|
tree.Root.SetIALAttr("liandiArticleId", articleId)
|
|
|
|
|
if err = writeJSONQueue(tree); nil != err {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
msg := fmt.Sprintf(Conf.Language(181), util.LiandiServer+"/article/"+articleId)
|
|
|
|
|
util.PushMsg(msg, 7000)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-27 01:19:52 +08:00
|
|
|
|
func ExportSystemLog() (zipPath string) {
|
|
|
|
|
exportFolder := filepath.Join(util.TempDir, "export", "system-log")
|
|
|
|
|
os.RemoveAll(exportFolder)
|
|
|
|
|
if err := os.MkdirAll(exportFolder, 0755); nil != err {
|
|
|
|
|
logging.LogErrorf("create export temp folder failed: %s", err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
appLog := filepath.Join(util.HomeDir, ".config", "siyuan", "app.log")
|
|
|
|
|
if gulu.File.IsExist(appLog) {
|
|
|
|
|
to := filepath.Join(exportFolder, "app.log")
|
|
|
|
|
if err := gulu.File.CopyFile(appLog, to); nil != err {
|
|
|
|
|
logging.LogErrorf("copy app log from [%s] to [%s] failed: %s", err, appLog, to)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
kernelLog := filepath.Join(util.TempDir, "siyuan.log")
|
|
|
|
|
if gulu.File.IsExist(kernelLog) {
|
|
|
|
|
to := filepath.Join(exportFolder, "siyuan.log")
|
|
|
|
|
if err := gulu.File.CopyFile(kernelLog, to); nil != err {
|
|
|
|
|
logging.LogErrorf("copy kernel log from [%s] to [%s] failed: %s", err, kernelLog, to)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
zipPath = exportFolder + ".zip"
|
|
|
|
|
zip, err := gulu.Zip.Create(zipPath)
|
|
|
|
|
if nil != err {
|
|
|
|
|
logging.LogErrorf("create export log zip [%s] failed: %s", exportFolder, err)
|
|
|
|
|
return ""
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if err = zip.AddDirectory("log", exportFolder); nil != err {
|
|
|
|
|
logging.LogErrorf("create export log zip [%s] failed: %s", exportFolder, err)
|
|
|
|
|
return ""
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if err = zip.Close(); nil != err {
|
|
|
|
|
logging.LogErrorf("close export log zip failed: %s", err)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
os.RemoveAll(exportFolder)
|
|
|
|
|
zipPath = "/export/" + url.PathEscape(filepath.Base(zipPath))
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-24 22:05:14 +08:00
|
|
|
|
func ExportNotebookSY(id string) (zipPath string) {
|
|
|
|
|
zipPath = exportBoxSYZip(id)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-26 15:18:53 +08:00
|
|
|
|
func ExportSY(id string) (name, zipPath string) {
|
|
|
|
|
block := treenode.GetBlockTree(id)
|
|
|
|
|
if nil == block {
|
2022-07-17 12:22:32 +08:00
|
|
|
|
logging.LogErrorf("not found block [%s]", id)
|
2022-05-26 15:18:53 +08:00
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
boxID := block.BoxID
|
|
|
|
|
box := Conf.Box(boxID)
|
|
|
|
|
baseFolderName := path.Base(block.HPath)
|
|
|
|
|
if "." == baseFolderName {
|
|
|
|
|
baseFolderName = path.Base(block.Path)
|
|
|
|
|
}
|
|
|
|
|
rootPath := block.Path
|
|
|
|
|
docPaths := []string{rootPath}
|
|
|
|
|
docFiles := box.ListFiles(strings.TrimSuffix(block.Path, ".sy"))
|
|
|
|
|
for _, docFile := range docFiles {
|
|
|
|
|
docPaths = append(docPaths, docFile.path)
|
|
|
|
|
}
|
|
|
|
|
zipPath = exportSYZip(boxID, path.Dir(rootPath), baseFolderName, docPaths)
|
|
|
|
|
name = strings.TrimSuffix(filepath.Base(block.Path), ".sy")
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-07 16:38:49 +08:00
|
|
|
|
func ExportDataInFolder(exportFolder string) (name string, err error) {
|
2022-05-26 15:18:53 +08:00
|
|
|
|
util.PushEndlessProgress(Conf.Language(65))
|
|
|
|
|
defer util.ClearPushProgress(100)
|
|
|
|
|
|
2022-12-31 16:30:24 +08:00
|
|
|
|
zipPath, err := ExportData()
|
2022-05-26 15:18:53 +08:00
|
|
|
|
if nil != err {
|
|
|
|
|
return
|
|
|
|
|
}
|
2022-12-07 16:38:49 +08:00
|
|
|
|
name = filepath.Base(zipPath)
|
2022-12-31 16:30:24 +08:00
|
|
|
|
targetZipPath := filepath.Join(exportFolder, name)
|
|
|
|
|
zipAbsPath := filepath.Join(util.TempDir, "export", name)
|
|
|
|
|
err = filelock.RoboCopy(zipAbsPath, targetZipPath)
|
|
|
|
|
if nil != err {
|
|
|
|
|
logging.LogErrorf("copy export zip from [%s] to [%s] failed: %s", zipAbsPath, targetZipPath, err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if removeErr := os.Remove(zipAbsPath); nil != removeErr {
|
|
|
|
|
logging.LogErrorf("remove export zip failed: %s", removeErr)
|
|
|
|
|
}
|
2022-05-26 15:18:53 +08:00
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-31 16:30:24 +08:00
|
|
|
|
func ExportData() (zipPath string, err error) {
|
2022-05-26 15:18:53 +08:00
|
|
|
|
util.PushEndlessProgress(Conf.Language(65))
|
|
|
|
|
defer util.ClearPushProgress(100)
|
|
|
|
|
|
2022-12-07 16:38:49 +08:00
|
|
|
|
exportFolder := filepath.Join(util.TempDir, "export", util.CurrentTimeSecondsStr())
|
2022-12-31 16:30:24 +08:00
|
|
|
|
zipPath, err = exportData(exportFolder)
|
2022-05-26 15:18:53 +08:00
|
|
|
|
if nil != err {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
zipPath = "/export/" + url.PathEscape(filepath.Base(zipPath))
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-07 16:38:49 +08:00
|
|
|
|
func exportData(exportFolder string) (zipPath string, err error) {
|
2022-12-31 16:30:24 +08:00
|
|
|
|
WaitForWritingFiles()
|
|
|
|
|
|
2022-05-26 15:18:53 +08:00
|
|
|
|
baseFolderName := "data-" + util.CurrentTimeSecondsStr()
|
|
|
|
|
if err = os.MkdirAll(exportFolder, 0755); nil != err {
|
2022-07-17 12:22:32 +08:00
|
|
|
|
logging.LogErrorf("create export temp folder failed: %s", err)
|
2022-05-26 15:18:53 +08:00
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
data := filepath.Join(util.WorkspaceDir, "data")
|
2022-09-29 21:52:01 +08:00
|
|
|
|
if err = filelock.RoboCopy(data, exportFolder); nil != err {
|
2022-07-17 12:22:32 +08:00
|
|
|
|
logging.LogErrorf("copy data dir from [%s] to [%s] failed: %s", data, baseFolderName, err)
|
2022-07-13 20:23:04 +08:00
|
|
|
|
err = errors.New(fmt.Sprintf(Conf.Language(14), formatErrorMsg(err)))
|
2022-05-26 15:18:53 +08:00
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-07 16:38:49 +08:00
|
|
|
|
zipPath = exportFolder + ".zip"
|
2022-05-26 15:18:53 +08:00
|
|
|
|
zip, err := gulu.Zip.Create(zipPath)
|
|
|
|
|
if nil != err {
|
2022-07-17 12:22:32 +08:00
|
|
|
|
logging.LogErrorf("create export data zip [%s] failed: %s", exportFolder, err)
|
2022-05-26 15:18:53 +08:00
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if err = zip.AddDirectory(baseFolderName, exportFolder); nil != err {
|
2022-07-17 12:22:32 +08:00
|
|
|
|
logging.LogErrorf("create export data zip [%s] failed: %s", exportFolder, err)
|
2022-05-26 15:18:53 +08:00
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if err = zip.Close(); nil != err {
|
2022-07-17 12:22:32 +08:00
|
|
|
|
logging.LogErrorf("close export data zip failed: %s", err)
|
2022-05-26 15:18:53 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
os.RemoveAll(exportFolder)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func Preview(id string) string {
|
|
|
|
|
tree, _ := loadTreeByBlockID(id)
|
2022-09-24 21:50:22 +08:00
|
|
|
|
tree = exportTree(tree, false, false, false)
|
2022-05-26 15:18:53 +08:00
|
|
|
|
luteEngine := NewLute()
|
|
|
|
|
luteEngine.SetFootnotes(true)
|
|
|
|
|
md := treenode.FormatNode(tree.Root, luteEngine)
|
|
|
|
|
tree = parse.Parse("", []byte(md), luteEngine.ParseOptions)
|
|
|
|
|
return luteEngine.ProtylePreview(tree, luteEngine.RenderOptions)
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-10 18:23:12 +08:00
|
|
|
|
func ExportDocx(id, savePath string, removeAssets, merge bool) (err error) {
|
2022-05-26 15:18:53 +08:00
|
|
|
|
if !util.IsValidPandocBin(Conf.Export.PandocBin) {
|
|
|
|
|
return errors.New(Conf.Language(115))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
tmpDir := filepath.Join(util.TempDir, "export", gulu.Rand.String(7))
|
2022-09-01 15:16:17 +08:00
|
|
|
|
if err = os.MkdirAll(tmpDir, 0755); nil != err {
|
|
|
|
|
return
|
|
|
|
|
}
|
2022-05-26 15:18:53 +08:00
|
|
|
|
defer os.Remove(tmpDir)
|
2022-12-10 18:23:12 +08:00
|
|
|
|
name, content := ExportMarkdownHTML(id, tmpDir, true, merge)
|
2022-09-01 15:16:17 +08:00
|
|
|
|
|
2022-05-26 15:18:53 +08:00
|
|
|
|
tmpDocxPath := filepath.Join(tmpDir, name+".docx")
|
|
|
|
|
args := []string{ // pandoc -f html --resource-path=请从这里开始 请从这里开始\index.html -o test.docx
|
2022-09-01 15:16:17 +08:00
|
|
|
|
"-f", "html+tex_math_dollars",
|
2022-05-26 15:18:53 +08:00
|
|
|
|
"--resource-path", tmpDir,
|
|
|
|
|
"-o", tmpDocxPath,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pandoc := exec.Command(Conf.Export.PandocBin, args...)
|
2022-09-29 21:52:01 +08:00
|
|
|
|
gulu.CmdAttr(pandoc)
|
2022-09-01 15:16:17 +08:00
|
|
|
|
pandoc.Stdin = bytes.NewBufferString(content)
|
2022-05-26 15:18:53 +08:00
|
|
|
|
output, err := pandoc.CombinedOutput()
|
|
|
|
|
if nil != err {
|
2022-07-17 12:22:32 +08:00
|
|
|
|
logging.LogErrorf("export docx failed: %s", gulu.Str.FromBytes(output))
|
2022-05-26 15:18:53 +08:00
|
|
|
|
msg := fmt.Sprintf(Conf.Language(14), gulu.Str.FromBytes(output))
|
|
|
|
|
return errors.New(msg)
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-19 22:52:00 +08:00
|
|
|
|
if err = gulu.File.Copy(tmpDocxPath, filepath.Join(savePath, name+".docx")); nil != err {
|
2022-07-17 12:22:32 +08:00
|
|
|
|
logging.LogErrorf("export docx failed: %s", err)
|
2022-05-26 15:18:53 +08:00
|
|
|
|
return errors.New(fmt.Sprintf(Conf.Language(14), err))
|
|
|
|
|
}
|
2022-07-20 10:43:02 +08:00
|
|
|
|
|
|
|
|
|
if tmpAssets := filepath.Join(tmpDir, "assets"); !removeAssets && gulu.File.IsDir(tmpAssets) {
|
2022-07-19 22:52:00 +08:00
|
|
|
|
if err = gulu.File.Copy(tmpAssets, filepath.Join(savePath, "assets")); nil != err {
|
2022-07-17 12:22:32 +08:00
|
|
|
|
logging.LogErrorf("export docx failed: %s", err)
|
2022-05-26 15:18:53 +08:00
|
|
|
|
return errors.New(fmt.Sprintf(Conf.Language(14), err))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-10 18:23:12 +08:00
|
|
|
|
func ExportMarkdownHTML(id, savePath string, docx, merge bool) (name, dom string) {
|
2022-05-26 15:18:53 +08:00
|
|
|
|
tree, _ := loadTreeByBlockID(id)
|
|
|
|
|
|
2022-12-10 18:23:12 +08:00
|
|
|
|
if merge {
|
|
|
|
|
var mergeErr error
|
|
|
|
|
tree, mergeErr = mergeSubDocs(tree)
|
|
|
|
|
if nil != mergeErr {
|
|
|
|
|
logging.LogErrorf("merge sub docs failed: %s", mergeErr)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-24 21:50:22 +08:00
|
|
|
|
tree = exportTree(tree, true, true, false)
|
2022-05-26 15:18:53 +08:00
|
|
|
|
name = path.Base(tree.HPath)
|
2022-08-11 23:52:50 +08:00
|
|
|
|
name = util.FilterFileName(name) // 导出 PDF、HTML 和 Word 时未移除不支持的文件名符号 https://github.com/siyuan-note/siyuan/issues/5614
|
2022-11-23 17:00:51 +08:00
|
|
|
|
savePath = strings.TrimSpace(savePath)
|
2022-05-26 15:18:53 +08:00
|
|
|
|
|
|
|
|
|
if err := os.MkdirAll(savePath, 0755); nil != err {
|
2022-07-17 12:22:32 +08:00
|
|
|
|
logging.LogErrorf("mkdir [%s] failed: %s", savePath, err)
|
2022-05-26 15:18:53 +08:00
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
assets := assetsLinkDestsInTree(tree)
|
|
|
|
|
for _, asset := range assets {
|
|
|
|
|
if strings.HasPrefix(asset, "assets/") {
|
|
|
|
|
srcAbsPath, err := GetAssetAbsPath(asset)
|
|
|
|
|
if nil != err {
|
2022-07-17 12:22:32 +08:00
|
|
|
|
logging.LogWarnf("resolve path of asset [%s] failed: %s", asset, err)
|
2022-05-26 15:18:53 +08:00
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
targetAbsPath := filepath.Join(savePath, asset)
|
|
|
|
|
if err = gulu.File.Copy(srcAbsPath, targetAbsPath); nil != err {
|
2022-07-17 12:22:32 +08:00
|
|
|
|
logging.LogWarnf("copy asset from [%s] to [%s] failed: %s", srcAbsPath, targetAbsPath, err)
|
2022-05-26 15:18:53 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
srcs := []string{"stage/build/export", "stage/build/fonts", "stage/protyle"}
|
|
|
|
|
for _, src := range srcs {
|
|
|
|
|
from := filepath.Join(util.WorkingDir, src)
|
|
|
|
|
to := filepath.Join(savePath, src)
|
|
|
|
|
if err := gulu.File.Copy(from, to); nil != err {
|
2022-07-17 12:22:32 +08:00
|
|
|
|
logging.LogWarnf("copy stage from [%s] to [%s] failed: %s", from, savePath, err)
|
2022-05-26 15:18:53 +08:00
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
theme := Conf.Appearance.ThemeLight
|
|
|
|
|
if 1 == Conf.Appearance.Mode {
|
|
|
|
|
theme = Conf.Appearance.ThemeDark
|
|
|
|
|
}
|
|
|
|
|
srcs = []string{"icons", "themes/" + theme}
|
|
|
|
|
for _, src := range srcs {
|
|
|
|
|
from := filepath.Join(util.AppearancePath, src)
|
|
|
|
|
to := filepath.Join(savePath, "appearance", src)
|
|
|
|
|
if err := gulu.File.Copy(from, to); nil != err {
|
2022-07-17 12:22:32 +08:00
|
|
|
|
logging.LogErrorf("copy appearance from [%s] to [%s] failed: %s", from, savePath, err)
|
2022-05-26 15:18:53 +08:00
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-28 23:47:55 +08:00
|
|
|
|
// 复制自定义表情图片
|
2022-07-29 23:37:04 +08:00
|
|
|
|
emojis := emojisInTree(tree)
|
|
|
|
|
for _, emoji := range emojis {
|
|
|
|
|
from := filepath.Join(util.DataDir, emoji)
|
|
|
|
|
to := filepath.Join(savePath, emoji)
|
|
|
|
|
if err := gulu.File.Copy(from, to); nil != err {
|
|
|
|
|
logging.LogErrorf("copy emojis from [%s] to [%s] failed: %s", from, savePath, err)
|
|
|
|
|
return
|
|
|
|
|
}
|
2022-07-28 23:47:55 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-05-26 15:18:53 +08:00
|
|
|
|
luteEngine := NewLute()
|
|
|
|
|
luteEngine.SetFootnotes(true)
|
|
|
|
|
md := treenode.FormatNode(tree.Root, luteEngine)
|
|
|
|
|
tree = parse.Parse("", []byte(md), luteEngine.ParseOptions)
|
|
|
|
|
if docx {
|
|
|
|
|
processIFrame(tree)
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-29 23:37:04 +08:00
|
|
|
|
// 自定义表情图片地址去掉开头的 /
|
|
|
|
|
ast.Walk(tree.Root, func(n *ast.Node, entering bool) ast.WalkStatus {
|
|
|
|
|
if !entering {
|
|
|
|
|
return ast.WalkContinue
|
|
|
|
|
}
|
|
|
|
|
if ast.NodeEmojiImg == n.Type {
|
|
|
|
|
n.Tokens = bytes.ReplaceAll(n.Tokens, []byte("src=\"/emojis"), []byte("src=\"emojis"))
|
|
|
|
|
}
|
|
|
|
|
return ast.WalkContinue
|
|
|
|
|
})
|
2022-09-01 15:16:17 +08:00
|
|
|
|
|
|
|
|
|
if docx {
|
|
|
|
|
renderer := render.NewProtyleExportDocxRenderer(tree, luteEngine.RenderOptions)
|
|
|
|
|
output := renderer.Render()
|
|
|
|
|
dom = gulu.Str.FromBytes(output)
|
|
|
|
|
} else {
|
|
|
|
|
dom = luteEngine.ProtylePreview(tree, luteEngine.RenderOptions)
|
|
|
|
|
}
|
2022-05-26 15:18:53 +08:00
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-26 11:42:41 +08:00
|
|
|
|
func ExportHTML(id, savePath string, pdf, image, keepFold, merge bool) (name, dom string) {
|
2022-11-23 17:00:51 +08:00
|
|
|
|
savePath = strings.TrimSpace(savePath)
|
2022-05-26 15:18:53 +08:00
|
|
|
|
tree, _ := loadTreeByBlockID(id)
|
2022-12-11 11:42:37 +08:00
|
|
|
|
|
|
|
|
|
if merge {
|
|
|
|
|
var mergeErr error
|
|
|
|
|
tree, mergeErr = mergeSubDocs(tree)
|
|
|
|
|
if nil != mergeErr {
|
|
|
|
|
logging.LogErrorf("merge sub docs failed: %s", mergeErr)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-26 15:18:53 +08:00
|
|
|
|
var headings []*ast.Node
|
|
|
|
|
if pdf { // 导出 PDF 需要标记目录书签
|
|
|
|
|
ast.Walk(tree.Root, func(n *ast.Node, entering bool) ast.WalkStatus {
|
|
|
|
|
if entering && ast.NodeHeading == n.Type && !n.ParentIs(ast.NodeBlockquote) {
|
|
|
|
|
headings = append(headings, n)
|
|
|
|
|
return ast.WalkSkipChildren
|
|
|
|
|
}
|
|
|
|
|
return ast.WalkContinue
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
for _, h := range headings {
|
|
|
|
|
link := &ast.Node{Type: ast.NodeLink}
|
|
|
|
|
link.AppendChild(&ast.Node{Type: ast.NodeOpenBracket})
|
|
|
|
|
link.AppendChild(&ast.Node{Type: ast.NodeText, Tokens: []byte(" ")})
|
|
|
|
|
link.AppendChild(&ast.Node{Type: ast.NodeCloseBracket})
|
|
|
|
|
link.AppendChild(&ast.Node{Type: ast.NodeOpenParen})
|
|
|
|
|
link.AppendChild(&ast.Node{Type: ast.NodeLinkDest, Tokens: []byte("pdf-outline://" + h.ID)})
|
|
|
|
|
link.AppendChild(&ast.Node{Type: ast.NodeCloseParen})
|
|
|
|
|
h.PrependChild(link)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-24 21:50:22 +08:00
|
|
|
|
tree = exportTree(tree, true, true, keepFold)
|
2022-09-08 11:33:44 +08:00
|
|
|
|
name = path.Base(tree.HPath)
|
2022-08-11 23:52:50 +08:00
|
|
|
|
name = util.FilterFileName(name) // 导出 PDF、HTML 和 Word 时未移除不支持的文件名符号 https://github.com/siyuan-note/siyuan/issues/5614
|
2022-05-26 15:18:53 +08:00
|
|
|
|
|
2022-09-08 10:20:06 +08:00
|
|
|
|
if "" != savePath {
|
|
|
|
|
if err := os.MkdirAll(savePath, 0755); nil != err {
|
|
|
|
|
logging.LogErrorf("mkdir [%s] failed: %s", savePath, err)
|
|
|
|
|
return
|
2022-05-26 15:18:53 +08:00
|
|
|
|
}
|
2022-09-08 10:20:06 +08:00
|
|
|
|
|
|
|
|
|
assets := assetsLinkDestsInTree(tree)
|
|
|
|
|
for _, asset := range assets {
|
2022-09-19 16:53:22 +08:00
|
|
|
|
if strings.Contains(asset, "?") {
|
|
|
|
|
asset = asset[:strings.LastIndex(asset, "?")]
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-08 10:20:06 +08:00
|
|
|
|
srcAbsPath, err := GetAssetAbsPath(asset)
|
|
|
|
|
if nil != err {
|
|
|
|
|
logging.LogWarnf("resolve path of asset [%s] failed: %s", asset, err)
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
targetAbsPath := filepath.Join(savePath, asset)
|
|
|
|
|
if err = gulu.File.Copy(srcAbsPath, targetAbsPath); nil != err {
|
|
|
|
|
logging.LogWarnf("copy asset from [%s] to [%s] failed: %s", srcAbsPath, targetAbsPath, err)
|
|
|
|
|
}
|
2022-05-26 15:18:53 +08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
luteEngine := NewLute()
|
2022-09-08 10:20:06 +08:00
|
|
|
|
if !pdf && "" != savePath { // 导出 HTML 需要复制静态资源
|
2022-05-26 15:18:53 +08:00
|
|
|
|
srcs := []string{"stage/build/export", "stage/build/fonts", "stage/protyle"}
|
|
|
|
|
for _, src := range srcs {
|
|
|
|
|
from := filepath.Join(util.WorkingDir, src)
|
|
|
|
|
to := filepath.Join(savePath, src)
|
|
|
|
|
if err := gulu.File.Copy(from, to); nil != err {
|
2022-07-17 12:22:32 +08:00
|
|
|
|
logging.LogErrorf("copy stage from [%s] to [%s] failed: %s", from, savePath, err)
|
2022-05-26 15:18:53 +08:00
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
theme := Conf.Appearance.ThemeLight
|
|
|
|
|
if 1 == Conf.Appearance.Mode {
|
|
|
|
|
theme = Conf.Appearance.ThemeDark
|
|
|
|
|
}
|
|
|
|
|
srcs = []string{"icons", "themes/" + theme}
|
|
|
|
|
for _, src := range srcs {
|
|
|
|
|
from := filepath.Join(util.AppearancePath, src)
|
|
|
|
|
to := filepath.Join(savePath, "appearance", src)
|
|
|
|
|
if err := gulu.File.Copy(from, to); nil != err {
|
2022-07-17 12:22:32 +08:00
|
|
|
|
logging.LogErrorf("copy appearance from [%s] to [%s] failed: %s", from, savePath, err)
|
2022-05-26 15:18:53 +08:00
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-07-28 23:47:55 +08:00
|
|
|
|
|
|
|
|
|
// 复制自定义表情图片
|
2022-07-29 23:37:04 +08:00
|
|
|
|
emojis := emojisInTree(tree)
|
|
|
|
|
for _, emoji := range emojis {
|
|
|
|
|
from := filepath.Join(util.DataDir, emoji)
|
|
|
|
|
to := filepath.Join(savePath, emoji)
|
|
|
|
|
if err := gulu.File.Copy(from, to); nil != err {
|
|
|
|
|
logging.LogErrorf("copy emojis from [%s] to [%s] failed: %s", from, savePath, err)
|
|
|
|
|
return
|
|
|
|
|
}
|
2022-07-28 23:47:55 +08:00
|
|
|
|
}
|
2022-12-26 11:42:41 +08:00
|
|
|
|
} else if pdf && !image { // 导出 PDF 需要将资源文件路径改为 HTTP 伺服
|
2022-10-27 09:47:03 +08:00
|
|
|
|
luteEngine.RenderOptions.LinkBase = "http://" + util.LocalHost + ":" + util.ServerPort + "/"
|
2022-05-26 15:18:53 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if pdf {
|
|
|
|
|
processIFrame(tree)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
luteEngine.SetFootnotes(true)
|
|
|
|
|
luteEngine.RenderOptions.ProtyleContenteditable = false
|
2022-09-17 16:32:05 +08:00
|
|
|
|
luteEngine.SetProtyleMarkNetImg(false)
|
2022-10-17 09:34:27 +08:00
|
|
|
|
// 不进行安全过滤,因为导出时需要保留所有的 HTML 标签
|
|
|
|
|
// 使用属性 `data-export-html` 导出时 `<style></style>` 标签丢失 https://github.com/siyuan-note/siyuan/issues/6228
|
|
|
|
|
luteEngine.SetSanitize(false)
|
2022-09-15 23:06:53 +08:00
|
|
|
|
renderer := render.NewProtyleExportRenderer(tree, luteEngine.RenderOptions)
|
2022-05-26 15:18:53 +08:00
|
|
|
|
dom = gulu.Str.FromBytes(renderer.Render())
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func processIFrame(tree *parse.Tree) {
|
|
|
|
|
// 导出 PDF/Word 时 IFrame 块使用超链接 https://github.com/siyuan-note/siyuan/issues/4035
|
|
|
|
|
var unlinks []*ast.Node
|
|
|
|
|
ast.Walk(tree.Root, func(n *ast.Node, entering bool) ast.WalkStatus {
|
|
|
|
|
if !entering {
|
|
|
|
|
return ast.WalkContinue
|
|
|
|
|
}
|
|
|
|
|
if ast.NodeIFrame == n.Type {
|
|
|
|
|
index := bytes.Index(n.Tokens, []byte("src=\""))
|
|
|
|
|
if 0 > index {
|
|
|
|
|
n.InsertBefore(&ast.Node{Type: ast.NodeText, Tokens: n.Tokens})
|
|
|
|
|
} else {
|
|
|
|
|
src := n.Tokens[index+len("src=\""):]
|
|
|
|
|
src = src[:bytes.Index(src, []byte("\""))]
|
|
|
|
|
src = html.UnescapeHTML(src)
|
|
|
|
|
link := &ast.Node{Type: ast.NodeLink}
|
|
|
|
|
link.AppendChild(&ast.Node{Type: ast.NodeOpenBracket})
|
|
|
|
|
link.AppendChild(&ast.Node{Type: ast.NodeText, Tokens: src})
|
|
|
|
|
link.AppendChild(&ast.Node{Type: ast.NodeCloseBracket})
|
|
|
|
|
link.AppendChild(&ast.Node{Type: ast.NodeOpenParen})
|
|
|
|
|
link.AppendChild(&ast.Node{Type: ast.NodeLinkDest, Tokens: src})
|
|
|
|
|
link.AppendChild(&ast.Node{Type: ast.NodeCloseParen})
|
|
|
|
|
n.InsertBefore(link)
|
|
|
|
|
}
|
|
|
|
|
unlinks = append(unlinks, n)
|
|
|
|
|
}
|
|
|
|
|
return ast.WalkContinue
|
|
|
|
|
})
|
|
|
|
|
for _, n := range unlinks {
|
|
|
|
|
n.Unlink()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-11 12:17:43 +08:00
|
|
|
|
func AddPDFOutline(id, p string, merge bool) (err error) {
|
2022-05-26 15:18:53 +08:00
|
|
|
|
inFile := p
|
|
|
|
|
links, err := api.ListToCLinks(inFile)
|
|
|
|
|
if nil != err {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
sort.Slice(links, func(i, j int) bool {
|
|
|
|
|
return links[i].Page < links[j].Page
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
bms := map[string]*pdfcpu.Bookmark{}
|
2022-08-30 16:53:21 +08:00
|
|
|
|
footnotes := map[string]*pdfcpu.Bookmark{}
|
2022-05-26 15:18:53 +08:00
|
|
|
|
for _, link := range links {
|
|
|
|
|
linkID := link.URI[strings.LastIndex(link.URI, "/")+1:]
|
2022-12-11 11:51:04 +08:00
|
|
|
|
b := sql.GetBlock(linkID)
|
|
|
|
|
if nil == b {
|
|
|
|
|
logging.LogWarnf("pdf outline block [%s] not found", linkID)
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
title := b.Content
|
2022-05-26 15:18:53 +08:00
|
|
|
|
title, _ = url.QueryUnescape(title)
|
|
|
|
|
bm := &pdfcpu.Bookmark{
|
|
|
|
|
Title: title,
|
|
|
|
|
PageFrom: link.Page,
|
|
|
|
|
AbsPos: link.Rect.UR.Y,
|
|
|
|
|
}
|
|
|
|
|
bms[linkID] = bm
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-30 16:53:21 +08:00
|
|
|
|
if 1 > len(bms) && 1 > len(footnotes) {
|
2022-05-26 15:18:53 +08:00
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
tree, _ := loadTreeByBlockID(id)
|
|
|
|
|
if nil == tree {
|
|
|
|
|
return
|
|
|
|
|
}
|
2022-12-11 12:17:43 +08:00
|
|
|
|
if merge {
|
|
|
|
|
var mergeErr error
|
|
|
|
|
tree, mergeErr = mergeSubDocs(tree)
|
|
|
|
|
if nil != mergeErr {
|
|
|
|
|
logging.LogErrorf("merge sub docs failed: %s", mergeErr)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-05-26 15:18:53 +08:00
|
|
|
|
|
|
|
|
|
var headings []*ast.Node
|
|
|
|
|
ast.Walk(tree.Root, func(n *ast.Node, entering bool) ast.WalkStatus {
|
|
|
|
|
if entering && ast.NodeHeading == n.Type && !n.ParentIs(ast.NodeBlockquote) {
|
|
|
|
|
headings = append(headings, n)
|
|
|
|
|
return ast.WalkSkipChildren
|
|
|
|
|
}
|
|
|
|
|
return ast.WalkContinue
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
var topBms []*pdfcpu.Bookmark
|
|
|
|
|
stack := linkedliststack.New()
|
|
|
|
|
for _, h := range headings {
|
|
|
|
|
L:
|
|
|
|
|
for ; ; stack.Pop() {
|
|
|
|
|
cur, ok := stack.Peek()
|
|
|
|
|
if !ok {
|
|
|
|
|
bm := bms[h.ID]
|
2022-09-19 16:53:22 +08:00
|
|
|
|
if nil == bm {
|
|
|
|
|
break L
|
|
|
|
|
}
|
2022-05-26 15:18:53 +08:00
|
|
|
|
bm.Level = h.HeadingLevel
|
|
|
|
|
stack.Push(bm)
|
|
|
|
|
topBms = append(topBms, bm)
|
|
|
|
|
break L
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
tip := cur.(*pdfcpu.Bookmark)
|
|
|
|
|
if tip.Level < h.HeadingLevel {
|
|
|
|
|
bm := bms[h.ID]
|
|
|
|
|
bm.Level = h.HeadingLevel
|
|
|
|
|
bm.Parent = tip
|
|
|
|
|
tip.Children = append(tip.Children, bm)
|
|
|
|
|
stack.Push(bm)
|
|
|
|
|
break L
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-30 16:53:21 +08:00
|
|
|
|
//if 4 == Conf.Export.BlockRefMode { // 块引转脚注
|
|
|
|
|
// var footnotesBms []*pdfcpu.Bookmark
|
|
|
|
|
// for _, bm := range footnotes {
|
|
|
|
|
// footnotesBms = append(footnotesBms, bm)
|
|
|
|
|
// }
|
|
|
|
|
// sort.Slice(footnotesBms, func(i, j int) bool { return footnotesBms[i].PageFrom < footnotesBms[j].PageFrom })
|
|
|
|
|
// for _, bm := range footnotesBms {
|
|
|
|
|
// topBms = append(topBms, bm)
|
|
|
|
|
// }
|
|
|
|
|
//}
|
|
|
|
|
|
2022-05-26 15:18:53 +08:00
|
|
|
|
outFile := inFile + ".tmp"
|
|
|
|
|
err = api.AddBookmarksFile(inFile, outFile, topBms, nil)
|
|
|
|
|
if nil != err {
|
2022-07-17 12:22:32 +08:00
|
|
|
|
logging.LogErrorf("add bookmark failed: %s", err)
|
2022-05-26 15:18:53 +08:00
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
err = os.Rename(outFile, inFile)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func CopyStdMarkdown(id string) string {
|
|
|
|
|
tree, _ := loadTreeByBlockID(id)
|
2022-09-24 21:50:22 +08:00
|
|
|
|
tree = exportTree(tree, false, false, false)
|
2022-05-26 15:18:53 +08:00
|
|
|
|
luteEngine := NewLute()
|
|
|
|
|
luteEngine.SetFootnotes(true)
|
|
|
|
|
luteEngine.SetKramdownIAL(false)
|
|
|
|
|
if IsSubscriber() {
|
|
|
|
|
// 订阅用户使用云端图床服务
|
|
|
|
|
luteEngine.RenderOptions.LinkBase = "https://assets.b3logfile.com/siyuan/" + Conf.User.UserId + "/"
|
|
|
|
|
}
|
2022-09-19 09:48:25 +08:00
|
|
|
|
return treenode.ExportNodeStdMd(tree.Root, luteEngine)
|
2022-05-26 15:18:53 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func ExportMarkdown(id string) (name, zipPath string) {
|
|
|
|
|
block := treenode.GetBlockTree(id)
|
|
|
|
|
if nil == block {
|
2022-07-17 12:22:32 +08:00
|
|
|
|
logging.LogErrorf("not found block [%s]", id)
|
2022-05-26 15:18:53 +08:00
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
boxID := block.BoxID
|
|
|
|
|
box := Conf.Box(boxID)
|
|
|
|
|
baseFolderName := path.Base(block.HPath)
|
|
|
|
|
if "." == baseFolderName {
|
|
|
|
|
baseFolderName = path.Base(block.Path)
|
|
|
|
|
}
|
|
|
|
|
docPaths := []string{block.Path}
|
|
|
|
|
docFiles := box.ListFiles(strings.TrimSuffix(block.Path, ".sy"))
|
|
|
|
|
for _, docFile := range docFiles {
|
|
|
|
|
docPaths = append(docPaths, docFile.path)
|
|
|
|
|
}
|
|
|
|
|
zipPath = exportMarkdownZip(boxID, baseFolderName, docPaths)
|
|
|
|
|
name = strings.TrimSuffix(filepath.Base(block.Path), ".sy")
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func BatchExportMarkdown(boxID, folderPath string) (zipPath string) {
|
|
|
|
|
box := Conf.Box(boxID)
|
|
|
|
|
|
|
|
|
|
var baseFolderName string
|
|
|
|
|
if "/" == folderPath {
|
|
|
|
|
baseFolderName = box.Name
|
|
|
|
|
} else {
|
|
|
|
|
block := treenode.GetBlockTreeRootByHPath(box.ID, folderPath)
|
|
|
|
|
if nil == block {
|
2022-07-17 12:22:32 +08:00
|
|
|
|
logging.LogErrorf("not found block")
|
2022-05-26 15:18:53 +08:00
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
baseFolderName = path.Base(block.HPath)
|
|
|
|
|
}
|
|
|
|
|
if "" == baseFolderName {
|
|
|
|
|
baseFolderName = "Untitled"
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
docFiles := box.ListFiles(folderPath)
|
|
|
|
|
var docPaths []string
|
|
|
|
|
for _, docFile := range docFiles {
|
|
|
|
|
docPaths = append(docPaths, docFile.path)
|
|
|
|
|
}
|
|
|
|
|
zipPath = exportMarkdownZip(boxID, baseFolderName, docPaths)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func exportMarkdownZip(boxID, baseFolderName string, docPaths []string) (zipPath string) {
|
|
|
|
|
dir, name := path.Split(baseFolderName)
|
|
|
|
|
name = util.FilterFileName(name)
|
|
|
|
|
if strings.HasSuffix(name, "..") {
|
|
|
|
|
// 文档标题以 `..` 结尾时无法导出 Markdown https://github.com/siyuan-note/siyuan/issues/4698
|
|
|
|
|
// 似乎是 os.MkdirAll 的 bug,以 .. 结尾的路径无法创建,所以这里加上 _ 结尾
|
|
|
|
|
name += "_"
|
|
|
|
|
}
|
|
|
|
|
baseFolderName = path.Join(dir, name)
|
|
|
|
|
box := Conf.Box(boxID)
|
|
|
|
|
|
|
|
|
|
exportFolder := filepath.Join(util.TempDir, "export", baseFolderName)
|
|
|
|
|
if err := os.MkdirAll(exportFolder, 0755); nil != err {
|
2022-07-17 12:22:32 +08:00
|
|
|
|
logging.LogErrorf("create export temp folder failed: %s", err)
|
2022-05-26 15:18:53 +08:00
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
luteEngine := util.NewLute()
|
|
|
|
|
for _, p := range docPaths {
|
|
|
|
|
docIAL := box.docIAL(p)
|
|
|
|
|
if nil == docIAL {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
id := docIAL["id"]
|
|
|
|
|
hPath, md := exportMarkdownContent(id)
|
2022-12-17 11:53:11 +08:00
|
|
|
|
md = yfm(docIAL) + md
|
2022-05-26 15:18:53 +08:00
|
|
|
|
dir, name = path.Split(hPath)
|
|
|
|
|
dir = util.FilterFilePath(dir) // 导出文档时未移除不支持的文件名符号 https://github.com/siyuan-note/siyuan/issues/4590
|
|
|
|
|
name = util.FilterFileName(name)
|
|
|
|
|
hPath = path.Join(dir, name)
|
|
|
|
|
p = hPath + ".md"
|
|
|
|
|
writePath := filepath.Join(exportFolder, p)
|
|
|
|
|
if gulu.File.IsExist(writePath) {
|
|
|
|
|
// 重名文档加 ID
|
|
|
|
|
p = hPath + "-" + id + ".md"
|
|
|
|
|
writePath = filepath.Join(exportFolder, p)
|
|
|
|
|
}
|
|
|
|
|
writeFolder := filepath.Dir(writePath)
|
|
|
|
|
if err := os.MkdirAll(writeFolder, 0755); nil != err {
|
2022-07-17 12:22:32 +08:00
|
|
|
|
logging.LogErrorf("create export temp folder [%s] failed: %s", writeFolder, err)
|
2022-05-26 15:18:53 +08:00
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
if err := gulu.File.WriteFileSafer(writePath, gulu.Str.ToBytes(md), 0644); nil != err {
|
2022-07-17 12:22:32 +08:00
|
|
|
|
logging.LogErrorf("write export markdown file [%s] failed: %s", writePath, err)
|
2022-05-26 15:18:53 +08:00
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 解析导出后的标准 Markdown,汇总 assets
|
|
|
|
|
tree := parse.Parse("", gulu.Str.ToBytes(md), luteEngine.ParseOptions)
|
|
|
|
|
var assets []string
|
|
|
|
|
assets = append(assets, assetsLinkDestsInTree(tree)...)
|
|
|
|
|
for _, asset := range assets {
|
|
|
|
|
asset = string(html.DecodeDestination([]byte(asset)))
|
|
|
|
|
if strings.Contains(asset, "?") {
|
|
|
|
|
asset = asset[:strings.LastIndex(asset, "?")]
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
srcPath, err := GetAssetAbsPath(asset)
|
|
|
|
|
if nil != err {
|
2022-07-17 12:22:32 +08:00
|
|
|
|
logging.LogWarnf("get asset [%s] abs path failed: %s", asset, err)
|
2022-05-26 15:18:53 +08:00
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
destPath := filepath.Join(writeFolder, asset)
|
|
|
|
|
if gulu.File.IsDir(srcPath) {
|
|
|
|
|
err = gulu.File.Copy(srcPath, destPath)
|
|
|
|
|
} else {
|
|
|
|
|
err = gulu.File.CopyFile(srcPath, destPath)
|
|
|
|
|
}
|
|
|
|
|
if nil != err {
|
2022-07-17 12:22:32 +08:00
|
|
|
|
logging.LogErrorf("copy asset from [%s] to [%s] failed: %s", srcPath, destPath, err)
|
2022-05-26 15:18:53 +08:00
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
zipPath = exportFolder + ".zip"
|
|
|
|
|
zip, err := gulu.Zip.Create(zipPath)
|
|
|
|
|
if nil != err {
|
2022-07-17 12:22:32 +08:00
|
|
|
|
logging.LogErrorf("create export markdown zip [%s] failed: %s", exportFolder, err)
|
2022-05-26 15:18:53 +08:00
|
|
|
|
return ""
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-15 10:15:18 +08:00
|
|
|
|
// 导出 Markdown zip 包内不带文件夹 https://github.com/siyuan-note/siyuan/issues/6869
|
|
|
|
|
entries, err := os.ReadDir(exportFolder)
|
|
|
|
|
if nil != err {
|
|
|
|
|
logging.LogErrorf("read export markdown folder [%s] failed: %s", exportFolder, err)
|
2022-05-26 15:18:53 +08:00
|
|
|
|
return ""
|
|
|
|
|
}
|
2022-12-15 10:15:18 +08:00
|
|
|
|
for _, entry := range entries {
|
|
|
|
|
entryPath := filepath.Join(exportFolder, entry.Name())
|
|
|
|
|
if gulu.File.IsDir(entryPath) {
|
|
|
|
|
err = zip.AddDirectory(entry.Name(), entryPath)
|
|
|
|
|
} else {
|
|
|
|
|
err = zip.AddEntry(entry.Name(), entryPath)
|
|
|
|
|
}
|
|
|
|
|
if nil != err {
|
|
|
|
|
logging.LogErrorf("add entry [%s] to zip failed: %s", entry.Name(), err)
|
|
|
|
|
return ""
|
|
|
|
|
}
|
|
|
|
|
}
|
2022-05-26 15:18:53 +08:00
|
|
|
|
|
|
|
|
|
if err = zip.Close(); nil != err {
|
2022-07-17 12:22:32 +08:00
|
|
|
|
logging.LogErrorf("close export markdown zip failed: %s", err)
|
2022-05-26 15:18:53 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
os.RemoveAll(exportFolder)
|
|
|
|
|
zipPath = "/export/" + url.PathEscape(filepath.Base(zipPath))
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2022-12-17 11:53:11 +08:00
|
|
|
|
func yfm(docIAL map[string]string) string {
|
|
|
|
|
// 导出 Markdown 文件时开头附上一些元数据 https://github.com/siyuan-note/siyuan/issues/6880
|
|
|
|
|
|
|
|
|
|
buf := bytes.Buffer{}
|
|
|
|
|
buf.WriteString("---\n")
|
|
|
|
|
var title, created, updated, tags string
|
|
|
|
|
for k, v := range docIAL {
|
|
|
|
|
if "id" == k {
|
|
|
|
|
createdTime, parseErr := time.Parse("20060102150405", util.TimeFromID(v))
|
|
|
|
|
if nil == parseErr {
|
|
|
|
|
created = createdTime.Format(time.RFC3339)
|
|
|
|
|
}
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
if "title" == k {
|
|
|
|
|
title = v
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
if "updated" == k {
|
|
|
|
|
updatedTime, parseErr := time.Parse("20060102150405", v)
|
|
|
|
|
if nil == parseErr {
|
|
|
|
|
updated = updatedTime.Format(time.RFC3339)
|
|
|
|
|
}
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
if "tags" == k {
|
|
|
|
|
tags = v
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if "" != title {
|
|
|
|
|
buf.WriteString("title: ")
|
|
|
|
|
buf.WriteString(title)
|
|
|
|
|
buf.WriteString("\n")
|
|
|
|
|
}
|
|
|
|
|
if "" == updated {
|
|
|
|
|
updated = time.Now().Format(time.RFC3339)
|
|
|
|
|
}
|
|
|
|
|
if "" == created {
|
|
|
|
|
created = updated
|
|
|
|
|
}
|
2022-12-22 10:24:28 +08:00
|
|
|
|
buf.WriteString("date: ")
|
2022-12-17 11:53:11 +08:00
|
|
|
|
buf.WriteString(created)
|
|
|
|
|
buf.WriteString("\n")
|
2022-12-22 10:24:28 +08:00
|
|
|
|
buf.WriteString("lastmod: ")
|
2022-12-17 11:53:11 +08:00
|
|
|
|
buf.WriteString(updated)
|
|
|
|
|
buf.WriteString("\n")
|
|
|
|
|
if "" != tags {
|
|
|
|
|
buf.WriteString("tags: [")
|
|
|
|
|
buf.WriteString(tags)
|
|
|
|
|
buf.WriteString("]\n")
|
|
|
|
|
}
|
|
|
|
|
buf.WriteString("---\n\n")
|
|
|
|
|
return buf.String()
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-24 22:05:14 +08:00
|
|
|
|
func exportBoxSYZip(boxID string) (zipPath string) {
|
|
|
|
|
box := Conf.Box(boxID)
|
|
|
|
|
if nil == box {
|
|
|
|
|
logging.LogErrorf("not found box [%s]", boxID)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
baseFolderName := box.Name
|
|
|
|
|
|
|
|
|
|
var docPaths []string
|
|
|
|
|
docFiles := box.ListFiles("/")
|
|
|
|
|
for _, docFile := range docFiles {
|
|
|
|
|
docPaths = append(docPaths, docFile.path)
|
|
|
|
|
}
|
|
|
|
|
zipPath = exportSYZip(boxID, "/", baseFolderName, docPaths)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-26 15:18:53 +08:00
|
|
|
|
func exportSYZip(boxID, rootDirPath, baseFolderName string, docPaths []string) (zipPath string) {
|
|
|
|
|
dir, name := path.Split(baseFolderName)
|
|
|
|
|
name = util.FilterFileName(name)
|
|
|
|
|
if strings.HasSuffix(name, "..") {
|
|
|
|
|
// 文档标题以 `..` 结尾时无法导出 Markdown https://github.com/siyuan-note/siyuan/issues/4698
|
|
|
|
|
// 似乎是 os.MkdirAll 的 bug,以 .. 结尾的路径无法创建,所以这里加上 _ 结尾
|
|
|
|
|
name += "_"
|
|
|
|
|
}
|
|
|
|
|
baseFolderName = path.Join(dir, name)
|
|
|
|
|
box := Conf.Box(boxID)
|
|
|
|
|
|
|
|
|
|
exportFolder := filepath.Join(util.TempDir, "export", baseFolderName)
|
|
|
|
|
if err := os.MkdirAll(exportFolder, 0755); nil != err {
|
2022-07-17 12:22:32 +08:00
|
|
|
|
logging.LogErrorf("create export temp folder failed: %s", err)
|
2022-05-26 15:18:53 +08:00
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
trees := map[string]*parse.Tree{}
|
|
|
|
|
refTrees := map[string]*parse.Tree{}
|
|
|
|
|
for _, p := range docPaths {
|
|
|
|
|
docIAL := box.docIAL(p)
|
|
|
|
|
if nil == docIAL {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
id := docIAL["id"]
|
|
|
|
|
tree, err := loadTreeByBlockID(id)
|
|
|
|
|
if nil != err {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
trees[tree.ID] = tree
|
|
|
|
|
}
|
|
|
|
|
for _, tree := range trees {
|
|
|
|
|
refs := exportRefTrees(tree)
|
|
|
|
|
for refTreeID, refTree := range refs {
|
|
|
|
|
if nil == trees[refTreeID] {
|
|
|
|
|
refTrees[refTreeID] = refTree
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 按文件夹结构复制选择的树
|
|
|
|
|
for _, tree := range trees {
|
|
|
|
|
readPath := filepath.Join(util.DataDir, tree.Box, tree.Path)
|
2022-09-29 21:52:01 +08:00
|
|
|
|
data, readErr := filelock.ReadFile(readPath)
|
2022-05-26 15:18:53 +08:00
|
|
|
|
if nil != readErr {
|
2022-07-17 12:22:32 +08:00
|
|
|
|
logging.LogErrorf("read file [%s] failed: %s", readPath, readErr)
|
2022-05-26 15:18:53 +08:00
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
writePath := strings.TrimPrefix(tree.Path, rootDirPath)
|
|
|
|
|
writePath = filepath.Join(exportFolder, writePath)
|
|
|
|
|
writeFolder := filepath.Dir(writePath)
|
|
|
|
|
if mkdirErr := os.MkdirAll(writeFolder, 0755); nil != mkdirErr {
|
2022-07-17 12:22:32 +08:00
|
|
|
|
logging.LogErrorf("create export temp folder [%s] failed: %s", writeFolder, mkdirErr)
|
2022-05-26 15:18:53 +08:00
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
if writeErr := os.WriteFile(writePath, data, 0644); nil != writeErr {
|
2022-07-17 12:22:32 +08:00
|
|
|
|
logging.LogErrorf("write export file [%s] failed: %s", writePath, writeErr)
|
2022-05-26 15:18:53 +08:00
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 引用树放在导出文件夹根路径下
|
|
|
|
|
for treeID, tree := range refTrees {
|
|
|
|
|
readPath := filepath.Join(util.DataDir, tree.Box, tree.Path)
|
2022-09-29 21:52:01 +08:00
|
|
|
|
data, readErr := filelock.ReadFile(readPath)
|
2022-05-26 15:18:53 +08:00
|
|
|
|
if nil != readErr {
|
2022-07-17 12:22:32 +08:00
|
|
|
|
logging.LogErrorf("read file [%s] failed: %s", readPath, readErr)
|
2022-05-26 15:18:53 +08:00
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
writePath := strings.TrimPrefix(tree.Path, rootDirPath)
|
|
|
|
|
writePath = filepath.Join(exportFolder, treeID+".sy")
|
|
|
|
|
if writeErr := os.WriteFile(writePath, data, 0644); nil != writeErr {
|
2022-07-17 12:22:32 +08:00
|
|
|
|
logging.LogErrorf("write export file [%s] failed: %s", writePath, writeErr)
|
2022-05-26 15:18:53 +08:00
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 将引用树合并到选择树中,以便后面一次性导出资源文件
|
|
|
|
|
for treeID, tree := range refTrees {
|
|
|
|
|
trees[treeID] = tree
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 导出引用的资源文件
|
|
|
|
|
copiedAssets := hashset.New()
|
|
|
|
|
for _, tree := range trees {
|
|
|
|
|
var assets []string
|
|
|
|
|
assets = append(assets, assetsLinkDestsInTree(tree)...)
|
|
|
|
|
for _, asset := range assets {
|
|
|
|
|
asset = string(html.DecodeDestination([]byte(asset)))
|
|
|
|
|
if strings.Contains(asset, "?") {
|
|
|
|
|
asset = asset[:strings.LastIndex(asset, "?")]
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if copiedAssets.Contains(asset) {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
srcPath, assetErr := GetAssetAbsPath(asset)
|
|
|
|
|
if nil != assetErr {
|
2022-07-17 12:22:32 +08:00
|
|
|
|
logging.LogWarnf("get asset [%s] abs path failed: %s", asset, assetErr)
|
2022-05-26 15:18:53 +08:00
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
destPath := filepath.Join(exportFolder, asset)
|
|
|
|
|
if gulu.File.IsDir(srcPath) {
|
|
|
|
|
assetErr = gulu.File.Copy(srcPath, destPath)
|
|
|
|
|
} else {
|
|
|
|
|
assetErr = gulu.File.CopyFile(srcPath, destPath)
|
|
|
|
|
}
|
|
|
|
|
if nil != assetErr {
|
2022-07-17 12:22:32 +08:00
|
|
|
|
logging.LogErrorf("copy asset from [%s] to [%s] failed: %s", srcPath, destPath, assetErr)
|
2022-05-26 15:18:53 +08:00
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
copiedAssets.Add(asset)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-24 09:34:32 +08:00
|
|
|
|
// 导出自定义排序
|
|
|
|
|
sortPath := filepath.Join(util.DataDir, box.ID, ".siyuan", "sort.json")
|
|
|
|
|
fullSortIDs := map[string]int{}
|
|
|
|
|
sortIDs := map[string]int{}
|
|
|
|
|
var sortData []byte
|
|
|
|
|
var sortErr error
|
|
|
|
|
if gulu.File.IsExist(sortPath) {
|
2022-09-29 21:52:01 +08:00
|
|
|
|
sortData, sortErr = filelock.ReadFile(sortPath)
|
2022-09-24 09:34:32 +08:00
|
|
|
|
if nil != sortErr {
|
|
|
|
|
logging.LogErrorf("read sort conf failed: %s", sortErr)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if sortErr = gulu.JSON.UnmarshalJSON(sortData, &fullSortIDs); nil != sortErr {
|
|
|
|
|
logging.LogErrorf("unmarshal sort conf failed: %s", sortErr)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if 0 < len(fullSortIDs) {
|
|
|
|
|
for _, tree := range trees {
|
|
|
|
|
if v, ok := fullSortIDs[tree.ID]; ok {
|
|
|
|
|
sortIDs[tree.ID] = v
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if 0 < len(sortIDs) {
|
|
|
|
|
sortData, sortErr = gulu.JSON.MarshalJSON(sortIDs)
|
|
|
|
|
if nil != sortErr {
|
|
|
|
|
logging.LogErrorf("marshal sort conf failed: %s", sortErr)
|
|
|
|
|
}
|
|
|
|
|
if 0 < len(sortData) {
|
|
|
|
|
confDir := filepath.Join(exportFolder, ".siyuan")
|
|
|
|
|
if mkdirErr := os.MkdirAll(confDir, 0755); nil != mkdirErr {
|
|
|
|
|
logging.LogErrorf("create export conf folder [%s] failed: %s", confDir, mkdirErr)
|
|
|
|
|
} else {
|
|
|
|
|
sortPath = filepath.Join(confDir, "sort.json")
|
|
|
|
|
if writeErr := os.WriteFile(sortPath, sortData, 0644); nil != writeErr {
|
|
|
|
|
logging.LogErrorf("write sort conf failed: %s", writeErr)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-26 15:18:53 +08:00
|
|
|
|
zipPath = exportFolder + ".sy.zip"
|
|
|
|
|
zip, err := gulu.Zip.Create(zipPath)
|
|
|
|
|
if nil != err {
|
2022-07-17 12:22:32 +08:00
|
|
|
|
logging.LogErrorf("create export markdown zip [%s] failed: %s", exportFolder, err)
|
2022-05-26 15:18:53 +08:00
|
|
|
|
return ""
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if err = zip.AddDirectory(baseFolderName, exportFolder); nil != err {
|
2022-07-17 12:22:32 +08:00
|
|
|
|
logging.LogErrorf("create export markdown zip [%s] failed: %s", exportFolder, err)
|
2022-05-26 15:18:53 +08:00
|
|
|
|
return ""
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if err = zip.Close(); nil != err {
|
2022-07-17 12:22:32 +08:00
|
|
|
|
logging.LogErrorf("close export markdown zip failed: %s", err)
|
2022-05-26 15:18:53 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
os.RemoveAll(exportFolder)
|
|
|
|
|
zipPath = "/export/" + url.PathEscape(filepath.Base(zipPath))
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func ExportMarkdownContent(id string) (hPath, exportedMd string) {
|
|
|
|
|
return exportMarkdownContent(id)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func exportMarkdownContent(id string) (hPath, exportedMd string) {
|
|
|
|
|
tree, _ := loadTreeByBlockID(id)
|
|
|
|
|
hPath = tree.HPath
|
2023-01-02 21:28:50 +08:00
|
|
|
|
exportedMd = exportMarkdownContent0(tree)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func exportMarkdownContent0(tree *parse.Tree) (ret string) {
|
2022-09-24 21:50:22 +08:00
|
|
|
|
tree = exportTree(tree, false, true, false)
|
2022-05-26 15:18:53 +08:00
|
|
|
|
luteEngine := NewLute()
|
|
|
|
|
luteEngine.SetFootnotes(true)
|
|
|
|
|
luteEngine.SetKramdownIAL(false)
|
2022-09-15 11:46:41 +08:00
|
|
|
|
renderer := render.NewProtyleExportMdRenderer(tree, luteEngine.RenderOptions)
|
2023-01-02 21:28:50 +08:00
|
|
|
|
ret = gulu.Str.FromBytes(renderer.Render())
|
2022-05-26 15:18:53 +08:00
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-05 00:03:14 +08:00
|
|
|
|
func processKaTexMacros(n *ast.Node) {
|
2022-12-08 20:19:35 +08:00
|
|
|
|
if ast.NodeMathBlockContent != n.Type && ast.NodeTextMark != n.Type {
|
2022-09-16 20:50:14 +08:00
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
if ast.NodeTextMark == n.Type && !n.IsTextMarkType("inline-math") {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var mathContent string
|
|
|
|
|
if ast.NodeTextMark == n.Type {
|
|
|
|
|
mathContent = n.TextMarkInlineMathContent
|
|
|
|
|
} else {
|
|
|
|
|
mathContent = string(n.Tokens)
|
|
|
|
|
}
|
|
|
|
|
mathContent = strings.TrimSpace(mathContent)
|
|
|
|
|
if "" == mathContent {
|
2022-09-05 00:03:14 +08:00
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
macros := map[string]string{}
|
|
|
|
|
if err := gulu.JSON.UnmarshalJSON([]byte(Conf.Editor.KaTexMacros), ¯os); nil != err {
|
|
|
|
|
logging.LogWarnf("parse katex macros failed: %s", err)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var keys []string
|
2022-09-06 23:06:17 +08:00
|
|
|
|
for k := range macros {
|
2022-09-05 00:03:14 +08:00
|
|
|
|
keys = append(keys, k)
|
|
|
|
|
}
|
|
|
|
|
useMacro := false
|
2022-09-06 23:06:17 +08:00
|
|
|
|
for k := range macros {
|
|
|
|
|
if strings.Contains(mathContent, k) {
|
2022-09-05 00:03:14 +08:00
|
|
|
|
useMacro = true
|
|
|
|
|
break
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if !useMacro {
|
|
|
|
|
return
|
|
|
|
|
}
|
2022-09-06 23:06:17 +08:00
|
|
|
|
sort.Slice(keys, func(i, j int) bool { return len(keys[i]) > len(keys[j]) })
|
2022-09-05 00:03:14 +08:00
|
|
|
|
|
2022-09-06 23:06:17 +08:00
|
|
|
|
mathContent = escapeKaTexSupportedFunctions(mathContent)
|
|
|
|
|
usedMacros := extractUsedMacros(mathContent, &keys)
|
2022-09-05 00:03:14 +08:00
|
|
|
|
for _, usedMacro := range usedMacros {
|
|
|
|
|
expanded := resolveKaTexMacro(usedMacro, ¯os, &keys)
|
2022-09-06 23:06:17 +08:00
|
|
|
|
expanded = unescapeKaTexSupportedFunctions(expanded)
|
2022-09-07 00:12:31 +08:00
|
|
|
|
mathContent = strings.ReplaceAll(mathContent, usedMacro, expanded)
|
2022-09-05 00:03:14 +08:00
|
|
|
|
}
|
2022-09-07 00:12:31 +08:00
|
|
|
|
mathContent = unescapeKaTexSupportedFunctions(mathContent)
|
2022-09-16 20:50:14 +08:00
|
|
|
|
if ast.NodeTextMark == n.Type {
|
|
|
|
|
n.TextMarkInlineMathContent = mathContent
|
|
|
|
|
} else {
|
|
|
|
|
n.Tokens = []byte(mathContent)
|
|
|
|
|
}
|
2022-09-05 00:03:14 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-09-24 21:50:22 +08:00
|
|
|
|
func exportTree(tree *parse.Tree, wysiwyg, expandKaTexMacros, keepFold bool) (ret *parse.Tree) {
|
2022-05-26 15:18:53 +08:00
|
|
|
|
luteEngine := NewLute()
|
|
|
|
|
ret = tree
|
|
|
|
|
id := tree.Root.ID
|
|
|
|
|
var unlinks []*ast.Node
|
|
|
|
|
|
|
|
|
|
// 解析查询嵌入节点
|
|
|
|
|
ast.Walk(ret.Root, func(n *ast.Node, entering bool) ast.WalkStatus {
|
|
|
|
|
if !entering || ast.NodeBlockQueryEmbed != n.Type {
|
|
|
|
|
return ast.WalkContinue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var defMd string
|
|
|
|
|
stmt := n.ChildByType(ast.NodeBlockQueryEmbedScript).TokensStr()
|
|
|
|
|
stmt = html.UnescapeString(stmt)
|
2022-10-18 20:41:20 +08:00
|
|
|
|
embedBlocks := searchEmbedBlock(n.ID, stmt, nil, 0, false)
|
2022-10-12 10:11:08 +08:00
|
|
|
|
if 1 > len(embedBlocks) {
|
2022-05-26 15:18:53 +08:00
|
|
|
|
return ast.WalkContinue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
defMdBuf := bytes.Buffer{}
|
2022-10-12 10:11:08 +08:00
|
|
|
|
for _, def := range embedBlocks {
|
|
|
|
|
defMdBuf.WriteString(renderBlockMarkdownR(def.Block.ID))
|
2022-05-26 15:18:53 +08:00
|
|
|
|
defMdBuf.WriteString("\n\n")
|
|
|
|
|
}
|
|
|
|
|
defMd = defMdBuf.String()
|
|
|
|
|
|
|
|
|
|
buf := &bytes.Buffer{}
|
|
|
|
|
lines := strings.Split(defMd, "\n")
|
|
|
|
|
for i, line := range lines {
|
|
|
|
|
if 0 == Conf.Export.BlockEmbedMode { // 原始文本
|
|
|
|
|
buf.WriteString(line)
|
|
|
|
|
} else { // Blockquote
|
|
|
|
|
buf.WriteString("> " + line)
|
|
|
|
|
}
|
|
|
|
|
if i < len(lines)-1 {
|
|
|
|
|
buf.WriteString("\n")
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
buf.WriteString("\n\n")
|
|
|
|
|
|
|
|
|
|
refTree := parse.Parse("", buf.Bytes(), luteEngine.ParseOptions)
|
|
|
|
|
var children []*ast.Node
|
|
|
|
|
for c := refTree.Root.FirstChild; nil != c; c = c.Next {
|
|
|
|
|
children = append(children, c)
|
|
|
|
|
}
|
|
|
|
|
for _, c := range children {
|
|
|
|
|
if ast.NodeDocument == c.Type {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
n.InsertBefore(c)
|
|
|
|
|
}
|
|
|
|
|
unlinks = append(unlinks, n)
|
|
|
|
|
return ast.WalkSkipChildren
|
|
|
|
|
})
|
|
|
|
|
for _, n := range unlinks {
|
|
|
|
|
n.Unlink()
|
|
|
|
|
}
|
|
|
|
|
unlinks = nil
|
|
|
|
|
|
|
|
|
|
// 收集引用转脚注
|
|
|
|
|
var refFootnotes []*refAsFootnotes
|
|
|
|
|
if 4 == Conf.Export.BlockRefMode { // 块引转脚注
|
|
|
|
|
treeCache := map[string]*parse.Tree{}
|
|
|
|
|
treeCache[id] = ret
|
|
|
|
|
depth := 0
|
|
|
|
|
collectFootnotesDefs(ret.ID, &refFootnotes, &treeCache, &depth)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ast.Walk(ret.Root, func(n *ast.Node, entering bool) ast.WalkStatus {
|
|
|
|
|
if !entering {
|
|
|
|
|
return ast.WalkContinue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
switch n.Type {
|
|
|
|
|
case ast.NodeSuperBlockOpenMarker, ast.NodeSuperBlockLayoutMarker, ast.NodeSuperBlockCloseMarker:
|
|
|
|
|
if !wysiwyg {
|
|
|
|
|
unlinks = append(unlinks, n)
|
|
|
|
|
return ast.WalkContinue
|
|
|
|
|
}
|
|
|
|
|
case ast.NodeHeading:
|
|
|
|
|
n.HeadingNormalizedID = n.IALAttr("id")
|
|
|
|
|
n.ID = n.HeadingNormalizedID
|
2022-12-08 20:19:35 +08:00
|
|
|
|
case ast.NodeMathBlockContent:
|
2022-05-26 15:18:53 +08:00
|
|
|
|
n.Tokens = bytes.TrimSpace(n.Tokens) // 导出 Markdown 时去除公式内容中的首尾空格 https://github.com/siyuan-note/siyuan/issues/4666
|
|
|
|
|
return ast.WalkContinue
|
2022-09-16 20:50:14 +08:00
|
|
|
|
case ast.NodeTextMark:
|
|
|
|
|
if n.IsTextMarkType("inline-math") {
|
|
|
|
|
n.TextMarkInlineMathContent = strings.TrimSpace(n.TextMarkInlineMathContent)
|
|
|
|
|
return ast.WalkContinue
|
2022-09-16 22:59:24 +08:00
|
|
|
|
} else if n.IsTextMarkType("file-annotation-ref") {
|
|
|
|
|
refID := n.TextMarkFileAnnotationRefID
|
|
|
|
|
status := processFileAnnotationRef(refID, n)
|
|
|
|
|
unlinks = append(unlinks, n)
|
|
|
|
|
return status
|
2022-09-20 11:26:53 +08:00
|
|
|
|
} else if n.IsTextMarkType("tag") {
|
|
|
|
|
if !wysiwyg {
|
|
|
|
|
n.Type = ast.NodeText
|
|
|
|
|
n.Tokens = []byte(Conf.Export.TagOpenMarker + n.TextMarkTextContent + Conf.Export.TagCloseMarker)
|
|
|
|
|
return ast.WalkContinue
|
|
|
|
|
}
|
2022-09-16 20:50:14 +08:00
|
|
|
|
}
|
2022-05-26 15:18:53 +08:00
|
|
|
|
}
|
|
|
|
|
|
2022-09-16 18:02:04 +08:00
|
|
|
|
if !treenode.IsBlockRef(n) {
|
2022-05-26 15:18:53 +08:00
|
|
|
|
return ast.WalkContinue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 处理引用节点
|
|
|
|
|
|
2022-09-16 18:02:04 +08:00
|
|
|
|
defID, linkText, _ := treenode.GetBlockRef(n)
|
|
|
|
|
if "" == linkText {
|
|
|
|
|
linkText = sql.GetRefText(defID)
|
2022-05-26 15:18:53 +08:00
|
|
|
|
}
|
|
|
|
|
if Conf.Editor.BlockRefDynamicAnchorTextMaxLen < utf8.RuneCountInString(linkText) {
|
|
|
|
|
linkText = gulu.Str.SubStr(linkText, Conf.Editor.BlockRefDynamicAnchorTextMaxLen) + "..."
|
|
|
|
|
}
|
|
|
|
|
linkText = Conf.Export.BlockRefTextLeft + linkText + Conf.Export.BlockRefTextRight
|
|
|
|
|
|
2022-09-16 18:02:04 +08:00
|
|
|
|
defTree, _ := loadTreeByBlockID(defID)
|
2022-05-26 15:18:53 +08:00
|
|
|
|
if nil == defTree {
|
|
|
|
|
return ast.WalkContinue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
switch Conf.Export.BlockRefMode {
|
|
|
|
|
case 2: // 锚文本块链
|
|
|
|
|
var blockRefLink *ast.Node
|
|
|
|
|
blockRefLink = &ast.Node{Type: ast.NodeLink}
|
|
|
|
|
blockRefLink.AppendChild(&ast.Node{Type: ast.NodeOpenBracket})
|
|
|
|
|
blockRefLink.AppendChild(&ast.Node{Type: ast.NodeLinkText, Tokens: []byte(linkText)})
|
|
|
|
|
blockRefLink.AppendChild(&ast.Node{Type: ast.NodeCloseBracket})
|
|
|
|
|
blockRefLink.AppendChild(&ast.Node{Type: ast.NodeOpenParen})
|
2022-09-16 18:02:04 +08:00
|
|
|
|
blockRefLink.AppendChild(&ast.Node{Type: ast.NodeLinkDest, Tokens: []byte("siyuan://blocks/" + defID)})
|
2022-05-26 15:18:53 +08:00
|
|
|
|
blockRefLink.AppendChild(&ast.Node{Type: ast.NodeCloseParen})
|
|
|
|
|
n.InsertBefore(blockRefLink)
|
|
|
|
|
case 3: // 仅锚文本
|
|
|
|
|
n.InsertBefore(&ast.Node{Type: ast.NodeText, Tokens: []byte(linkText)})
|
|
|
|
|
case 4: // 脚注
|
|
|
|
|
refFoot := getRefAsFootnotes(defID, &refFootnotes)
|
|
|
|
|
n.InsertBefore(&ast.Node{Type: ast.NodeText, Tokens: []byte(linkText)})
|
|
|
|
|
n.InsertBefore(&ast.Node{Type: ast.NodeFootnotesRef, Tokens: []byte("^" + refFoot.refNum), FootnotesRefId: refFoot.refNum, FootnotesRefLabel: []byte("^" + refFoot.refNum)})
|
|
|
|
|
}
|
|
|
|
|
unlinks = append(unlinks, n)
|
2022-09-17 16:58:16 +08:00
|
|
|
|
if nil != n.Next && ast.NodeKramdownSpanIAL == n.Next.Type {
|
|
|
|
|
// 引用加排版标记(比如颜色)重叠时丢弃后面的排版属性节点
|
|
|
|
|
unlinks = append(unlinks, n.Next)
|
|
|
|
|
}
|
2022-05-26 15:18:53 +08:00
|
|
|
|
return ast.WalkSkipChildren
|
|
|
|
|
})
|
|
|
|
|
for _, n := range unlinks {
|
|
|
|
|
n.Unlink()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if 4 == Conf.Export.BlockRefMode { // 块引转脚注
|
|
|
|
|
if footnotesDefBlock := resolveFootnotesDefs(&refFootnotes, ret.Root.ID); nil != footnotesDefBlock {
|
|
|
|
|
ret.Root.AppendChild(footnotesDefBlock)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if Conf.Export.AddTitle {
|
|
|
|
|
if root, _ := getBlock(id); nil != root {
|
2022-10-24 23:24:13 +08:00
|
|
|
|
title := &ast.Node{Type: ast.NodeHeading, HeadingLevel: 1, KramdownIAL: parse.Map2IAL(root.IAL)}
|
2022-09-08 20:49:55 +08:00
|
|
|
|
content := html.UnescapeString(root.Content)
|
|
|
|
|
title.AppendChild(&ast.Node{Type: ast.NodeText, Tokens: []byte(content)})
|
2022-05-26 15:18:53 +08:00
|
|
|
|
ret.Root.PrependChild(title)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// 导出时支持导出题头图 https://github.com/siyuan-note/siyuan/issues/4372
|
|
|
|
|
titleImgPath := treenode.GetDocTitleImgPath(ret.Root)
|
|
|
|
|
if "" != titleImgPath {
|
|
|
|
|
p := &ast.Node{Type: ast.NodeParagraph}
|
|
|
|
|
titleImg := &ast.Node{Type: ast.NodeImage}
|
|
|
|
|
titleImg.AppendChild(&ast.Node{Type: ast.NodeBang})
|
|
|
|
|
titleImg.AppendChild(&ast.Node{Type: ast.NodeOpenBracket})
|
|
|
|
|
titleImg.AppendChild(&ast.Node{Type: ast.NodeLinkText, Tokens: []byte("image")})
|
|
|
|
|
titleImg.AppendChild(&ast.Node{Type: ast.NodeCloseBracket})
|
|
|
|
|
titleImg.AppendChild(&ast.Node{Type: ast.NodeOpenParen})
|
|
|
|
|
titleImg.AppendChild(&ast.Node{Type: ast.NodeLinkDest, Tokens: []byte(titleImgPath)})
|
|
|
|
|
titleImg.AppendChild(&ast.Node{Type: ast.NodeCloseParen})
|
|
|
|
|
p.AppendChild(titleImg)
|
|
|
|
|
ret.Root.PrependChild(p)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
unlinks = nil
|
2022-09-03 22:56:13 +08:00
|
|
|
|
var emptyParagraphs []*ast.Node
|
2022-05-26 15:18:53 +08:00
|
|
|
|
ast.Walk(ret.Root, func(n *ast.Node, entering bool) ast.WalkStatus {
|
|
|
|
|
if !entering {
|
|
|
|
|
return ast.WalkContinue
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-24 21:50:22 +08:00
|
|
|
|
// 支持按照现有折叠状态导出 PDF https://github.com/siyuan-note/siyuan/issues/5941
|
|
|
|
|
if !keepFold {
|
|
|
|
|
// 块折叠以后导出 HTML/PDF 固定展开 https://github.com/siyuan-note/siyuan/issues/4064
|
|
|
|
|
n.RemoveIALAttr("fold")
|
|
|
|
|
n.RemoveIALAttr("heading-fold")
|
2022-09-24 23:17:23 +08:00
|
|
|
|
} else {
|
|
|
|
|
if "1" == n.IALAttr("heading-fold") {
|
|
|
|
|
unlinks = append(unlinks, n)
|
|
|
|
|
return ast.WalkContinue
|
|
|
|
|
}
|
2022-09-24 21:50:22 +08:00
|
|
|
|
}
|
2022-05-26 15:18:53 +08:00
|
|
|
|
|
2022-12-04 12:13:51 +08:00
|
|
|
|
switch n.Type {
|
|
|
|
|
case ast.NodeParagraph:
|
2022-09-03 22:56:13 +08:00
|
|
|
|
if nil == n.FirstChild {
|
|
|
|
|
// 空的段落块需要补全文本展位,否则后续格式化后再解析树会语义不一致 https://github.com/siyuan-note/siyuan/issues/5806
|
|
|
|
|
emptyParagraphs = append(emptyParagraphs, n)
|
|
|
|
|
}
|
2022-12-08 20:19:35 +08:00
|
|
|
|
case ast.NodeMathBlockContent:
|
2022-12-04 12:13:51 +08:00
|
|
|
|
if expandKaTexMacros {
|
|
|
|
|
processKaTexMacros(n)
|
|
|
|
|
}
|
|
|
|
|
case ast.NodeTextMark:
|
|
|
|
|
if expandKaTexMacros && n.IsTextMarkType("inline-math") {
|
|
|
|
|
processKaTexMacros(n)
|
|
|
|
|
}
|
|
|
|
|
case ast.NodeWidget:
|
2022-10-13 22:25:31 +08:00
|
|
|
|
// 挂件块导出 https://github.com/siyuan-note/siyuan/issues/3834 https://github.com/siyuan-note/siyuan/issues/6188
|
|
|
|
|
|
|
|
|
|
if wysiwyg {
|
|
|
|
|
exportHtmlVal := n.IALAttr("data-export-html")
|
|
|
|
|
if "" != exportHtmlVal {
|
|
|
|
|
htmlBlock := &ast.Node{Type: ast.NodeHTMLBlock, Tokens: []byte(exportHtmlVal)}
|
|
|
|
|
n.InsertBefore(htmlBlock)
|
|
|
|
|
unlinks = append(unlinks, n)
|
|
|
|
|
return ast.WalkContinue
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-26 15:18:53 +08:00
|
|
|
|
exportMdVal := n.IALAttr("data-export-md")
|
|
|
|
|
exportMdVal = html.UnescapeString(exportMdVal) // 导出 `data-export-md` 时未解析代码块与行内代码内的转义字符 https://github.com/siyuan-note/siyuan/issues/4180
|
|
|
|
|
if "" != exportMdVal {
|
|
|
|
|
exportMdTree := parse.Parse("", []byte(exportMdVal), luteEngine.ParseOptions)
|
|
|
|
|
var insertNodes []*ast.Node
|
|
|
|
|
for c := exportMdTree.Root.FirstChild; nil != c; c = c.Next {
|
|
|
|
|
if ast.NodeKramdownBlockIAL != c.Type {
|
|
|
|
|
insertNodes = append(insertNodes, c)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
for _, insertNode := range insertNodes {
|
|
|
|
|
n.InsertBefore(insertNode)
|
|
|
|
|
}
|
|
|
|
|
unlinks = append(unlinks, n)
|
|
|
|
|
}
|
2022-12-04 12:13:51 +08:00
|
|
|
|
case ast.NodeSuperBlockOpenMarker, ast.NodeSuperBlockLayoutMarker, ast.NodeSuperBlockCloseMarker:
|
|
|
|
|
if !wysiwyg {
|
|
|
|
|
unlinks = append(unlinks, n)
|
|
|
|
|
}
|
2022-05-26 15:18:53 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ast.NodeText != n.Type {
|
|
|
|
|
return ast.WalkContinue
|
|
|
|
|
}
|
2022-12-04 12:13:51 +08:00
|
|
|
|
|
2022-05-26 15:18:53 +08:00
|
|
|
|
// Shift+Enter 换行在导出为 Markdown 时使用硬换行 https://github.com/siyuan-note/siyuan/issues/3458
|
|
|
|
|
n.Tokens = bytes.ReplaceAll(n.Tokens, []byte("\n"), []byte(" \n"))
|
|
|
|
|
return ast.WalkContinue
|
|
|
|
|
})
|
|
|
|
|
for _, n := range unlinks {
|
|
|
|
|
n.Unlink()
|
|
|
|
|
}
|
2022-09-03 22:56:13 +08:00
|
|
|
|
for _, emptyParagraph := range emptyParagraphs {
|
2022-09-14 00:35:13 +08:00
|
|
|
|
emptyParagraph.AppendChild(&ast.Node{Type: ast.NodeText, Tokens: []byte(editor.Zwj)})
|
2022-09-03 22:56:13 +08:00
|
|
|
|
}
|
2022-05-26 15:18:53 +08:00
|
|
|
|
return ret
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func resolveFootnotesDefs(refFootnotes *[]*refAsFootnotes, rootID string) (footnotesDefBlock *ast.Node) {
|
|
|
|
|
if 1 > len(*refFootnotes) {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
footnotesDefBlock = &ast.Node{Type: ast.NodeFootnotesDefBlock}
|
2022-08-02 11:17:08 +08:00
|
|
|
|
var rendered []string
|
2022-05-26 15:18:53 +08:00
|
|
|
|
for _, foot := range *refFootnotes {
|
|
|
|
|
t, err := loadTreeByBlockID(foot.defID)
|
|
|
|
|
if nil != err {
|
|
|
|
|
continue
|
|
|
|
|
}
|
|
|
|
|
defNode := treenode.GetNodeInTree(t, foot.defID)
|
2022-09-23 19:42:27 +08:00
|
|
|
|
docID := strings.TrimSuffix(path.Base(defNode.Path), ".sy")
|
2022-05-26 15:18:53 +08:00
|
|
|
|
var nodes []*ast.Node
|
|
|
|
|
if ast.NodeHeading == defNode.Type {
|
|
|
|
|
nodes = append(nodes, defNode)
|
2022-09-23 19:42:27 +08:00
|
|
|
|
if rootID != docID {
|
|
|
|
|
// 同文档块引转脚注缩略定义考虑容器块和标题块 https://github.com/siyuan-note/siyuan/issues/5917
|
|
|
|
|
children := treenode.HeadingChildren(defNode)
|
|
|
|
|
nodes = append(nodes, children...)
|
|
|
|
|
}
|
2022-05-26 15:18:53 +08:00
|
|
|
|
} else if ast.NodeDocument == defNode.Type {
|
|
|
|
|
docTitle := &ast.Node{ID: defNode.ID, Type: ast.NodeHeading, HeadingLevel: 1}
|
|
|
|
|
docTitle.AppendChild(&ast.Node{Type: ast.NodeText, Tokens: []byte(defNode.IALAttr("title"))})
|
|
|
|
|
nodes = append(nodes, docTitle)
|
|
|
|
|
for c := defNode.FirstChild; nil != c; c = c.Next {
|
|
|
|
|
nodes = append(nodes, c)
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
nodes = append(nodes, defNode)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
var newNodes []*ast.Node
|
|
|
|
|
for _, node := range nodes {
|
|
|
|
|
var unlinks []*ast.Node
|
|
|
|
|
|
|
|
|
|
ast.Walk(node, func(n *ast.Node, entering bool) ast.WalkStatus {
|
|
|
|
|
if !entering {
|
|
|
|
|
return ast.WalkContinue
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-16 18:02:04 +08:00
|
|
|
|
if treenode.IsBlockRef(n) {
|
|
|
|
|
defID, _, _ := treenode.GetBlockRef(n)
|
2022-05-26 15:18:53 +08:00
|
|
|
|
if f := getRefAsFootnotes(defID, refFootnotes); nil != f {
|
|
|
|
|
n.InsertBefore(&ast.Node{Type: ast.NodeText, Tokens: []byte(Conf.Export.BlockRefTextLeft + f.refAnchorText + Conf.Export.BlockRefTextRight)})
|
|
|
|
|
n.InsertBefore(&ast.Node{Type: ast.NodeFootnotesRef, Tokens: []byte("^" + f.refNum), FootnotesRefId: f.refNum, FootnotesRefLabel: []byte("^" + f.refNum)})
|
|
|
|
|
unlinks = append(unlinks, n)
|
|
|
|
|
}
|
|
|
|
|
return ast.WalkSkipChildren
|
|
|
|
|
} else if ast.NodeBlockQueryEmbed == n.Type {
|
|
|
|
|
stmt := n.ChildByType(ast.NodeBlockQueryEmbedScript).TokensStr()
|
|
|
|
|
stmt = html.UnescapeString(stmt)
|
|
|
|
|
sqlBlocks := sql.SelectBlocksRawStmt(stmt, Conf.Search.Limit)
|
|
|
|
|
for _, b := range sqlBlocks {
|
2022-08-02 11:17:08 +08:00
|
|
|
|
subNodes := renderBlockMarkdownR0(b.ID, &rendered)
|
2022-05-26 15:18:53 +08:00
|
|
|
|
for _, subNode := range subNodes {
|
|
|
|
|
if ast.NodeListItem == subNode.Type {
|
|
|
|
|
parentList := &ast.Node{Type: ast.NodeList, ListData: &ast.ListData{Typ: subNode.ListData.Typ}}
|
|
|
|
|
parentList.AppendChild(subNode)
|
|
|
|
|
newNodes = append(newNodes, parentList)
|
|
|
|
|
} else {
|
|
|
|
|
newNodes = append(newNodes, subNode)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
unlinks = append(unlinks, n)
|
|
|
|
|
return ast.WalkSkipChildren
|
|
|
|
|
}
|
|
|
|
|
return ast.WalkContinue
|
|
|
|
|
})
|
|
|
|
|
for _, n := range unlinks {
|
|
|
|
|
n.Unlink()
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if ast.NodeBlockQueryEmbed != node.Type {
|
|
|
|
|
if ast.NodeListItem == node.Type {
|
|
|
|
|
parentList := &ast.Node{Type: ast.NodeList, ListData: &ast.ListData{Typ: node.ListData.Typ}}
|
|
|
|
|
parentList.AppendChild(node)
|
|
|
|
|
newNodes = append(newNodes, parentList)
|
|
|
|
|
} else {
|
|
|
|
|
newNodes = append(newNodes, node)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
footnotesDef := &ast.Node{Type: ast.NodeFootnotesDef, Tokens: []byte("^" + foot.refNum), FootnotesRefId: foot.refNum, FootnotesRefLabel: []byte("^" + foot.refNum)}
|
|
|
|
|
for _, node := range newNodes {
|
|
|
|
|
ast.Walk(node, func(n *ast.Node, entering bool) ast.WalkStatus {
|
|
|
|
|
if !entering {
|
|
|
|
|
return ast.WalkContinue
|
|
|
|
|
}
|
|
|
|
|
if ast.NodeParagraph != n.Type {
|
|
|
|
|
return ast.WalkContinue
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
docID := strings.TrimSuffix(path.Base(n.Path), ".sy")
|
2022-09-23 19:42:27 +08:00
|
|
|
|
if rootID == docID {
|
|
|
|
|
// 同文档块引转脚注缩略定义 https://github.com/siyuan-note/siyuan/issues/3299
|
2022-05-26 15:18:53 +08:00
|
|
|
|
if text := sql.GetRefText(n.ID); 64 < utf8.RuneCountInString(text) {
|
|
|
|
|
var unlinkChildren []*ast.Node
|
|
|
|
|
for c := n.FirstChild; nil != c; c = c.Next {
|
|
|
|
|
unlinkChildren = append(unlinkChildren, c)
|
|
|
|
|
}
|
|
|
|
|
for _, c := range unlinkChildren {
|
|
|
|
|
c.Unlink()
|
|
|
|
|
}
|
|
|
|
|
text = gulu.Str.SubStr(text, 64) + "..."
|
|
|
|
|
n.AppendChild(&ast.Node{Type: ast.NodeText, Tokens: []byte(text)})
|
|
|
|
|
return ast.WalkSkipChildren
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return ast.WalkContinue
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
footnotesDef.AppendChild(node)
|
|
|
|
|
}
|
|
|
|
|
footnotesDefBlock.AppendChild(footnotesDef)
|
|
|
|
|
}
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func collectFootnotesDefs(id string, refFootnotes *[]*refAsFootnotes, treeCache *map[string]*parse.Tree, depth *int) {
|
|
|
|
|
*depth++
|
|
|
|
|
if 4096 < *depth {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
b := treenode.GetBlockTree(id)
|
|
|
|
|
if nil == b {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
t := (*treeCache)[b.RootID]
|
|
|
|
|
if nil == t {
|
|
|
|
|
var err error
|
|
|
|
|
if t, err = loadTreeByBlockID(b.ID); nil != err {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
(*treeCache)[t.ID] = t
|
|
|
|
|
}
|
|
|
|
|
node := treenode.GetNodeInTree(t, b.ID)
|
|
|
|
|
if nil == node {
|
2022-07-17 12:22:32 +08:00
|
|
|
|
logging.LogErrorf("not found node [%s] in tree [%s]", b.ID, t.Root.ID)
|
2022-05-26 15:18:53 +08:00
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
collectFootnotesDefs0(node, refFootnotes, treeCache, depth)
|
|
|
|
|
if ast.NodeHeading == node.Type {
|
|
|
|
|
children := treenode.HeadingChildren(node)
|
|
|
|
|
for _, c := range children {
|
|
|
|
|
collectFootnotesDefs0(c, refFootnotes, treeCache, depth)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func collectFootnotesDefs0(node *ast.Node, refFootnotes *[]*refAsFootnotes, treeCache *map[string]*parse.Tree, depth *int) {
|
|
|
|
|
ast.Walk(node, func(n *ast.Node, entering bool) ast.WalkStatus {
|
|
|
|
|
if !entering {
|
|
|
|
|
return ast.WalkContinue
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-16 18:02:04 +08:00
|
|
|
|
if treenode.IsBlockRef(n) {
|
2022-11-16 21:41:24 +08:00
|
|
|
|
defID, refText, _ := treenode.GetBlockRef(n)
|
2022-05-26 15:18:53 +08:00
|
|
|
|
if nil == getRefAsFootnotes(defID, refFootnotes) {
|
2022-11-16 21:41:24 +08:00
|
|
|
|
anchorText := refText
|
2022-05-26 15:18:53 +08:00
|
|
|
|
if Conf.Editor.BlockRefDynamicAnchorTextMaxLen < utf8.RuneCountInString(anchorText) {
|
|
|
|
|
anchorText = gulu.Str.SubStr(anchorText, Conf.Editor.BlockRefDynamicAnchorTextMaxLen) + "..."
|
|
|
|
|
}
|
|
|
|
|
*refFootnotes = append(*refFootnotes, &refAsFootnotes{
|
|
|
|
|
defID: defID,
|
|
|
|
|
refNum: strconv.Itoa(len(*refFootnotes) + 1),
|
|
|
|
|
refAnchorText: anchorText,
|
|
|
|
|
})
|
|
|
|
|
collectFootnotesDefs(defID, refFootnotes, treeCache, depth)
|
|
|
|
|
}
|
|
|
|
|
return ast.WalkSkipChildren
|
|
|
|
|
}
|
|
|
|
|
return ast.WalkContinue
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
func getRefAsFootnotes(defID string, slice *[]*refAsFootnotes) *refAsFootnotes {
|
|
|
|
|
for _, e := range *slice {
|
|
|
|
|
if e.defID == defID {
|
|
|
|
|
return e
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
type refAsFootnotes struct {
|
|
|
|
|
defID string
|
|
|
|
|
refNum string
|
|
|
|
|
refAnchorText string
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func exportRefTrees(tree *parse.Tree) (ret map[string]*parse.Tree) {
|
|
|
|
|
ret = map[string]*parse.Tree{}
|
|
|
|
|
exportRefTrees0(tree, &ret)
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func exportRefTrees0(tree *parse.Tree, retTrees *map[string]*parse.Tree) {
|
|
|
|
|
if nil != (*retTrees)[tree.ID] {
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
(*retTrees)[tree.ID] = tree
|
|
|
|
|
|
|
|
|
|
ast.Walk(tree.Root, func(n *ast.Node, entering bool) ast.WalkStatus {
|
|
|
|
|
if !entering {
|
|
|
|
|
return ast.WalkContinue
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-16 18:02:04 +08:00
|
|
|
|
if treenode.IsBlockRef(n) {
|
|
|
|
|
defID, _, _ := treenode.GetBlockRef(n)
|
|
|
|
|
if "" == defID {
|
|
|
|
|
return ast.WalkContinue
|
2022-05-26 15:18:53 +08:00
|
|
|
|
}
|
|
|
|
|
defBlock := treenode.GetBlockTree(defID)
|
|
|
|
|
if nil == defBlock {
|
|
|
|
|
return ast.WalkSkipChildren
|
|
|
|
|
}
|
|
|
|
|
defTree, err := loadTreeByBlockID(defBlock.RootID)
|
|
|
|
|
if nil != err {
|
|
|
|
|
return ast.WalkSkipChildren
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
exportRefTrees0(defTree, retTrees)
|
|
|
|
|
}
|
|
|
|
|
return ast.WalkContinue
|
|
|
|
|
})
|
|
|
|
|
}
|
2022-09-16 22:59:24 +08:00
|
|
|
|
|
|
|
|
|
func processFileAnnotationRef(refID string, n *ast.Node) ast.WalkStatus {
|
|
|
|
|
p := refID[:strings.LastIndex(refID, "/")]
|
|
|
|
|
absPath, err := GetAssetAbsPath(p)
|
|
|
|
|
if nil != err {
|
|
|
|
|
logging.LogWarnf("get assets abs path by rel path [%s] failed: %s", p, err)
|
|
|
|
|
return ast.WalkSkipChildren
|
|
|
|
|
}
|
|
|
|
|
sya := absPath + ".sya"
|
|
|
|
|
syaData, err := os.ReadFile(sya)
|
|
|
|
|
if nil != err {
|
|
|
|
|
logging.LogErrorf("read file [%s] failed: %s", sya, err)
|
|
|
|
|
return ast.WalkSkipChildren
|
|
|
|
|
}
|
|
|
|
|
syaJSON := map[string]interface{}{}
|
|
|
|
|
if err = gulu.JSON.UnmarshalJSON(syaData, &syaJSON); nil != err {
|
|
|
|
|
logging.LogErrorf("unmarshal file [%s] failed: %s", sya, err)
|
|
|
|
|
return ast.WalkSkipChildren
|
|
|
|
|
}
|
|
|
|
|
annotationID := refID[strings.LastIndex(refID, "/")+1:]
|
|
|
|
|
annotationData := syaJSON[annotationID]
|
|
|
|
|
if nil == annotationData {
|
|
|
|
|
logging.LogErrorf("not found annotation [%s] in .sya", annotationID)
|
|
|
|
|
return ast.WalkSkipChildren
|
|
|
|
|
}
|
|
|
|
|
pages := annotationData.(map[string]interface{})["pages"].([]interface{})
|
|
|
|
|
page := int(pages[0].(map[string]interface{})["index"].(float64)) + 1
|
|
|
|
|
pageStr := strconv.Itoa(page)
|
|
|
|
|
|
2022-12-08 20:32:42 +08:00
|
|
|
|
refText := n.TextMarkTextContent
|
2022-09-16 22:59:24 +08:00
|
|
|
|
ext := filepath.Ext(p)
|
|
|
|
|
file := p[7:len(p)-23-len(ext)] + ext
|
|
|
|
|
fileAnnotationRefLink := &ast.Node{Type: ast.NodeLink}
|
|
|
|
|
fileAnnotationRefLink.AppendChild(&ast.Node{Type: ast.NodeOpenBracket})
|
|
|
|
|
if 0 == Conf.Export.FileAnnotationRefMode {
|
|
|
|
|
fileAnnotationRefLink.AppendChild(&ast.Node{Type: ast.NodeLinkText, Tokens: []byte(file + " - p" + pageStr + " - " + refText)})
|
|
|
|
|
} else {
|
|
|
|
|
fileAnnotationRefLink.AppendChild(&ast.Node{Type: ast.NodeLinkText, Tokens: []byte(refText)})
|
|
|
|
|
}
|
|
|
|
|
fileAnnotationRefLink.AppendChild(&ast.Node{Type: ast.NodeCloseBracket})
|
|
|
|
|
fileAnnotationRefLink.AppendChild(&ast.Node{Type: ast.NodeOpenParen})
|
|
|
|
|
fileAnnotationRefLink.AppendChild(&ast.Node{Type: ast.NodeLinkDest, Tokens: []byte(p + "?p=" + pageStr)})
|
|
|
|
|
fileAnnotationRefLink.AppendChild(&ast.Node{Type: ast.NodeCloseParen})
|
|
|
|
|
n.InsertBefore(fileAnnotationRefLink)
|
|
|
|
|
return ast.WalkSkipChildren
|
|
|
|
|
}
|