export.go 42 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389
  1. // SiYuan - Build Your Eternal Digital Garden
  2. // Copyright (c) 2020-present, b3log.org
  3. //
  4. // This program is free software: you can redistribute it and/or modify
  5. // it under the terms of the GNU Affero General Public License as published by
  6. // the Free Software Foundation, either version 3 of the License, or
  7. // (at your option) any later version.
  8. //
  9. // This program is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU Affero General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Affero General Public License
  15. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  16. package model
  17. import (
  18. "bytes"
  19. "errors"
  20. "fmt"
  21. "net/url"
  22. "os"
  23. "os/exec"
  24. "path"
  25. "path/filepath"
  26. "sort"
  27. "strconv"
  28. "strings"
  29. "unicode/utf8"
  30. "github.com/88250/gulu"
  31. "github.com/88250/lute/ast"
  32. "github.com/88250/lute/html"
  33. "github.com/88250/lute/lex"
  34. "github.com/88250/lute/parse"
  35. "github.com/88250/lute/render"
  36. "github.com/88250/pdfcpu/pkg/api"
  37. "github.com/88250/pdfcpu/pkg/pdfcpu"
  38. "github.com/emirpasic/gods/sets/hashset"
  39. "github.com/emirpasic/gods/stacks/linkedliststack"
  40. "github.com/siyuan-note/siyuan/kernel/filesys"
  41. "github.com/siyuan-note/siyuan/kernel/sql"
  42. "github.com/siyuan-note/siyuan/kernel/treenode"
  43. "github.com/siyuan-note/siyuan/kernel/util"
  44. )
  45. func ExportSY(id string) (name, zipPath string) {
  46. block := treenode.GetBlockTree(id)
  47. if nil == block {
  48. util.LogErrorf("not found block [%s]", id)
  49. return
  50. }
  51. boxID := block.BoxID
  52. box := Conf.Box(boxID)
  53. baseFolderName := path.Base(block.HPath)
  54. if "." == baseFolderName {
  55. baseFolderName = path.Base(block.Path)
  56. }
  57. rootPath := block.Path
  58. docPaths := []string{rootPath}
  59. docFiles := box.ListFiles(strings.TrimSuffix(block.Path, ".sy"))
  60. for _, docFile := range docFiles {
  61. docPaths = append(docPaths, docFile.path)
  62. }
  63. zipPath = exportSYZip(boxID, path.Dir(rootPath), baseFolderName, docPaths)
  64. name = strings.TrimSuffix(filepath.Base(block.Path), ".sy")
  65. return
  66. }
  67. func ExportDataInFolder(exportFolder string) (err error) {
  68. util.PushEndlessProgress(Conf.Language(65))
  69. defer util.ClearPushProgress(100)
  70. WaitForWritingFiles()
  71. writingDataLock.Lock()
  72. defer writingDataLock.Unlock()
  73. exportFolder = filepath.Join(exportFolder, util.CurrentTimeSecondsStr())
  74. err = exportData(exportFolder)
  75. if nil != err {
  76. return
  77. }
  78. return
  79. }
  80. func ExportData() (zipPath string) {
  81. util.PushEndlessProgress(Conf.Language(65))
  82. defer util.ClearPushProgress(100)
  83. WaitForWritingFiles()
  84. writingDataLock.Lock()
  85. defer writingDataLock.Unlock()
  86. baseFolderName := "data-" + util.CurrentTimeSecondsStr()
  87. exportFolder := filepath.Join(util.TempDir, "export", baseFolderName)
  88. zipPath = exportFolder + ".zip"
  89. err := exportData(exportFolder)
  90. if nil != err {
  91. return
  92. }
  93. zipPath = "/export/" + url.PathEscape(filepath.Base(zipPath))
  94. return
  95. }
  96. func exportData(exportFolder string) (err error) {
  97. baseFolderName := "data-" + util.CurrentTimeSecondsStr()
  98. if err = os.MkdirAll(exportFolder, 0755); nil != err {
  99. util.LogErrorf("create export temp folder failed: %s", err)
  100. return
  101. }
  102. filesys.ReleaseAllFileLocks()
  103. data := filepath.Join(util.WorkspaceDir, "data")
  104. if err = stableCopy(data, exportFolder); nil != err {
  105. util.LogErrorf("copy data dir from [%s] to [%s] failed: %s", data, baseFolderName, err)
  106. err = errors.New(fmt.Sprintf(Conf.Language(23), formatErrorMsg(err)))
  107. return
  108. }
  109. zipPath := exportFolder + ".zip"
  110. zip, err := gulu.Zip.Create(zipPath)
  111. if nil != err {
  112. util.LogErrorf("create export data zip [%s] failed: %s", exportFolder, err)
  113. return
  114. }
  115. if err = zip.AddDirectory(baseFolderName, exportFolder); nil != err {
  116. util.LogErrorf("create export data zip [%s] failed: %s", exportFolder, err)
  117. return
  118. }
  119. if err = zip.Close(); nil != err {
  120. util.LogErrorf("close export data zip failed: %s", err)
  121. }
  122. os.RemoveAll(exportFolder)
  123. return
  124. }
  125. func Preview(id string) string {
  126. tree, _ := loadTreeByBlockID(id)
  127. tree = exportTree(tree, false)
  128. luteEngine := NewLute()
  129. luteEngine.SetFootnotes(true)
  130. md := treenode.FormatNode(tree.Root, luteEngine)
  131. tree = parse.Parse("", []byte(md), luteEngine.ParseOptions)
  132. return luteEngine.ProtylePreview(tree, luteEngine.RenderOptions)
  133. }
  134. func ExportDocx(id, savePath string) (err error) {
  135. if !util.IsValidPandocBin(Conf.Export.PandocBin) {
  136. return errors.New(Conf.Language(115))
  137. }
  138. tmpDir := filepath.Join(util.TempDir, "export", gulu.Rand.String(7))
  139. defer os.Remove(tmpDir)
  140. name, dom := ExportMarkdownHTML(id, tmpDir, true)
  141. tmpDocxPath := filepath.Join(tmpDir, name+".docx")
  142. args := []string{ // pandoc -f html --resource-path=请从这里开始 请从这里开始\index.html -o test.docx
  143. "-f", "html",
  144. "--resource-path", tmpDir,
  145. "-o", tmpDocxPath,
  146. }
  147. pandoc := exec.Command(Conf.Export.PandocBin, args...)
  148. util.CmdAttr(pandoc)
  149. pandoc.Stdin = bytes.NewBufferString(dom)
  150. output, err := pandoc.CombinedOutput()
  151. if nil != err {
  152. util.LogErrorf("export docx failed: %s", gulu.Str.FromBytes(output))
  153. msg := fmt.Sprintf(Conf.Language(14), gulu.Str.FromBytes(output))
  154. return errors.New(msg)
  155. }
  156. if err = gulu.File.Copy(tmpDocxPath, filepath.Join(savePath, name+".docx")); nil != err {
  157. util.LogErrorf("export docx failed: %s", err)
  158. return errors.New(fmt.Sprintf(Conf.Language(14), err))
  159. }
  160. tmpAssets := filepath.Join(tmpDir, "assets")
  161. if gulu.File.IsDir(tmpAssets) {
  162. if err = gulu.File.Copy(tmpAssets, filepath.Join(savePath, "assets")); nil != err {
  163. util.LogErrorf("export docx failed: %s", err)
  164. return errors.New(fmt.Sprintf(Conf.Language(14), err))
  165. }
  166. }
  167. return
  168. }
  169. func ExportMarkdownHTML(id, savePath string, docx bool) (name, dom string) {
  170. tree, _ := loadTreeByBlockID(id)
  171. tree = exportTree(tree, true)
  172. name = path.Base(tree.HPath)
  173. if err := os.MkdirAll(savePath, 0755); nil != err {
  174. util.LogErrorf("mkdir [%s] failed: %s", savePath, err)
  175. return
  176. }
  177. assets := assetsLinkDestsInTree(tree)
  178. for _, asset := range assets {
  179. if strings.HasPrefix(asset, "assets/") {
  180. srcAbsPath, err := GetAssetAbsPath(asset)
  181. if nil != err {
  182. util.LogWarnf("resolve path of asset [%s] failed: %s", asset, err)
  183. continue
  184. }
  185. targetAbsPath := filepath.Join(savePath, asset)
  186. if err = gulu.File.Copy(srcAbsPath, targetAbsPath); nil != err {
  187. util.LogWarnf("copy asset from [%s] to [%s] failed: %s", srcAbsPath, targetAbsPath, err)
  188. }
  189. }
  190. }
  191. srcs := []string{"stage/build/export", "stage/build/fonts", "stage/protyle"}
  192. for _, src := range srcs {
  193. from := filepath.Join(util.WorkingDir, src)
  194. to := filepath.Join(savePath, src)
  195. if err := gulu.File.Copy(from, to); nil != err {
  196. util.LogWarnf("copy stage from [%s] to [%s] failed: %s", from, savePath, err)
  197. return
  198. }
  199. }
  200. theme := Conf.Appearance.ThemeLight
  201. if 1 == Conf.Appearance.Mode {
  202. theme = Conf.Appearance.ThemeDark
  203. }
  204. srcs = []string{"icons", "themes/" + theme}
  205. for _, src := range srcs {
  206. from := filepath.Join(util.AppearancePath, src)
  207. to := filepath.Join(savePath, "appearance", src)
  208. if err := gulu.File.Copy(from, to); nil != err {
  209. util.LogErrorf("copy appearance from [%s] to [%s] failed: %s", from, savePath, err)
  210. return
  211. }
  212. }
  213. luteEngine := NewLute()
  214. luteEngine.SetFootnotes(true)
  215. md := treenode.FormatNode(tree.Root, luteEngine)
  216. tree = parse.Parse("", []byte(md), luteEngine.ParseOptions)
  217. if docx {
  218. processIFrame(tree)
  219. }
  220. dom = luteEngine.ProtylePreview(tree, luteEngine.RenderOptions)
  221. return
  222. }
  223. func ExportHTML(id, savePath string, pdf bool) (name, dom string) {
  224. tree, _ := loadTreeByBlockID(id)
  225. var headings []*ast.Node
  226. if pdf { // 导出 PDF 需要标记目录书签
  227. ast.Walk(tree.Root, func(n *ast.Node, entering bool) ast.WalkStatus {
  228. if entering && ast.NodeHeading == n.Type && !n.ParentIs(ast.NodeBlockquote) {
  229. headings = append(headings, n)
  230. return ast.WalkSkipChildren
  231. }
  232. return ast.WalkContinue
  233. })
  234. for _, h := range headings {
  235. link := &ast.Node{Type: ast.NodeLink}
  236. link.AppendChild(&ast.Node{Type: ast.NodeOpenBracket})
  237. link.AppendChild(&ast.Node{Type: ast.NodeText, Tokens: []byte(" ")})
  238. link.AppendChild(&ast.Node{Type: ast.NodeCloseBracket})
  239. link.AppendChild(&ast.Node{Type: ast.NodeOpenParen})
  240. link.AppendChild(&ast.Node{Type: ast.NodeLinkDest, Tokens: []byte("pdf-outline://" + h.ID)})
  241. link.AppendChild(&ast.Node{Type: ast.NodeCloseParen})
  242. h.PrependChild(link)
  243. }
  244. }
  245. tree = exportTree(tree, true)
  246. name = path.Base(tree.Path)
  247. if err := os.MkdirAll(savePath, 0755); nil != err {
  248. util.LogErrorf("mkdir [%s] failed: %s", savePath, err)
  249. return
  250. }
  251. assets := assetsLinkDestsInTree(tree)
  252. for _, asset := range assets {
  253. srcAbsPath, err := GetAssetAbsPath(asset)
  254. if nil != err {
  255. util.LogWarnf("resolve path of asset [%s] failed: %s", asset, err)
  256. continue
  257. }
  258. targetAbsPath := filepath.Join(savePath, asset)
  259. if err = gulu.File.Copy(srcAbsPath, targetAbsPath); nil != err {
  260. util.LogWarnf("copy asset from [%s] to [%s] failed: %s", srcAbsPath, targetAbsPath, err)
  261. }
  262. }
  263. luteEngine := NewLute()
  264. if !pdf { // 导出 HTML 需要复制静态资源
  265. srcs := []string{"stage/build/export", "stage/build/fonts", "stage/protyle"}
  266. for _, src := range srcs {
  267. from := filepath.Join(util.WorkingDir, src)
  268. to := filepath.Join(savePath, src)
  269. if err := gulu.File.Copy(from, to); nil != err {
  270. util.LogErrorf("copy stage from [%s] to [%s] failed: %s", from, savePath, err)
  271. return
  272. }
  273. }
  274. theme := Conf.Appearance.ThemeLight
  275. if 1 == Conf.Appearance.Mode {
  276. theme = Conf.Appearance.ThemeDark
  277. }
  278. srcs = []string{"icons", "themes/" + theme}
  279. for _, src := range srcs {
  280. from := filepath.Join(util.AppearancePath, src)
  281. to := filepath.Join(savePath, "appearance", src)
  282. if err := gulu.File.Copy(from, to); nil != err {
  283. util.LogErrorf("copy appearance from [%s] to [%s] failed: %s", from, savePath, err)
  284. return
  285. }
  286. }
  287. } else { // 导出 PDF 需要将资源文件路径改为 HTTP 伺服
  288. luteEngine.RenderOptions.LinkBase = "http://127.0.0.1:" + util.ServerPort + "/"
  289. }
  290. if pdf {
  291. processIFrame(tree)
  292. }
  293. luteEngine.SetFootnotes(true)
  294. luteEngine.RenderOptions.ProtyleContenteditable = false
  295. renderer := render.NewBlockExportRenderer(tree, luteEngine.RenderOptions)
  296. dom = gulu.Str.FromBytes(renderer.Render())
  297. return
  298. }
  299. func processIFrame(tree *parse.Tree) {
  300. // 导出 PDF/Word 时 IFrame 块使用超链接 https://github.com/siyuan-note/siyuan/issues/4035
  301. var unlinks []*ast.Node
  302. ast.Walk(tree.Root, func(n *ast.Node, entering bool) ast.WalkStatus {
  303. if !entering {
  304. return ast.WalkContinue
  305. }
  306. if ast.NodeIFrame == n.Type {
  307. index := bytes.Index(n.Tokens, []byte("src=\""))
  308. if 0 > index {
  309. n.InsertBefore(&ast.Node{Type: ast.NodeText, Tokens: n.Tokens})
  310. } else {
  311. src := n.Tokens[index+len("src=\""):]
  312. src = src[:bytes.Index(src, []byte("\""))]
  313. src = html.UnescapeHTML(src)
  314. link := &ast.Node{Type: ast.NodeLink}
  315. link.AppendChild(&ast.Node{Type: ast.NodeOpenBracket})
  316. link.AppendChild(&ast.Node{Type: ast.NodeText, Tokens: src})
  317. link.AppendChild(&ast.Node{Type: ast.NodeCloseBracket})
  318. link.AppendChild(&ast.Node{Type: ast.NodeOpenParen})
  319. link.AppendChild(&ast.Node{Type: ast.NodeLinkDest, Tokens: src})
  320. link.AppendChild(&ast.Node{Type: ast.NodeCloseParen})
  321. n.InsertBefore(link)
  322. }
  323. unlinks = append(unlinks, n)
  324. }
  325. return ast.WalkContinue
  326. })
  327. for _, n := range unlinks {
  328. n.Unlink()
  329. }
  330. }
  331. func AddPDFOutline(id, p string) (err error) {
  332. inFile := p
  333. links, err := api.ListToCLinks(inFile)
  334. if nil != err {
  335. return
  336. }
  337. sort.Slice(links, func(i, j int) bool {
  338. return links[i].Page < links[j].Page
  339. })
  340. bms := map[string]*pdfcpu.Bookmark{}
  341. for _, link := range links {
  342. linkID := link.URI[strings.LastIndex(link.URI, "/")+1:]
  343. title := sql.GetBlock(linkID).Content
  344. title, _ = url.QueryUnescape(title)
  345. bm := &pdfcpu.Bookmark{
  346. Title: title,
  347. PageFrom: link.Page,
  348. AbsPos: link.Rect.UR.Y,
  349. }
  350. bms[linkID] = bm
  351. }
  352. if 1 > len(bms) {
  353. return
  354. }
  355. tree, _ := loadTreeByBlockID(id)
  356. if nil == tree {
  357. return
  358. }
  359. var headings []*ast.Node
  360. ast.Walk(tree.Root, func(n *ast.Node, entering bool) ast.WalkStatus {
  361. if entering && ast.NodeHeading == n.Type && !n.ParentIs(ast.NodeBlockquote) {
  362. headings = append(headings, n)
  363. return ast.WalkSkipChildren
  364. }
  365. return ast.WalkContinue
  366. })
  367. var topBms []*pdfcpu.Bookmark
  368. stack := linkedliststack.New()
  369. for _, h := range headings {
  370. L:
  371. for ; ; stack.Pop() {
  372. cur, ok := stack.Peek()
  373. if !ok {
  374. bm := bms[h.ID]
  375. bm.Level = h.HeadingLevel
  376. stack.Push(bm)
  377. topBms = append(topBms, bm)
  378. break L
  379. }
  380. tip := cur.(*pdfcpu.Bookmark)
  381. if tip.Level < h.HeadingLevel {
  382. bm := bms[h.ID]
  383. bm.Level = h.HeadingLevel
  384. bm.Parent = tip
  385. tip.Children = append(tip.Children, bm)
  386. stack.Push(bm)
  387. break L
  388. }
  389. }
  390. }
  391. outFile := inFile + ".tmp"
  392. err = api.AddBookmarksFile(inFile, outFile, topBms, nil)
  393. if nil != err {
  394. util.LogErrorf("add bookmark failed: %s", err)
  395. return
  396. }
  397. err = os.Rename(outFile, inFile)
  398. return
  399. }
  400. func CopyStdMarkdown(id string) string {
  401. tree, _ := loadTreeByBlockID(id)
  402. tree = exportTree(tree, false)
  403. luteEngine := NewLute()
  404. luteEngine.SetFootnotes(true)
  405. luteEngine.SetKramdownIAL(false)
  406. if IsSubscriber() {
  407. // 订阅用户使用云端图床服务
  408. luteEngine.RenderOptions.LinkBase = "https://assets.b3logfile.com/siyuan/" + Conf.User.UserId + "/"
  409. }
  410. return treenode.FormatNode(tree.Root, luteEngine)
  411. }
  412. func ExportMarkdown(id string) (name, zipPath string) {
  413. block := treenode.GetBlockTree(id)
  414. if nil == block {
  415. util.LogErrorf("not found block [%s]", id)
  416. return
  417. }
  418. boxID := block.BoxID
  419. box := Conf.Box(boxID)
  420. baseFolderName := path.Base(block.HPath)
  421. if "." == baseFolderName {
  422. baseFolderName = path.Base(block.Path)
  423. }
  424. docPaths := []string{block.Path}
  425. docFiles := box.ListFiles(strings.TrimSuffix(block.Path, ".sy"))
  426. for _, docFile := range docFiles {
  427. docPaths = append(docPaths, docFile.path)
  428. }
  429. zipPath = exportMarkdownZip(boxID, baseFolderName, docPaths)
  430. name = strings.TrimSuffix(filepath.Base(block.Path), ".sy")
  431. return
  432. }
  433. func BatchExportMarkdown(boxID, folderPath string) (zipPath string) {
  434. box := Conf.Box(boxID)
  435. var baseFolderName string
  436. if "/" == folderPath {
  437. baseFolderName = box.Name
  438. } else {
  439. block := treenode.GetBlockTreeRootByHPath(box.ID, folderPath)
  440. if nil == block {
  441. util.LogErrorf("not found block")
  442. return
  443. }
  444. baseFolderName = path.Base(block.HPath)
  445. }
  446. if "" == baseFolderName {
  447. baseFolderName = "Untitled"
  448. }
  449. docFiles := box.ListFiles(folderPath)
  450. var docPaths []string
  451. for _, docFile := range docFiles {
  452. docPaths = append(docPaths, docFile.path)
  453. }
  454. zipPath = exportMarkdownZip(boxID, baseFolderName, docPaths)
  455. return
  456. }
  457. func exportMarkdownZip(boxID, baseFolderName string, docPaths []string) (zipPath string) {
  458. dir, name := path.Split(baseFolderName)
  459. name = util.FilterFileName(name)
  460. if strings.HasSuffix(name, "..") {
  461. // 文档标题以 `..` 结尾时无法导出 Markdown https://github.com/siyuan-note/siyuan/issues/4698
  462. // 似乎是 os.MkdirAll 的 bug,以 .. 结尾的路径无法创建,所以这里加上 _ 结尾
  463. name += "_"
  464. }
  465. baseFolderName = path.Join(dir, name)
  466. box := Conf.Box(boxID)
  467. exportFolder := filepath.Join(util.TempDir, "export", baseFolderName)
  468. if err := os.MkdirAll(exportFolder, 0755); nil != err {
  469. util.LogErrorf("create export temp folder failed: %s", err)
  470. return
  471. }
  472. copiedAssets := hashset.New()
  473. luteEngine := util.NewLute()
  474. for _, p := range docPaths {
  475. docIAL := box.docIAL(p)
  476. if nil == docIAL {
  477. continue
  478. }
  479. id := docIAL["id"]
  480. hPath, md := exportMarkdownContent(id)
  481. dir, name = path.Split(hPath)
  482. dir = util.FilterFilePath(dir) // 导出文档时未移除不支持的文件名符号 https://github.com/siyuan-note/siyuan/issues/4590
  483. name = util.FilterFileName(name)
  484. hPath = path.Join(dir, name)
  485. p = hPath + ".md"
  486. writePath := filepath.Join(exportFolder, p)
  487. if gulu.File.IsExist(writePath) {
  488. // 重名文档加 ID
  489. p = hPath + "-" + id + ".md"
  490. writePath = filepath.Join(exportFolder, p)
  491. }
  492. writeFolder := filepath.Dir(writePath)
  493. if err := os.MkdirAll(writeFolder, 0755); nil != err {
  494. util.LogErrorf("create export temp folder [%s] failed: %s", writeFolder, err)
  495. continue
  496. }
  497. if err := gulu.File.WriteFileSafer(writePath, gulu.Str.ToBytes(md), 0644); nil != err {
  498. util.LogErrorf("write export markdown file [%s] failed: %s", writePath, err)
  499. continue
  500. }
  501. // 解析导出后的标准 Markdown,汇总 assets
  502. tree := parse.Parse("", gulu.Str.ToBytes(md), luteEngine.ParseOptions)
  503. var assets []string
  504. assets = append(assets, assetsLinkDestsInTree(tree)...)
  505. for _, asset := range assets {
  506. asset = string(html.DecodeDestination([]byte(asset)))
  507. if strings.Contains(asset, "?") {
  508. asset = asset[:strings.LastIndex(asset, "?")]
  509. }
  510. if copiedAssets.Contains(asset) {
  511. continue
  512. }
  513. srcPath, err := GetAssetAbsPath(asset)
  514. if nil != err {
  515. util.LogWarnf("get asset [%s] abs path failed: %s", asset, err)
  516. continue
  517. }
  518. destPath := filepath.Join(writeFolder, asset)
  519. if gulu.File.IsDir(srcPath) {
  520. err = gulu.File.Copy(srcPath, destPath)
  521. } else {
  522. err = gulu.File.CopyFile(srcPath, destPath)
  523. }
  524. if nil != err {
  525. util.LogErrorf("copy asset from [%s] to [%s] failed: %s", srcPath, destPath, err)
  526. continue
  527. }
  528. copiedAssets.Add(asset)
  529. }
  530. }
  531. zipPath = exportFolder + ".zip"
  532. zip, err := gulu.Zip.Create(zipPath)
  533. if nil != err {
  534. util.LogErrorf("create export markdown zip [%s] failed: %s", exportFolder, err)
  535. return ""
  536. }
  537. if err = zip.AddDirectory(baseFolderName, exportFolder); nil != err {
  538. util.LogErrorf("create export markdown zip [%s] failed: %s", exportFolder, err)
  539. return ""
  540. }
  541. if err = zip.Close(); nil != err {
  542. util.LogErrorf("close export markdown zip failed: %s", err)
  543. }
  544. os.RemoveAll(exportFolder)
  545. zipPath = "/export/" + url.PathEscape(filepath.Base(zipPath))
  546. return
  547. }
  548. func exportSYZip(boxID, rootDirPath, baseFolderName string, docPaths []string) (zipPath string) {
  549. dir, name := path.Split(baseFolderName)
  550. name = util.FilterFileName(name)
  551. if strings.HasSuffix(name, "..") {
  552. // 文档标题以 `..` 结尾时无法导出 Markdown https://github.com/siyuan-note/siyuan/issues/4698
  553. // 似乎是 os.MkdirAll 的 bug,以 .. 结尾的路径无法创建,所以这里加上 _ 结尾
  554. name += "_"
  555. }
  556. baseFolderName = path.Join(dir, name)
  557. box := Conf.Box(boxID)
  558. exportFolder := filepath.Join(util.TempDir, "export", baseFolderName)
  559. if err := os.MkdirAll(exportFolder, 0755); nil != err {
  560. util.LogErrorf("create export temp folder failed: %s", err)
  561. return
  562. }
  563. trees := map[string]*parse.Tree{}
  564. refTrees := map[string]*parse.Tree{}
  565. for _, p := range docPaths {
  566. docIAL := box.docIAL(p)
  567. if nil == docIAL {
  568. continue
  569. }
  570. id := docIAL["id"]
  571. tree, err := loadTreeByBlockID(id)
  572. if nil != err {
  573. continue
  574. }
  575. trees[tree.ID] = tree
  576. }
  577. for _, tree := range trees {
  578. refs := exportRefTrees(tree)
  579. for refTreeID, refTree := range refs {
  580. if nil == trees[refTreeID] {
  581. refTrees[refTreeID] = refTree
  582. }
  583. }
  584. }
  585. // 按文件夹结构复制选择的树
  586. for _, tree := range trees {
  587. readPath := filepath.Join(util.DataDir, tree.Box, tree.Path)
  588. data, readErr := filesys.NoLockFileRead(readPath)
  589. if nil != readErr {
  590. util.LogErrorf("read file [%s] failed: %s", readPath, readErr)
  591. continue
  592. }
  593. writePath := strings.TrimPrefix(tree.Path, rootDirPath)
  594. writePath = filepath.Join(exportFolder, writePath)
  595. writeFolder := filepath.Dir(writePath)
  596. if mkdirErr := os.MkdirAll(writeFolder, 0755); nil != mkdirErr {
  597. util.LogErrorf("create export temp folder [%s] failed: %s", writeFolder, mkdirErr)
  598. continue
  599. }
  600. if writeErr := os.WriteFile(writePath, data, 0644); nil != writeErr {
  601. util.LogErrorf("write export file [%s] failed: %s", writePath, writeErr)
  602. continue
  603. }
  604. }
  605. // 引用树放在导出文件夹根路径下
  606. for treeID, tree := range refTrees {
  607. readPath := filepath.Join(util.DataDir, tree.Box, tree.Path)
  608. data, readErr := filesys.NoLockFileRead(readPath)
  609. if nil != readErr {
  610. util.LogErrorf("read file [%s] failed: %s", readPath, readErr)
  611. continue
  612. }
  613. writePath := strings.TrimPrefix(tree.Path, rootDirPath)
  614. writePath = filepath.Join(exportFolder, treeID+".sy")
  615. if writeErr := os.WriteFile(writePath, data, 0644); nil != writeErr {
  616. util.LogErrorf("write export file [%s] failed: %s", writePath, writeErr)
  617. continue
  618. }
  619. }
  620. // 将引用树合并到选择树中,以便后面一次性导出资源文件
  621. for treeID, tree := range refTrees {
  622. trees[treeID] = tree
  623. }
  624. // 导出引用的资源文件
  625. copiedAssets := hashset.New()
  626. for _, tree := range trees {
  627. var assets []string
  628. assets = append(assets, assetsLinkDestsInTree(tree)...)
  629. for _, asset := range assets {
  630. asset = string(html.DecodeDestination([]byte(asset)))
  631. if strings.Contains(asset, "?") {
  632. asset = asset[:strings.LastIndex(asset, "?")]
  633. }
  634. if copiedAssets.Contains(asset) {
  635. continue
  636. }
  637. srcPath, assetErr := GetAssetAbsPath(asset)
  638. if nil != assetErr {
  639. util.LogWarnf("get asset [%s] abs path failed: %s", asset, assetErr)
  640. continue
  641. }
  642. destPath := filepath.Join(exportFolder, asset)
  643. if gulu.File.IsDir(srcPath) {
  644. assetErr = gulu.File.Copy(srcPath, destPath)
  645. } else {
  646. assetErr = gulu.File.CopyFile(srcPath, destPath)
  647. }
  648. if nil != assetErr {
  649. util.LogErrorf("copy asset from [%s] to [%s] failed: %s", srcPath, destPath, assetErr)
  650. continue
  651. }
  652. copiedAssets.Add(asset)
  653. }
  654. }
  655. zipPath = exportFolder + ".sy.zip"
  656. zip, err := gulu.Zip.Create(zipPath)
  657. if nil != err {
  658. util.LogErrorf("create export markdown zip [%s] failed: %s", exportFolder, err)
  659. return ""
  660. }
  661. if err = zip.AddDirectory(baseFolderName, exportFolder); nil != err {
  662. util.LogErrorf("create export markdown zip [%s] failed: %s", exportFolder, err)
  663. return ""
  664. }
  665. if err = zip.Close(); nil != err {
  666. util.LogErrorf("close export markdown zip failed: %s", err)
  667. }
  668. os.RemoveAll(exportFolder)
  669. zipPath = "/export/" + url.PathEscape(filepath.Base(zipPath))
  670. return
  671. }
  672. func ExportMarkdownContent(id string) (hPath, exportedMd string) {
  673. return exportMarkdownContent(id)
  674. }
  675. func exportMarkdownContent(id string) (hPath, exportedMd string) {
  676. tree, _ := loadTreeByBlockID(id)
  677. hPath = tree.HPath
  678. tree = exportTree(tree, false)
  679. luteEngine := NewLute()
  680. luteEngine.SetFootnotes(true)
  681. luteEngine.SetKramdownIAL(false)
  682. exportedMd = formatExportMd(tree.Root, luteEngine.ParseOptions, luteEngine.RenderOptions)
  683. return
  684. }
  685. func formatExportMd(node *ast.Node, parseOptions *parse.Options, renderOptions *render.Options) string {
  686. root := &ast.Node{Type: ast.NodeDocument}
  687. tree := &parse.Tree{Root: root, Context: &parse.Context{ParseOption: parseOptions}}
  688. renderer := render.NewFormatRenderer(tree, renderOptions)
  689. renderer.Writer = &bytes.Buffer{}
  690. renderer.Writer.Grow(4096)
  691. renderer.NodeWriterStack = append(renderer.NodeWriterStack, renderer.Writer)
  692. ast.Walk(node, func(n *ast.Node, entering bool) ast.WalkStatus {
  693. switch n.Type {
  694. case ast.NodeParagraph: // 段落需要单独处理。导出为 Markdown 时段落开头空两格不生效 https://github.com/siyuan-note/siyuan/issues/3167
  695. return renderExportMdParagraph(renderer, n, entering)
  696. case ast.NodeMathBlockContent:
  697. return renderExportMdMathBlockContent(renderer, n, entering)
  698. case ast.NodeInlineMathContent:
  699. return renderExportMdInlineMathContent(renderer, n, entering)
  700. case ast.NodeCodeBlockCode:
  701. return renderExportMdCodeBlockCode(renderer, n, entering)
  702. default:
  703. rendererFunc := renderer.RendererFuncs[n.Type]
  704. return rendererFunc(n, entering)
  705. }
  706. })
  707. return gulu.Str.FromBytes(renderer.Writer.Bytes())
  708. }
  709. func renderExportMdCodeBlockCode(r *render.FormatRenderer, node *ast.Node, entering bool) ast.WalkStatus {
  710. if entering {
  711. tokens := node.Tokens
  712. info := node.Parent.ChildByType(ast.NodeCodeBlockFenceInfoMarker)
  713. if nil != info &&
  714. bytes.Contains(info.CodeBlockInfo, []byte("flowchart")) ||
  715. bytes.Contains(info.CodeBlockInfo, []byte("mermaid")) ||
  716. bytes.Contains(info.CodeBlockInfo, []byte("graphviz")) ||
  717. bytes.Contains(info.CodeBlockInfo, []byte("plantuml")) {
  718. tokens = html.UnescapeHTML(tokens)
  719. }
  720. r.Write(tokens)
  721. }
  722. return ast.WalkContinue
  723. }
  724. func renderExportMdMathBlockContent(r *render.FormatRenderer, node *ast.Node, entering bool) ast.WalkStatus {
  725. if entering {
  726. tokens := html.UnescapeHTML(node.Tokens)
  727. r.Write(tokens)
  728. r.WriteByte(lex.ItemNewline)
  729. }
  730. return ast.WalkContinue
  731. }
  732. func renderExportMdInlineMathContent(r *render.FormatRenderer, node *ast.Node, entering bool) ast.WalkStatus {
  733. if entering {
  734. tokens := html.UnescapeHTML(node.Tokens)
  735. r.Write(tokens)
  736. }
  737. return ast.WalkContinue
  738. }
  739. func renderExportMdParagraph(r *render.FormatRenderer, node *ast.Node, entering bool) ast.WalkStatus {
  740. if entering {
  741. if r.Options.ChineseParagraphBeginningSpace && ast.NodeDocument == node.Parent.Type {
  742. r.WriteString("  ")
  743. }
  744. } else {
  745. if !r.Options.KeepParagraphBeginningSpace && nil != node.FirstChild {
  746. node.FirstChild.Tokens = bytes.TrimSpace(node.FirstChild.Tokens)
  747. }
  748. if node.ParentIs(ast.NodeTableCell) {
  749. if nil != node.Next && ast.NodeText != node.Next.Type {
  750. r.WriteString("<br /><br />")
  751. }
  752. return ast.WalkContinue
  753. }
  754. if withoutKramdownBlockIAL(r, node) {
  755. r.Newline()
  756. }
  757. inTightList := false
  758. lastListItemLastPara := false
  759. if parent := node.Parent; nil != parent {
  760. if ast.NodeListItem == parent.Type { // ListItem.Paragraph
  761. listItem := parent
  762. if nil != listItem.Parent && nil != listItem.Parent.ListData {
  763. // 必须通过列表(而非列表项)上的紧凑标识判断,因为在设置该标识时仅设置了 List.Tight
  764. // 设置紧凑标识的具体实现可参考函数 List.Finalize()
  765. inTightList = listItem.Parent.ListData.Tight
  766. if nextItem := listItem.Next; nil == nextItem {
  767. nextPara := node.Next
  768. lastListItemLastPara = nil == nextPara
  769. }
  770. } else {
  771. inTightList = true
  772. }
  773. }
  774. }
  775. if !inTightList || (lastListItemLastPara) {
  776. if withoutKramdownBlockIAL(r, node) {
  777. r.WriteByte(lex.ItemNewline)
  778. }
  779. }
  780. }
  781. return ast.WalkContinue
  782. }
  783. func withoutKramdownBlockIAL(r *render.FormatRenderer, node *ast.Node) bool {
  784. return !r.Options.KramdownBlockIAL || 0 == len(node.KramdownIAL)
  785. }
  786. func exportTree(tree *parse.Tree, wysiwyg bool) (ret *parse.Tree) {
  787. luteEngine := NewLute()
  788. ret = tree
  789. id := tree.Root.ID
  790. var unlinks []*ast.Node
  791. // 解析查询嵌入节点
  792. ast.Walk(ret.Root, func(n *ast.Node, entering bool) ast.WalkStatus {
  793. if !entering || ast.NodeBlockQueryEmbed != n.Type {
  794. return ast.WalkContinue
  795. }
  796. var defMd string
  797. stmt := n.ChildByType(ast.NodeBlockQueryEmbedScript).TokensStr()
  798. stmt = html.UnescapeString(stmt)
  799. blocks := searchEmbedBlock(stmt, nil, 0)
  800. if 1 > len(blocks) {
  801. return ast.WalkContinue
  802. }
  803. defMdBuf := bytes.Buffer{}
  804. for _, def := range blocks {
  805. defMdBuf.WriteString(renderBlockMarkdownR(def.ID))
  806. defMdBuf.WriteString("\n\n")
  807. }
  808. defMd = defMdBuf.String()
  809. buf := &bytes.Buffer{}
  810. lines := strings.Split(defMd, "\n")
  811. for i, line := range lines {
  812. if 0 == Conf.Export.BlockEmbedMode { // 原始文本
  813. buf.WriteString(line)
  814. } else { // Blockquote
  815. buf.WriteString("> " + line)
  816. }
  817. if i < len(lines)-1 {
  818. buf.WriteString("\n")
  819. }
  820. }
  821. buf.WriteString("\n\n")
  822. refTree := parse.Parse("", buf.Bytes(), luteEngine.ParseOptions)
  823. var children []*ast.Node
  824. for c := refTree.Root.FirstChild; nil != c; c = c.Next {
  825. children = append(children, c)
  826. }
  827. for _, c := range children {
  828. if ast.NodeDocument == c.Type {
  829. continue
  830. }
  831. n.InsertBefore(c)
  832. }
  833. unlinks = append(unlinks, n)
  834. return ast.WalkSkipChildren
  835. })
  836. for _, n := range unlinks {
  837. n.Unlink()
  838. }
  839. unlinks = nil
  840. // 收集引用转脚注
  841. var refFootnotes []*refAsFootnotes
  842. if 4 == Conf.Export.BlockRefMode { // 块引转脚注
  843. treeCache := map[string]*parse.Tree{}
  844. treeCache[id] = ret
  845. depth := 0
  846. collectFootnotesDefs(ret.ID, &refFootnotes, &treeCache, &depth)
  847. }
  848. ast.Walk(ret.Root, func(n *ast.Node, entering bool) ast.WalkStatus {
  849. if !entering {
  850. return ast.WalkContinue
  851. }
  852. switch n.Type {
  853. case ast.NodeTagOpenMarker: // 配置标签开始标记符
  854. if !wysiwyg {
  855. n.Type = ast.NodeText
  856. n.Tokens = []byte(Conf.Export.TagOpenMarker)
  857. return ast.WalkContinue
  858. }
  859. case ast.NodeTagCloseMarker: // 配置标记结束标记符
  860. if !wysiwyg {
  861. n.Type = ast.NodeText
  862. n.Tokens = []byte(Conf.Export.TagCloseMarker)
  863. return ast.WalkContinue
  864. }
  865. case ast.NodeSuperBlockOpenMarker, ast.NodeSuperBlockLayoutMarker, ast.NodeSuperBlockCloseMarker:
  866. if !wysiwyg {
  867. unlinks = append(unlinks, n)
  868. return ast.WalkContinue
  869. }
  870. case ast.NodeHeading:
  871. n.HeadingNormalizedID = n.IALAttr("id")
  872. n.ID = n.HeadingNormalizedID
  873. case ast.NodeInlineMathContent, ast.NodeMathBlockContent:
  874. n.Tokens = bytes.TrimSpace(n.Tokens) // 导出 Markdown 时去除公式内容中的首尾空格 https://github.com/siyuan-note/siyuan/issues/4666
  875. return ast.WalkContinue
  876. case ast.NodeFileAnnotationRef:
  877. refIDNode := n.ChildByType(ast.NodeFileAnnotationRefID)
  878. if nil == refIDNode {
  879. return ast.WalkSkipChildren
  880. }
  881. refID := refIDNode.TokensStr()
  882. p := refID[:strings.LastIndex(refID, "/")]
  883. absPath, err := GetAssetAbsPath(p)
  884. if nil != err {
  885. util.LogWarnf("get assets abs path by rel path [%s] failed: %s", p, err)
  886. return ast.WalkSkipChildren
  887. }
  888. sya := absPath + ".sya"
  889. syaData, err := os.ReadFile(sya)
  890. if nil != err {
  891. util.LogErrorf("read file [%s] failed: %s", sya, err)
  892. return ast.WalkSkipChildren
  893. }
  894. syaJSON := map[string]interface{}{}
  895. if err = gulu.JSON.UnmarshalJSON(syaData, &syaJSON); nil != err {
  896. util.LogErrorf("unmarshal file [%s] failed: %s", sya, err)
  897. return ast.WalkSkipChildren
  898. }
  899. annotationID := refID[strings.LastIndex(refID, "/")+1:]
  900. annotationData := syaJSON[annotationID]
  901. if nil == annotationData {
  902. util.LogErrorf("not found annotation [%s] in .sya", annotationID)
  903. return ast.WalkSkipChildren
  904. }
  905. pages := annotationData.(map[string]interface{})["pages"].([]interface{})
  906. page := int(pages[0].(map[string]interface{})["index"].(float64)) + 1
  907. pageStr := strconv.Itoa(page)
  908. refTextNode := n.ChildByType(ast.NodeFileAnnotationRefText)
  909. if nil == refTextNode {
  910. return ast.WalkSkipChildren
  911. }
  912. refText := refTextNode.TokensStr()
  913. ext := filepath.Ext(p)
  914. file := p[7:len(p)-23-len(ext)] + ext
  915. fileAnnotationRefLink := &ast.Node{Type: ast.NodeLink}
  916. fileAnnotationRefLink.AppendChild(&ast.Node{Type: ast.NodeOpenBracket})
  917. if 0 == Conf.Export.FileAnnotationRefMode {
  918. fileAnnotationRefLink.AppendChild(&ast.Node{Type: ast.NodeLinkText, Tokens: []byte(file + " - p" + pageStr + " - " + refText)})
  919. } else {
  920. fileAnnotationRefLink.AppendChild(&ast.Node{Type: ast.NodeLinkText, Tokens: []byte(refText)})
  921. }
  922. fileAnnotationRefLink.AppendChild(&ast.Node{Type: ast.NodeCloseBracket})
  923. fileAnnotationRefLink.AppendChild(&ast.Node{Type: ast.NodeOpenParen})
  924. fileAnnotationRefLink.AppendChild(&ast.Node{Type: ast.NodeLinkDest, Tokens: []byte(p + "?p=" + pageStr)})
  925. fileAnnotationRefLink.AppendChild(&ast.Node{Type: ast.NodeCloseParen})
  926. n.InsertBefore(fileAnnotationRefLink)
  927. unlinks = append(unlinks, n)
  928. return ast.WalkSkipChildren
  929. }
  930. if ast.NodeBlockRef != n.Type {
  931. return ast.WalkContinue
  932. }
  933. // 处理引用节点
  934. var linkText string
  935. id := n.ChildByType(ast.NodeBlockRefID).TokensStr()
  936. if anchor := n.ChildByType(ast.NodeBlockRefText); nil != anchor {
  937. linkText = anchor.Text()
  938. } else if anchor = n.ChildByType(ast.NodeBlockRefDynamicText); nil != anchor {
  939. linkText = anchor.Text()
  940. } else {
  941. linkText = sql.GetRefText(id)
  942. }
  943. if Conf.Editor.BlockRefDynamicAnchorTextMaxLen < utf8.RuneCountInString(linkText) {
  944. linkText = gulu.Str.SubStr(linkText, Conf.Editor.BlockRefDynamicAnchorTextMaxLen) + "..."
  945. }
  946. linkText = Conf.Export.BlockRefTextLeft + linkText + Conf.Export.BlockRefTextRight
  947. defTree, _ := loadTreeByBlockID(id)
  948. if nil == defTree {
  949. return ast.WalkContinue
  950. }
  951. switch Conf.Export.BlockRefMode {
  952. case 2: // 锚文本块链
  953. var blockRefLink *ast.Node
  954. blockRefLink = &ast.Node{Type: ast.NodeLink}
  955. blockRefLink.AppendChild(&ast.Node{Type: ast.NodeOpenBracket})
  956. blockRefLink.AppendChild(&ast.Node{Type: ast.NodeLinkText, Tokens: []byte(linkText)})
  957. blockRefLink.AppendChild(&ast.Node{Type: ast.NodeCloseBracket})
  958. blockRefLink.AppendChild(&ast.Node{Type: ast.NodeOpenParen})
  959. blockRefLink.AppendChild(&ast.Node{Type: ast.NodeLinkDest, Tokens: []byte("siyuan://blocks/" + id)})
  960. blockRefLink.AppendChild(&ast.Node{Type: ast.NodeCloseParen})
  961. n.InsertBefore(blockRefLink)
  962. case 3: // 仅锚文本
  963. n.InsertBefore(&ast.Node{Type: ast.NodeText, Tokens: []byte(linkText)})
  964. case 4: // 脚注
  965. defID := n.ChildByType(ast.NodeBlockRefID).TokensStr()
  966. refFoot := getRefAsFootnotes(defID, &refFootnotes)
  967. n.InsertBefore(&ast.Node{Type: ast.NodeText, Tokens: []byte(linkText)})
  968. n.InsertBefore(&ast.Node{Type: ast.NodeFootnotesRef, Tokens: []byte("^" + refFoot.refNum), FootnotesRefId: refFoot.refNum, FootnotesRefLabel: []byte("^" + refFoot.refNum)})
  969. }
  970. unlinks = append(unlinks, n)
  971. return ast.WalkSkipChildren
  972. })
  973. for _, n := range unlinks {
  974. n.Unlink()
  975. }
  976. if 4 == Conf.Export.BlockRefMode { // 块引转脚注
  977. if footnotesDefBlock := resolveFootnotesDefs(&refFootnotes, ret.Root.ID); nil != footnotesDefBlock {
  978. ret.Root.AppendChild(footnotesDefBlock)
  979. }
  980. }
  981. if Conf.Export.AddTitle {
  982. if root, _ := getBlock(id); nil != root {
  983. title := &ast.Node{Type: ast.NodeHeading, HeadingLevel: 1}
  984. title.AppendChild(&ast.Node{Type: ast.NodeText, Tokens: []byte(root.Content)})
  985. ret.Root.PrependChild(title)
  986. }
  987. }
  988. // 导出时支持导出题头图 https://github.com/siyuan-note/siyuan/issues/4372
  989. titleImgPath := treenode.GetDocTitleImgPath(ret.Root)
  990. if "" != titleImgPath {
  991. p := &ast.Node{Type: ast.NodeParagraph}
  992. titleImg := &ast.Node{Type: ast.NodeImage}
  993. titleImg.AppendChild(&ast.Node{Type: ast.NodeBang})
  994. titleImg.AppendChild(&ast.Node{Type: ast.NodeOpenBracket})
  995. titleImg.AppendChild(&ast.Node{Type: ast.NodeLinkText, Tokens: []byte("image")})
  996. titleImg.AppendChild(&ast.Node{Type: ast.NodeCloseBracket})
  997. titleImg.AppendChild(&ast.Node{Type: ast.NodeOpenParen})
  998. titleImg.AppendChild(&ast.Node{Type: ast.NodeLinkDest, Tokens: []byte(titleImgPath)})
  999. titleImg.AppendChild(&ast.Node{Type: ast.NodeCloseParen})
  1000. p.AppendChild(titleImg)
  1001. ret.Root.PrependChild(p)
  1002. }
  1003. unlinks = nil
  1004. ast.Walk(ret.Root, func(n *ast.Node, entering bool) ast.WalkStatus {
  1005. if !entering {
  1006. return ast.WalkContinue
  1007. }
  1008. // 块折叠以后导出 HTML/PDF 固定展开 https://github.com/siyuan-note/siyuan/issues/4064
  1009. n.RemoveIALAttr("fold")
  1010. n.RemoveIALAttr("heading-fold")
  1011. if ast.NodeWidget == n.Type {
  1012. // 挂件块导出 https://github.com/siyuan-note/siyuan/issues/3834
  1013. exportMdVal := n.IALAttr("data-export-md")
  1014. exportMdVal = html.UnescapeString(exportMdVal) // 导出 `data-export-md` 时未解析代码块与行内代码内的转义字符 https://github.com/siyuan-note/siyuan/issues/4180
  1015. if "" != exportMdVal {
  1016. exportMdTree := parse.Parse("", []byte(exportMdVal), luteEngine.ParseOptions)
  1017. var insertNodes []*ast.Node
  1018. for c := exportMdTree.Root.FirstChild; nil != c; c = c.Next {
  1019. if ast.NodeKramdownBlockIAL != c.Type {
  1020. insertNodes = append(insertNodes, c)
  1021. }
  1022. }
  1023. for _, insertNode := range insertNodes {
  1024. n.InsertBefore(insertNode)
  1025. }
  1026. unlinks = append(unlinks, n)
  1027. }
  1028. return ast.WalkContinue
  1029. }
  1030. if ast.NodeText != n.Type {
  1031. return ast.WalkContinue
  1032. }
  1033. // Shift+Enter 换行在导出为 Markdown 时使用硬换行 https://github.com/siyuan-note/siyuan/issues/3458
  1034. n.Tokens = bytes.ReplaceAll(n.Tokens, []byte("\n"), []byte(" \n"))
  1035. return ast.WalkContinue
  1036. })
  1037. for _, n := range unlinks {
  1038. n.Unlink()
  1039. }
  1040. return ret
  1041. }
  1042. func resolveFootnotesDefs(refFootnotes *[]*refAsFootnotes, rootID string) (footnotesDefBlock *ast.Node) {
  1043. if 1 > len(*refFootnotes) {
  1044. return nil
  1045. }
  1046. footnotesDefBlock = &ast.Node{Type: ast.NodeFootnotesDefBlock}
  1047. for _, foot := range *refFootnotes {
  1048. t, err := loadTreeByBlockID(foot.defID)
  1049. if nil != err {
  1050. continue
  1051. }
  1052. defNode := treenode.GetNodeInTree(t, foot.defID)
  1053. var nodes []*ast.Node
  1054. if ast.NodeHeading == defNode.Type {
  1055. nodes = append(nodes, defNode)
  1056. children := treenode.HeadingChildren(defNode)
  1057. nodes = append(nodes, children...)
  1058. } else if ast.NodeDocument == defNode.Type {
  1059. docTitle := &ast.Node{ID: defNode.ID, Type: ast.NodeHeading, HeadingLevel: 1}
  1060. docTitle.AppendChild(&ast.Node{Type: ast.NodeText, Tokens: []byte(defNode.IALAttr("title"))})
  1061. nodes = append(nodes, docTitle)
  1062. for c := defNode.FirstChild; nil != c; c = c.Next {
  1063. nodes = append(nodes, c)
  1064. }
  1065. } else {
  1066. nodes = append(nodes, defNode)
  1067. }
  1068. var newNodes []*ast.Node
  1069. for _, node := range nodes {
  1070. var unlinks []*ast.Node
  1071. ast.Walk(node, func(n *ast.Node, entering bool) ast.WalkStatus {
  1072. if !entering {
  1073. return ast.WalkContinue
  1074. }
  1075. if ast.NodeBlockRef == n.Type {
  1076. defID := n.ChildByType(ast.NodeBlockRefID).TokensStr()
  1077. if f := getRefAsFootnotes(defID, refFootnotes); nil != f {
  1078. n.InsertBefore(&ast.Node{Type: ast.NodeText, Tokens: []byte(Conf.Export.BlockRefTextLeft + f.refAnchorText + Conf.Export.BlockRefTextRight)})
  1079. n.InsertBefore(&ast.Node{Type: ast.NodeFootnotesRef, Tokens: []byte("^" + f.refNum), FootnotesRefId: f.refNum, FootnotesRefLabel: []byte("^" + f.refNum)})
  1080. unlinks = append(unlinks, n)
  1081. }
  1082. return ast.WalkSkipChildren
  1083. } else if ast.NodeBlockQueryEmbed == n.Type {
  1084. stmt := n.ChildByType(ast.NodeBlockQueryEmbedScript).TokensStr()
  1085. stmt = html.UnescapeString(stmt)
  1086. sqlBlocks := sql.SelectBlocksRawStmt(stmt, Conf.Search.Limit)
  1087. depth := 0
  1088. for _, b := range sqlBlocks {
  1089. subNodes := renderBlockMarkdownR0(b.ID, &depth)
  1090. for _, subNode := range subNodes {
  1091. if ast.NodeListItem == subNode.Type {
  1092. parentList := &ast.Node{Type: ast.NodeList, ListData: &ast.ListData{Typ: subNode.ListData.Typ}}
  1093. parentList.AppendChild(subNode)
  1094. newNodes = append(newNodes, parentList)
  1095. } else {
  1096. newNodes = append(newNodes, subNode)
  1097. }
  1098. }
  1099. }
  1100. unlinks = append(unlinks, n)
  1101. return ast.WalkSkipChildren
  1102. }
  1103. return ast.WalkContinue
  1104. })
  1105. for _, n := range unlinks {
  1106. n.Unlink()
  1107. }
  1108. if ast.NodeBlockQueryEmbed != node.Type {
  1109. if ast.NodeListItem == node.Type {
  1110. parentList := &ast.Node{Type: ast.NodeList, ListData: &ast.ListData{Typ: node.ListData.Typ}}
  1111. parentList.AppendChild(node)
  1112. newNodes = append(newNodes, parentList)
  1113. } else {
  1114. newNodes = append(newNodes, node)
  1115. }
  1116. }
  1117. }
  1118. footnotesDef := &ast.Node{Type: ast.NodeFootnotesDef, Tokens: []byte("^" + foot.refNum), FootnotesRefId: foot.refNum, FootnotesRefLabel: []byte("^" + foot.refNum)}
  1119. for _, node := range newNodes {
  1120. ast.Walk(node, func(n *ast.Node, entering bool) ast.WalkStatus {
  1121. if !entering {
  1122. return ast.WalkContinue
  1123. }
  1124. if ast.NodeParagraph != n.Type {
  1125. return ast.WalkContinue
  1126. }
  1127. docID := strings.TrimSuffix(path.Base(n.Path), ".sy")
  1128. if rootID == docID { // 在同一个文档的话缩略显示 https://github.com/siyuan-note/siyuan/issues/3299
  1129. if text := sql.GetRefText(n.ID); 64 < utf8.RuneCountInString(text) {
  1130. var unlinkChildren []*ast.Node
  1131. for c := n.FirstChild; nil != c; c = c.Next {
  1132. unlinkChildren = append(unlinkChildren, c)
  1133. }
  1134. for _, c := range unlinkChildren {
  1135. c.Unlink()
  1136. }
  1137. text = gulu.Str.SubStr(text, 64) + "..."
  1138. n.AppendChild(&ast.Node{Type: ast.NodeText, Tokens: []byte(text)})
  1139. return ast.WalkSkipChildren
  1140. }
  1141. }
  1142. return ast.WalkContinue
  1143. })
  1144. footnotesDef.AppendChild(node)
  1145. }
  1146. footnotesDefBlock.AppendChild(footnotesDef)
  1147. }
  1148. return
  1149. }
  1150. func collectFootnotesDefs(id string, refFootnotes *[]*refAsFootnotes, treeCache *map[string]*parse.Tree, depth *int) {
  1151. *depth++
  1152. if 4096 < *depth {
  1153. return
  1154. }
  1155. b := treenode.GetBlockTree(id)
  1156. if nil == b {
  1157. return
  1158. }
  1159. t := (*treeCache)[b.RootID]
  1160. if nil == t {
  1161. var err error
  1162. if t, err = loadTreeByBlockID(b.ID); nil != err {
  1163. return
  1164. }
  1165. (*treeCache)[t.ID] = t
  1166. }
  1167. node := treenode.GetNodeInTree(t, b.ID)
  1168. if nil == node {
  1169. util.LogErrorf("not found node [%s] in tree [%s]", b.ID, t.Root.ID)
  1170. return
  1171. }
  1172. collectFootnotesDefs0(node, refFootnotes, treeCache, depth)
  1173. if ast.NodeHeading == node.Type {
  1174. children := treenode.HeadingChildren(node)
  1175. for _, c := range children {
  1176. collectFootnotesDefs0(c, refFootnotes, treeCache, depth)
  1177. }
  1178. }
  1179. return
  1180. }
  1181. func collectFootnotesDefs0(node *ast.Node, refFootnotes *[]*refAsFootnotes, treeCache *map[string]*parse.Tree, depth *int) {
  1182. ast.Walk(node, func(n *ast.Node, entering bool) ast.WalkStatus {
  1183. if !entering {
  1184. return ast.WalkContinue
  1185. }
  1186. if ast.NodeBlockRef == n.Type {
  1187. defID := n.ChildByType(ast.NodeBlockRefID).TokensStr()
  1188. if nil == getRefAsFootnotes(defID, refFootnotes) {
  1189. anchorText := sql.GetRefText(defID)
  1190. if Conf.Editor.BlockRefDynamicAnchorTextMaxLen < utf8.RuneCountInString(anchorText) {
  1191. anchorText = gulu.Str.SubStr(anchorText, Conf.Editor.BlockRefDynamicAnchorTextMaxLen) + "..."
  1192. }
  1193. *refFootnotes = append(*refFootnotes, &refAsFootnotes{
  1194. defID: defID,
  1195. refNum: strconv.Itoa(len(*refFootnotes) + 1),
  1196. refAnchorText: anchorText,
  1197. })
  1198. collectFootnotesDefs(defID, refFootnotes, treeCache, depth)
  1199. }
  1200. return ast.WalkSkipChildren
  1201. }
  1202. return ast.WalkContinue
  1203. })
  1204. }
  1205. func getRefAsFootnotes(defID string, slice *[]*refAsFootnotes) *refAsFootnotes {
  1206. for _, e := range *slice {
  1207. if e.defID == defID {
  1208. return e
  1209. }
  1210. }
  1211. return nil
  1212. }
  1213. type refAsFootnotes struct {
  1214. defID string
  1215. refNum string
  1216. refAnchorText string
  1217. }
  1218. func exportRefTrees(tree *parse.Tree) (ret map[string]*parse.Tree) {
  1219. ret = map[string]*parse.Tree{}
  1220. exportRefTrees0(tree, &ret)
  1221. return
  1222. }
  1223. func exportRefTrees0(tree *parse.Tree, retTrees *map[string]*parse.Tree) {
  1224. if nil != (*retTrees)[tree.ID] {
  1225. return
  1226. }
  1227. (*retTrees)[tree.ID] = tree
  1228. ast.Walk(tree.Root, func(n *ast.Node, entering bool) ast.WalkStatus {
  1229. if !entering {
  1230. return ast.WalkContinue
  1231. }
  1232. if ast.NodeBlockRef == n.Type {
  1233. defIDNode := n.ChildByType(ast.NodeBlockRefID)
  1234. if nil == defIDNode {
  1235. return ast.WalkSkipChildren
  1236. }
  1237. defID := defIDNode.TokensStr()
  1238. defBlock := treenode.GetBlockTree(defID)
  1239. if nil == defBlock {
  1240. return ast.WalkSkipChildren
  1241. }
  1242. defTree, err := loadTreeByBlockID(defBlock.RootID)
  1243. if nil != err {
  1244. return ast.WalkSkipChildren
  1245. }
  1246. exportRefTrees0(defTree, retTrees)
  1247. }
  1248. return ast.WalkContinue
  1249. })
  1250. }