export.go 84 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794
  1. // SiYuan - Refactor your thinking
  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. "encoding/csv"
  20. "errors"
  21. "fmt"
  22. "github.com/88250/pdfcpu/pkg/font"
  23. "net/http"
  24. "net/url"
  25. "os"
  26. "os/exec"
  27. "path"
  28. "path/filepath"
  29. "sort"
  30. "strconv"
  31. "strings"
  32. "time"
  33. "unicode/utf8"
  34. "github.com/88250/gulu"
  35. "github.com/88250/lute/ast"
  36. "github.com/88250/lute/editor"
  37. "github.com/88250/lute/html"
  38. "github.com/88250/lute/parse"
  39. "github.com/88250/lute/render"
  40. "github.com/88250/pdfcpu/pkg/api"
  41. "github.com/88250/pdfcpu/pkg/pdfcpu"
  42. "github.com/emirpasic/gods/sets/hashset"
  43. "github.com/emirpasic/gods/stacks/linkedliststack"
  44. "github.com/imroc/req/v3"
  45. "github.com/siyuan-note/filelock"
  46. "github.com/siyuan-note/httpclient"
  47. "github.com/siyuan-note/logging"
  48. "github.com/siyuan-note/riff"
  49. "github.com/siyuan-note/siyuan/kernel/av"
  50. "github.com/siyuan-note/siyuan/kernel/filesys"
  51. "github.com/siyuan-note/siyuan/kernel/sql"
  52. "github.com/siyuan-note/siyuan/kernel/treenode"
  53. "github.com/siyuan-note/siyuan/kernel/util"
  54. )
  55. func ExportAv2CSV(avID, blockID string) (zipPath string, err error) {
  56. // Database block supports export as CSV https://github.com/siyuan-note/siyuan/issues/10072
  57. attrView, err := av.ParseAttributeView(avID)
  58. if nil != err {
  59. return
  60. }
  61. node, _, err := getNodeByBlockID(nil, blockID)
  62. if nil == node {
  63. return
  64. }
  65. viewID := node.IALAttr(av.NodeAttrView)
  66. view, err := attrView.GetCurrentView(viewID)
  67. if nil != err {
  68. return
  69. }
  70. name := util.FilterFileName(attrView.Name)
  71. if "" == name {
  72. name = Conf.language(105)
  73. }
  74. table, err := renderAttributeViewTable(attrView, view, "")
  75. if nil != err {
  76. logging.LogErrorf("render attribute view [%s] table failed: %s", avID, err)
  77. return
  78. }
  79. // 遵循视图过滤和排序规则 Use filtering and sorting of current view settings when exporting database blocks https://github.com/siyuan-note/siyuan/issues/10474
  80. table.FilterRows(attrView)
  81. table.SortRows(attrView)
  82. exportFolder := filepath.Join(util.TempDir, "export", "csv", name)
  83. if err = os.MkdirAll(exportFolder, 0755); nil != err {
  84. logging.LogErrorf("mkdir [%s] failed: %s", exportFolder, err)
  85. return
  86. }
  87. csvPath := filepath.Join(exportFolder, name+".csv")
  88. f, err := os.OpenFile(csvPath, os.O_RDWR|os.O_CREATE|os.O_TRUNC, 0644)
  89. if nil != err {
  90. logging.LogErrorf("open [%s] failed: %s", csvPath, err)
  91. return
  92. }
  93. if _, err = f.WriteString("\xEF\xBB\xBF"); nil != err { // 写入 UTF-8 BOM,避免使用 Microsoft Excel 打开乱码
  94. logging.LogErrorf("write UTF-8 BOM to [%s] failed: %s", csvPath, err)
  95. f.Close()
  96. return
  97. }
  98. writer := csv.NewWriter(f)
  99. var header []string
  100. for _, col := range table.Columns {
  101. header = append(header, col.Name)
  102. }
  103. if err = writer.Write(header); nil != err {
  104. logging.LogErrorf("write csv header [%s] failed: %s", header, err)
  105. f.Close()
  106. return
  107. }
  108. for _, row := range table.Rows {
  109. var rowVal []string
  110. for _, cell := range row.Cells {
  111. var val string
  112. if nil != cell.Value {
  113. if av.KeyTypeDate == cell.Value.Type {
  114. if nil != cell.Value.Date {
  115. cell.Value.Date = av.NewFormattedValueDate(cell.Value.Date.Content, cell.Value.Date.Content2, av.DateFormatNone, cell.Value.Date.IsNotTime, cell.Value.Date.HasEndDate)
  116. }
  117. } else if av.KeyTypeCreated == cell.Value.Type {
  118. if nil != cell.Value.Created {
  119. cell.Value.Created = av.NewFormattedValueCreated(cell.Value.Created.Content, 0, av.CreatedFormatNone)
  120. }
  121. } else if av.KeyTypeUpdated == cell.Value.Type {
  122. if nil != cell.Value.Updated {
  123. cell.Value.Updated = av.NewFormattedValueUpdated(cell.Value.Updated.Content, 0, av.UpdatedFormatNone)
  124. }
  125. } else if av.KeyTypeMAsset == cell.Value.Type {
  126. if nil != cell.Value.MAsset {
  127. buf := &bytes.Buffer{}
  128. for _, a := range cell.Value.MAsset {
  129. buf.WriteString("![](")
  130. buf.WriteString(a.Content)
  131. buf.WriteString(") ")
  132. }
  133. val = strings.TrimSpace(buf.String())
  134. }
  135. }
  136. val = cell.Value.String()
  137. }
  138. rowVal = append(rowVal, val)
  139. }
  140. if err = writer.Write(rowVal); nil != err {
  141. logging.LogErrorf("write csv row [%s] failed: %s", rowVal, err)
  142. f.Close()
  143. return
  144. }
  145. }
  146. writer.Flush()
  147. zipPath = exportFolder + ".db.zip"
  148. zip, err := gulu.Zip.Create(zipPath)
  149. if nil != err {
  150. logging.LogErrorf("create export .db.zip [%s] failed: %s", exportFolder, err)
  151. f.Close()
  152. return
  153. }
  154. if err = zip.AddDirectory("", exportFolder); nil != err {
  155. logging.LogErrorf("create export .db.zip [%s] failed: %s", exportFolder, err)
  156. f.Close()
  157. return
  158. }
  159. if err = zip.Close(); nil != err {
  160. logging.LogErrorf("close export .db.zip failed: %s", err)
  161. f.Close()
  162. return
  163. }
  164. f.Close()
  165. removeErr := os.RemoveAll(exportFolder)
  166. if nil != removeErr {
  167. logging.LogErrorf("remove export folder [%s] failed: %s", exportFolder, removeErr)
  168. }
  169. zipPath = "/export/csv/" + url.PathEscape(filepath.Base(zipPath))
  170. return
  171. }
  172. func Export2Liandi(id string) (err error) {
  173. tree, err := LoadTreeByBlockID(id)
  174. if nil != err {
  175. logging.LogErrorf("load tree by block id [%s] failed: %s", id, err)
  176. return
  177. }
  178. if IsUserGuide(tree.Box) {
  179. // Doc in the user guide no longer supports one-click sending to the community https://github.com/siyuan-note/siyuan/issues/8388
  180. return errors.New(Conf.Language(204))
  181. }
  182. assets := assetsLinkDestsInTree(tree)
  183. embedAssets := assetsLinkDestsInQueryEmbedNodes(tree)
  184. assets = append(assets, embedAssets...)
  185. assets = gulu.Str.RemoveDuplicatedElem(assets)
  186. err = uploadAssets2Cloud(assets, bizTypeExport2Liandi)
  187. if nil != err {
  188. return
  189. }
  190. msgId := util.PushMsg(Conf.Language(182), 15000)
  191. defer util.PushClearMsg(msgId)
  192. // 判断帖子是否已经存在,存在则使用更新接口
  193. const liandiArticleIdAttrName = "custom-liandi-articleId"
  194. foundArticle := false
  195. articleId := tree.Root.IALAttr(liandiArticleIdAttrName)
  196. if "" != articleId {
  197. result := gulu.Ret.NewResult()
  198. request := httpclient.NewCloudRequest30s()
  199. resp, getErr := request.
  200. SetSuccessResult(result).
  201. SetCookies(&http.Cookie{Name: "symphony", Value: Conf.GetUser().UserToken}).
  202. Get(util.GetCloudAccountServer() + "/api/v2/article/update/" + articleId)
  203. if nil != getErr {
  204. logging.LogErrorf("get liandi article info failed: %s", getErr)
  205. return getErr
  206. }
  207. switch resp.StatusCode {
  208. case 200:
  209. if 0 == result.Code {
  210. foundArticle = true
  211. } else if 1 == result.Code {
  212. foundArticle = false
  213. }
  214. case 404:
  215. foundArticle = false
  216. default:
  217. msg := fmt.Sprintf("get liandi article info failed [sc=%d]", resp.StatusCode)
  218. err = errors.New(msg)
  219. return
  220. }
  221. }
  222. apiURL := util.GetCloudAccountServer() + "/api/v2/article"
  223. if foundArticle {
  224. apiURL += "/" + articleId
  225. }
  226. title := path.Base(tree.HPath)
  227. tags := tree.Root.IALAttr("tags")
  228. content := exportMarkdownContent0(tree, util.GetCloudForumAssetsServer()+time.Now().Format("2006/01")+"/siyuan/"+Conf.GetUser().UserId+"/", true,
  229. 4, 1, 0,
  230. "#", "#",
  231. "", "",
  232. false, nil)
  233. result := gulu.Ret.NewResult()
  234. request := httpclient.NewCloudRequest30s()
  235. request = request.
  236. SetSuccessResult(result).
  237. SetCookies(&http.Cookie{Name: "symphony", Value: Conf.GetUser().UserToken}).
  238. SetBody(map[string]interface{}{
  239. "articleTitle": title,
  240. "articleTags": tags,
  241. "articleContent": content})
  242. var resp *req.Response
  243. var sendErr error
  244. if foundArticle {
  245. resp, sendErr = request.Put(apiURL)
  246. } else {
  247. resp, sendErr = request.Post(apiURL)
  248. }
  249. if nil != sendErr {
  250. logging.LogErrorf("send article to liandi failed: %s", err)
  251. return err
  252. }
  253. if 200 != resp.StatusCode {
  254. msg := fmt.Sprintf("send article to liandi failed [sc=%d]", resp.StatusCode)
  255. logging.LogErrorf(msg)
  256. return errors.New(msg)
  257. }
  258. if 0 != result.Code {
  259. msg := fmt.Sprintf("send article to liandi failed [code=%d, msg=%s]", result.Code, result.Msg)
  260. logging.LogErrorf(msg)
  261. util.PushClearMsg(msgId)
  262. return errors.New(result.Msg)
  263. }
  264. if !foundArticle {
  265. articleId = result.Data.(string)
  266. tree, _ = LoadTreeByBlockID(id) // 这里必须重新加载,因为前面导出时已经修改了树结构
  267. tree.Root.SetIALAttr(liandiArticleIdAttrName, articleId)
  268. if err = writeTreeUpsertQueue(tree); nil != err {
  269. return
  270. }
  271. }
  272. msg := fmt.Sprintf(Conf.Language(181), util.GetCloudAccountServer()+"/article/"+articleId)
  273. util.PushMsg(msg, 7000)
  274. return
  275. }
  276. func ExportSystemLog() (zipPath string) {
  277. exportFolder := filepath.Join(util.TempDir, "export", "system-log")
  278. os.RemoveAll(exportFolder)
  279. if err := os.MkdirAll(exportFolder, 0755); nil != err {
  280. logging.LogErrorf("create export temp folder failed: %s", err)
  281. return
  282. }
  283. appLog := filepath.Join(util.HomeDir, ".config", "siyuan", "app.log")
  284. if gulu.File.IsExist(appLog) {
  285. to := filepath.Join(exportFolder, "app.log")
  286. if err := filelock.Copy(appLog, to); nil != err {
  287. logging.LogErrorf("copy app log from [%s] to [%s] failed: %s", err, appLog, to)
  288. }
  289. }
  290. kernelLog := filepath.Join(util.HomeDir, ".config", "siyuan", "kernel.log")
  291. if gulu.File.IsExist(kernelLog) {
  292. to := filepath.Join(exportFolder, "kernel.log")
  293. if err := filelock.Copy(kernelLog, to); nil != err {
  294. logging.LogErrorf("copy kernel log from [%s] to [%s] failed: %s", err, kernelLog, to)
  295. }
  296. }
  297. siyuanLog := filepath.Join(util.TempDir, "siyuan.log")
  298. if gulu.File.IsExist(siyuanLog) {
  299. to := filepath.Join(exportFolder, "siyuan.log")
  300. if err := filelock.Copy(siyuanLog, to); nil != err {
  301. logging.LogErrorf("copy kernel log from [%s] to [%s] failed: %s", err, siyuanLog, to)
  302. }
  303. }
  304. mobileLog := filepath.Join(util.TempDir, "mobile.log")
  305. if gulu.File.IsExist(mobileLog) {
  306. to := filepath.Join(exportFolder, "mobile.log")
  307. if err := filelock.Copy(mobileLog, to); nil != err {
  308. logging.LogErrorf("copy mobile log from [%s] to [%s] failed: %s", err, mobileLog, to)
  309. }
  310. }
  311. zipPath = exportFolder + ".zip"
  312. zip, err := gulu.Zip.Create(zipPath)
  313. if nil != err {
  314. logging.LogErrorf("create export log zip [%s] failed: %s", exportFolder, err)
  315. return ""
  316. }
  317. if err = zip.AddDirectory("log", exportFolder); nil != err {
  318. logging.LogErrorf("create export log zip [%s] failed: %s", exportFolder, err)
  319. return ""
  320. }
  321. if err = zip.Close(); nil != err {
  322. logging.LogErrorf("close export log zip failed: %s", err)
  323. }
  324. os.RemoveAll(exportFolder)
  325. zipPath = "/export/" + url.PathEscape(filepath.Base(zipPath))
  326. return
  327. }
  328. func ExportNotebookSY(id string) (zipPath string) {
  329. zipPath = exportBoxSYZip(id)
  330. return
  331. }
  332. func ExportSY(id string) (name, zipPath string) {
  333. block := treenode.GetBlockTree(id)
  334. if nil == block {
  335. logging.LogErrorf("not found block [%s]", id)
  336. return
  337. }
  338. boxID := block.BoxID
  339. box := Conf.Box(boxID)
  340. baseFolderName := path.Base(block.HPath)
  341. if "." == baseFolderName {
  342. baseFolderName = path.Base(block.Path)
  343. }
  344. rootPath := block.Path
  345. docPaths := []string{rootPath}
  346. docFiles := box.ListFiles(strings.TrimSuffix(block.Path, ".sy"))
  347. for _, docFile := range docFiles {
  348. docPaths = append(docPaths, docFile.path)
  349. }
  350. zipPath = exportSYZip(boxID, path.Dir(rootPath), baseFolderName, docPaths)
  351. name = strings.TrimSuffix(filepath.Base(block.Path), ".sy")
  352. return
  353. }
  354. func ExportDataInFolder(exportFolder string) (name string, err error) {
  355. util.PushEndlessProgress(Conf.Language(65))
  356. defer util.ClearPushProgress(100)
  357. zipPath, err := ExportData()
  358. if nil != err {
  359. return
  360. }
  361. name = filepath.Base(zipPath)
  362. name, err = url.PathUnescape(name)
  363. if nil != err {
  364. logging.LogErrorf("url unescape [%s] failed: %s", name, err)
  365. return
  366. }
  367. targetZipPath := filepath.Join(exportFolder, name)
  368. zipAbsPath := filepath.Join(util.TempDir, "export", name)
  369. err = filelock.Copy(zipAbsPath, targetZipPath)
  370. if nil != err {
  371. logging.LogErrorf("copy export zip from [%s] to [%s] failed: %s", zipAbsPath, targetZipPath, err)
  372. return
  373. }
  374. if removeErr := os.Remove(zipAbsPath); nil != removeErr {
  375. logging.LogErrorf("remove export zip failed: %s", removeErr)
  376. }
  377. return
  378. }
  379. func ExportData() (zipPath string, err error) {
  380. util.PushEndlessProgress(Conf.Language(65))
  381. defer util.ClearPushProgress(100)
  382. name := util.FilterFileName(filepath.Base(util.WorkspaceDir)) + "-" + util.CurrentTimeSecondsStr()
  383. exportFolder := filepath.Join(util.TempDir, "export", name)
  384. zipPath, err = exportData(exportFolder)
  385. if nil != err {
  386. return
  387. }
  388. zipPath = "/export/" + url.PathEscape(filepath.Base(zipPath))
  389. return
  390. }
  391. func exportData(exportFolder string) (zipPath string, err error) {
  392. WaitForWritingFiles()
  393. baseFolderName := "data-" + util.CurrentTimeSecondsStr()
  394. if err = os.MkdirAll(exportFolder, 0755); nil != err {
  395. logging.LogErrorf("create export temp folder failed: %s", err)
  396. return
  397. }
  398. data := filepath.Join(util.WorkspaceDir, "data")
  399. if err = filelock.Copy(data, exportFolder); nil != err {
  400. logging.LogErrorf("copy data dir from [%s] to [%s] failed: %s", data, baseFolderName, err)
  401. err = errors.New(fmt.Sprintf(Conf.Language(14), err.Error()))
  402. return
  403. }
  404. zipPath = exportFolder + ".zip"
  405. zip, err := gulu.Zip.Create(zipPath)
  406. if nil != err {
  407. logging.LogErrorf("create export data zip [%s] failed: %s", exportFolder, err)
  408. return
  409. }
  410. if err = zip.AddDirectory(baseFolderName, exportFolder); nil != err {
  411. logging.LogErrorf("create export data zip [%s] failed: %s", exportFolder, err)
  412. return
  413. }
  414. if err = zip.Close(); nil != err {
  415. logging.LogErrorf("close export data zip failed: %s", err)
  416. }
  417. os.RemoveAll(exportFolder)
  418. return
  419. }
  420. func ExportResources(resourcePaths []string, mainName string) (exportFilePath string, err error) {
  421. WaitForWritingFiles()
  422. // 用于导出的临时文件夹完整路径
  423. exportFolderPath := filepath.Join(util.TempDir, "export", mainName)
  424. if err = os.MkdirAll(exportFolderPath, 0755); nil != err {
  425. logging.LogErrorf("create export temp folder failed: %s", err)
  426. return
  427. }
  428. // 将需要导出的文件/文件夹复制到临时文件夹
  429. for _, resourcePath := range resourcePaths {
  430. resourceFullPath := filepath.Join(util.WorkspaceDir, resourcePath) // 资源完整路径
  431. resourceBaseName := filepath.Base(resourceFullPath) // 资源名称
  432. resourceCopyPath := filepath.Join(exportFolderPath, resourceBaseName) // 资源副本完整路径
  433. if err = filelock.Copy(resourceFullPath, resourceCopyPath); nil != err {
  434. logging.LogErrorf("copy resource will be exported from [%s] to [%s] failed: %s", resourcePath, resourceCopyPath, err)
  435. err = fmt.Errorf(Conf.Language(14), err.Error())
  436. return
  437. }
  438. }
  439. zipFilePath := exportFolderPath + ".zip" // 导出的 *.zip 文件完整路径
  440. zip, err := gulu.Zip.Create(zipFilePath)
  441. if nil != err {
  442. logging.LogErrorf("create export zip [%s] failed: %s", zipFilePath, err)
  443. return
  444. }
  445. if err = zip.AddDirectory(mainName, exportFolderPath); nil != err {
  446. logging.LogErrorf("create export zip [%s] failed: %s", exportFolderPath, err)
  447. return
  448. }
  449. if err = zip.Close(); nil != err {
  450. logging.LogErrorf("close export zip failed: %s", err)
  451. }
  452. os.RemoveAll(exportFolderPath)
  453. exportFilePath = path.Join("temp", "export", mainName+".zip") // 导出的 *.zip 文件相对于工作区目录的路径
  454. return
  455. }
  456. func Preview(id string) (retStdHTML string, retOutline []*Path) {
  457. tree, _ := LoadTreeByBlockID(id)
  458. tree = exportTree(tree, false, false, false,
  459. Conf.Export.BlockRefMode, Conf.Export.BlockEmbedMode, Conf.Export.FileAnnotationRefMode,
  460. Conf.Export.TagOpenMarker, Conf.Export.TagCloseMarker,
  461. Conf.Export.BlockRefTextLeft, Conf.Export.BlockRefTextRight,
  462. Conf.Export.AddTitle)
  463. luteEngine := NewLute()
  464. luteEngine.SetFootnotes(true)
  465. md := treenode.FormatNode(tree.Root, luteEngine)
  466. tree = parse.Parse("", []byte(md), luteEngine.ParseOptions)
  467. retStdHTML = luteEngine.ProtylePreview(tree, luteEngine.RenderOptions)
  468. if footnotesDefBlock := tree.Root.ChildByType(ast.NodeFootnotesDefBlock); nil != footnotesDefBlock {
  469. footnotesDefBlock.Unlink()
  470. }
  471. retOutline = outline(tree)
  472. return
  473. }
  474. func ExportDocx(id, savePath string, removeAssets, merge bool) (err error) {
  475. if !util.IsValidPandocBin(Conf.Export.PandocBin) {
  476. Conf.Export.PandocBin = util.PandocBinPath
  477. Conf.Save()
  478. if !util.IsValidPandocBin(Conf.Export.PandocBin) {
  479. return errors.New(Conf.Language(115))
  480. }
  481. }
  482. tmpDir := filepath.Join(util.TempDir, "export", gulu.Rand.String(7))
  483. if err = os.MkdirAll(tmpDir, 0755); nil != err {
  484. return
  485. }
  486. defer os.Remove(tmpDir)
  487. name, content := ExportMarkdownHTML(id, tmpDir, true, merge)
  488. tmpDocxPath := filepath.Join(tmpDir, name+".docx")
  489. args := []string{ // pandoc -f html --resource-path=请从这里开始 请从这里开始\index.html -o test.docx
  490. "-f", "html+tex_math_dollars",
  491. "--resource-path", tmpDir,
  492. "-o", tmpDocxPath,
  493. }
  494. // Pandoc template for exporting docx https://github.com/siyuan-note/siyuan/issues/8740
  495. docxTemplate := gulu.Str.RemoveInvisible(Conf.Export.DocxTemplate)
  496. docxTemplate = strings.TrimSpace(docxTemplate)
  497. if "" != docxTemplate {
  498. if !gulu.File.IsExist(docxTemplate) {
  499. logging.LogErrorf("docx template [%s] not found", docxTemplate)
  500. msg := fmt.Sprintf(Conf.Language(197), docxTemplate)
  501. return errors.New(msg)
  502. }
  503. args = append(args, "--reference-doc", docxTemplate)
  504. }
  505. pandoc := exec.Command(Conf.Export.PandocBin, args...)
  506. gulu.CmdAttr(pandoc)
  507. pandoc.Stdin = bytes.NewBufferString(content)
  508. output, err := pandoc.CombinedOutput()
  509. if nil != err {
  510. logging.LogErrorf("export docx failed: %s", gulu.Str.FromBytes(output))
  511. msg := fmt.Sprintf(Conf.Language(14), gulu.Str.FromBytes(output))
  512. return errors.New(msg)
  513. }
  514. targetPath := filepath.Join(savePath, name+".docx")
  515. if gulu.File.IsExist(targetPath) {
  516. // 先删除目标文件,以检查是否被占用 https://github.com/siyuan-note/siyuan/issues/8822
  517. if err := os.RemoveAll(targetPath); nil != err {
  518. logging.LogErrorf("export docx failed: %s", err)
  519. msg := fmt.Sprintf(Conf.language(215))
  520. return errors.New(msg)
  521. }
  522. }
  523. if err = filelock.Copy(tmpDocxPath, targetPath); nil != err {
  524. logging.LogErrorf("export docx failed: %s", err)
  525. return errors.New(fmt.Sprintf(Conf.Language(14), err))
  526. }
  527. if tmpAssets := filepath.Join(tmpDir, "assets"); !removeAssets && gulu.File.IsDir(tmpAssets) {
  528. if err = filelock.Copy(tmpAssets, filepath.Join(savePath, "assets")); nil != err {
  529. logging.LogErrorf("export docx failed: %s", err)
  530. return errors.New(fmt.Sprintf(Conf.Language(14), err))
  531. }
  532. }
  533. return
  534. }
  535. func ExportMarkdownHTML(id, savePath string, docx, merge bool) (name, dom string) {
  536. bt := treenode.GetBlockTree(id)
  537. if nil == bt {
  538. return
  539. }
  540. tree := prepareExportTree(bt)
  541. if merge {
  542. var mergeErr error
  543. tree, mergeErr = mergeSubDocs(tree)
  544. if nil != mergeErr {
  545. logging.LogErrorf("merge sub docs failed: %s", mergeErr)
  546. return
  547. }
  548. }
  549. tree = exportTree(tree, true, true, false,
  550. Conf.Export.BlockRefMode, Conf.Export.BlockEmbedMode, Conf.Export.FileAnnotationRefMode,
  551. Conf.Export.TagOpenMarker, Conf.Export.TagCloseMarker,
  552. Conf.Export.BlockRefTextLeft, Conf.Export.BlockRefTextRight,
  553. Conf.Export.AddTitle)
  554. name = path.Base(tree.HPath)
  555. name = util.FilterFileName(name) // 导出 PDF、HTML 和 Word 时未移除不支持的文件名符号 https://github.com/siyuan-note/siyuan/issues/5614
  556. savePath = strings.TrimSpace(savePath)
  557. if err := os.MkdirAll(savePath, 0755); nil != err {
  558. logging.LogErrorf("mkdir [%s] failed: %s", savePath, err)
  559. return
  560. }
  561. assets := assetsLinkDestsInTree(tree)
  562. for _, asset := range assets {
  563. if strings.HasPrefix(asset, "assets/") {
  564. if strings.Contains(asset, "?") {
  565. asset = asset[:strings.LastIndex(asset, "?")]
  566. }
  567. srcAbsPath, err := GetAssetAbsPath(asset)
  568. if nil != err {
  569. logging.LogWarnf("resolve path of asset [%s] failed: %s", asset, err)
  570. continue
  571. }
  572. targetAbsPath := filepath.Join(savePath, asset)
  573. if err = filelock.Copy(srcAbsPath, targetAbsPath); nil != err {
  574. logging.LogWarnf("copy asset from [%s] to [%s] failed: %s", srcAbsPath, targetAbsPath, err)
  575. }
  576. }
  577. }
  578. srcs := []string{"stage/build/export", "stage/build/fonts", "stage/protyle"}
  579. for _, src := range srcs {
  580. from := filepath.Join(util.WorkingDir, src)
  581. to := filepath.Join(savePath, src)
  582. if err := filelock.Copy(from, to); nil != err {
  583. logging.LogWarnf("copy stage from [%s] to [%s] failed: %s", from, savePath, err)
  584. return
  585. }
  586. }
  587. theme := Conf.Appearance.ThemeLight
  588. if 1 == Conf.Appearance.Mode {
  589. theme = Conf.Appearance.ThemeDark
  590. }
  591. srcs = []string{"icons", "themes/" + theme}
  592. appearancePath := util.AppearancePath
  593. if util.IsSymlinkPath(util.AppearancePath) {
  594. // Support for symlinked theme folder when exporting HTML https://github.com/siyuan-note/siyuan/issues/9173
  595. var readErr error
  596. appearancePath, readErr = filepath.EvalSymlinks(util.AppearancePath)
  597. if nil != readErr {
  598. logging.LogErrorf("readlink [%s] failed: %s", util.AppearancePath, readErr)
  599. return
  600. }
  601. }
  602. for _, src := range srcs {
  603. from := filepath.Join(appearancePath, src)
  604. to := filepath.Join(savePath, "appearance", src)
  605. if err := filelock.Copy(from, to); nil != err {
  606. logging.LogErrorf("copy appearance from [%s] to [%s] failed: %s", from, savePath, err)
  607. return
  608. }
  609. }
  610. // 复制自定义表情图片
  611. emojis := emojisInTree(tree)
  612. for _, emoji := range emojis {
  613. from := filepath.Join(util.DataDir, emoji)
  614. to := filepath.Join(savePath, emoji)
  615. if err := filelock.Copy(from, to); nil != err {
  616. logging.LogErrorf("copy emojis from [%s] to [%s] failed: %s", from, savePath, err)
  617. return
  618. }
  619. }
  620. luteEngine := NewLute()
  621. luteEngine.SetFootnotes(true)
  622. md := treenode.FormatNode(tree.Root, luteEngine)
  623. tree = parse.Parse("", []byte(md), luteEngine.ParseOptions)
  624. if docx {
  625. processIFrame(tree)
  626. }
  627. // 自定义表情图片地址去掉开头的 /
  628. ast.Walk(tree.Root, func(n *ast.Node, entering bool) ast.WalkStatus {
  629. if !entering {
  630. return ast.WalkContinue
  631. }
  632. if ast.NodeEmojiImg == n.Type {
  633. n.Tokens = bytes.ReplaceAll(n.Tokens, []byte("src=\"/emojis"), []byte("src=\"emojis"))
  634. }
  635. return ast.WalkContinue
  636. })
  637. if docx {
  638. renderer := render.NewProtyleExportDocxRenderer(tree, luteEngine.RenderOptions)
  639. output := renderer.Render()
  640. dom = gulu.Str.FromBytes(output)
  641. } else {
  642. dom = luteEngine.ProtylePreview(tree, luteEngine.RenderOptions)
  643. }
  644. return
  645. }
  646. func ExportHTML(id, savePath string, pdf, image, keepFold, merge bool) (name, dom string, node *ast.Node) {
  647. savePath = strings.TrimSpace(savePath)
  648. bt := treenode.GetBlockTree(id)
  649. if nil == bt {
  650. return
  651. }
  652. tree := prepareExportTree(bt)
  653. node = treenode.GetNodeInTree(tree, id)
  654. if merge {
  655. var mergeErr error
  656. tree, mergeErr = mergeSubDocs(tree)
  657. if nil != mergeErr {
  658. logging.LogErrorf("merge sub docs failed: %s", mergeErr)
  659. return
  660. }
  661. }
  662. var headings []*ast.Node
  663. if pdf { // 导出 PDF 需要标记目录书签
  664. ast.Walk(tree.Root, func(n *ast.Node, entering bool) ast.WalkStatus {
  665. if entering && ast.NodeHeading == n.Type && !n.ParentIs(ast.NodeBlockquote) {
  666. headings = append(headings, n)
  667. return ast.WalkSkipChildren
  668. }
  669. return ast.WalkContinue
  670. })
  671. for _, h := range headings {
  672. link := &ast.Node{Type: ast.NodeLink}
  673. link.AppendChild(&ast.Node{Type: ast.NodeOpenBracket})
  674. link.AppendChild(&ast.Node{Type: ast.NodeText, Tokens: []byte(" ")})
  675. link.AppendChild(&ast.Node{Type: ast.NodeCloseBracket})
  676. link.AppendChild(&ast.Node{Type: ast.NodeOpenParen})
  677. link.AppendChild(&ast.Node{Type: ast.NodeLinkDest, Tokens: []byte("pdf-outline://" + h.ID)})
  678. link.AppendChild(&ast.Node{Type: ast.NodeCloseParen})
  679. h.PrependChild(link)
  680. }
  681. }
  682. tree = exportTree(tree, true, true, keepFold,
  683. Conf.Export.BlockRefMode, Conf.Export.BlockEmbedMode, Conf.Export.FileAnnotationRefMode,
  684. Conf.Export.TagOpenMarker, Conf.Export.TagCloseMarker,
  685. Conf.Export.BlockRefTextLeft, Conf.Export.BlockRefTextRight,
  686. Conf.Export.AddTitle)
  687. name = path.Base(tree.HPath)
  688. name = util.FilterFileName(name) // 导出 PDF、HTML 和 Word 时未移除不支持的文件名符号 https://github.com/siyuan-note/siyuan/issues/5614
  689. if "" != savePath {
  690. if err := os.MkdirAll(savePath, 0755); nil != err {
  691. logging.LogErrorf("mkdir [%s] failed: %s", savePath, err)
  692. return
  693. }
  694. assets := assetsLinkDestsInTree(tree)
  695. for _, asset := range assets {
  696. if strings.Contains(asset, "?") {
  697. asset = asset[:strings.LastIndex(asset, "?")]
  698. }
  699. srcAbsPath, err := GetAssetAbsPath(asset)
  700. if nil != err {
  701. logging.LogWarnf("resolve path of asset [%s] failed: %s", asset, err)
  702. continue
  703. }
  704. targetAbsPath := filepath.Join(savePath, asset)
  705. if err = filelock.Copy(srcAbsPath, targetAbsPath); nil != err {
  706. logging.LogWarnf("copy asset from [%s] to [%s] failed: %s", srcAbsPath, targetAbsPath, err)
  707. }
  708. }
  709. }
  710. luteEngine := NewLute()
  711. if !pdf && "" != savePath { // 导出 HTML 需要复制静态资源
  712. srcs := []string{"stage/build/export", "stage/build/fonts", "stage/protyle"}
  713. for _, src := range srcs {
  714. from := filepath.Join(util.WorkingDir, src)
  715. to := filepath.Join(savePath, src)
  716. if err := filelock.Copy(from, to); nil != err {
  717. logging.LogErrorf("copy stage from [%s] to [%s] failed: %s", from, savePath, err)
  718. return
  719. }
  720. }
  721. theme := Conf.Appearance.ThemeLight
  722. if 1 == Conf.Appearance.Mode {
  723. theme = Conf.Appearance.ThemeDark
  724. }
  725. srcs = []string{"icons", "themes/" + theme}
  726. appearancePath := util.AppearancePath
  727. if util.IsSymlinkPath(util.AppearancePath) {
  728. // Support for symlinked theme folder when exporting HTML https://github.com/siyuan-note/siyuan/issues/9173
  729. var readErr error
  730. appearancePath, readErr = filepath.EvalSymlinks(util.AppearancePath)
  731. if nil != readErr {
  732. logging.LogErrorf("readlink [%s] failed: %s", util.AppearancePath, readErr)
  733. return
  734. }
  735. }
  736. for _, src := range srcs {
  737. from := filepath.Join(appearancePath, src)
  738. to := filepath.Join(savePath, "appearance", src)
  739. if err := filelock.Copy(from, to); nil != err {
  740. logging.LogErrorf("copy appearance from [%s] to [%s] failed: %s", from, savePath, err)
  741. return
  742. }
  743. }
  744. // 复制自定义表情图片
  745. emojis := emojisInTree(tree)
  746. for _, emoji := range emojis {
  747. from := filepath.Join(util.DataDir, emoji)
  748. to := filepath.Join(savePath, emoji)
  749. if err := filelock.Copy(from, to); nil != err {
  750. logging.LogErrorf("copy emojis from [%s] to [%s] failed: %s", from, savePath, err)
  751. return
  752. }
  753. }
  754. }
  755. if pdf {
  756. processIFrame(tree)
  757. }
  758. luteEngine.SetFootnotes(true)
  759. luteEngine.RenderOptions.ProtyleContenteditable = false
  760. luteEngine.SetProtyleMarkNetImg(false)
  761. // 不进行安全过滤,因为导出时需要保留所有的 HTML 标签
  762. // 使用属性 `data-export-html` 导出时 `<style></style>` 标签丢失 https://github.com/siyuan-note/siyuan/issues/6228
  763. luteEngine.SetSanitize(false)
  764. renderer := render.NewProtyleExportRenderer(tree, luteEngine.RenderOptions)
  765. dom = gulu.Str.FromBytes(renderer.Render())
  766. return
  767. }
  768. func prepareExportTree(bt *treenode.BlockTree) (ret *parse.Tree) {
  769. luteEngine := NewLute()
  770. ret, _ = filesys.LoadTree(bt.BoxID, bt.Path, luteEngine)
  771. if "d" != bt.Type {
  772. node := treenode.GetNodeInTree(ret, bt.ID)
  773. nodes := []*ast.Node{node}
  774. if "h" == bt.Type {
  775. children := treenode.HeadingChildren(node)
  776. for _, child := range children {
  777. nodes = append(nodes, child)
  778. }
  779. }
  780. oldRoot := ret.Root
  781. ret = parse.Parse("", []byte(""), luteEngine.ParseOptions)
  782. first := ret.Root.FirstChild
  783. for _, n := range nodes {
  784. first.InsertBefore(n)
  785. }
  786. ret.Root.KramdownIAL = oldRoot.KramdownIAL
  787. }
  788. ret.Path = bt.Path
  789. ret.HPath = bt.HPath
  790. ret.Box = bt.BoxID
  791. ret.ID = bt.RootID
  792. return
  793. }
  794. func processIFrame(tree *parse.Tree) {
  795. // 导出 PDF/Word 时 IFrame 块使用超链接 https://github.com/siyuan-note/siyuan/issues/4035
  796. var unlinks []*ast.Node
  797. ast.Walk(tree.Root, func(n *ast.Node, entering bool) ast.WalkStatus {
  798. if !entering {
  799. return ast.WalkContinue
  800. }
  801. if ast.NodeIFrame == n.Type {
  802. index := bytes.Index(n.Tokens, []byte("src=\""))
  803. if 0 > index {
  804. n.InsertBefore(&ast.Node{Type: ast.NodeText, Tokens: n.Tokens})
  805. } else {
  806. src := n.Tokens[index+len("src=\""):]
  807. src = src[:bytes.Index(src, []byte("\""))]
  808. src = html.UnescapeHTML(src)
  809. link := &ast.Node{Type: ast.NodeLink}
  810. link.AppendChild(&ast.Node{Type: ast.NodeOpenBracket})
  811. link.AppendChild(&ast.Node{Type: ast.NodeText, Tokens: src})
  812. link.AppendChild(&ast.Node{Type: ast.NodeCloseBracket})
  813. link.AppendChild(&ast.Node{Type: ast.NodeOpenParen})
  814. link.AppendChild(&ast.Node{Type: ast.NodeLinkDest, Tokens: src})
  815. link.AppendChild(&ast.Node{Type: ast.NodeCloseParen})
  816. n.InsertBefore(link)
  817. }
  818. unlinks = append(unlinks, n)
  819. }
  820. return ast.WalkContinue
  821. })
  822. for _, n := range unlinks {
  823. n.Unlink()
  824. }
  825. }
  826. func ProcessPDF(id, p string, merge, removeAssets, watermark bool) (err error) {
  827. tree, _ := LoadTreeByBlockID(id)
  828. if nil == tree {
  829. return
  830. }
  831. if merge {
  832. var mergeErr error
  833. tree, mergeErr = mergeSubDocs(tree)
  834. if nil != mergeErr {
  835. logging.LogErrorf("merge sub docs failed: %s", mergeErr)
  836. return
  837. }
  838. }
  839. var headings []*ast.Node
  840. assetDests := assetsLinkDestsInTree(tree)
  841. ast.Walk(tree.Root, func(n *ast.Node, entering bool) ast.WalkStatus {
  842. if !entering {
  843. return ast.WalkContinue
  844. }
  845. if ast.NodeHeading == n.Type && !n.ParentIs(ast.NodeBlockquote) {
  846. headings = append(headings, n)
  847. return ast.WalkSkipChildren
  848. }
  849. return ast.WalkContinue
  850. })
  851. pdfcpu.ConfigPath = "disable"
  852. font.UserFontDir = filepath.Join(util.HomeDir, ".config", "siyuan", "fonts")
  853. if mkdirErr := os.MkdirAll(font.UserFontDir, 0755); nil != mkdirErr {
  854. logging.LogErrorf("mkdir [%s] failed: %s", font.UserFontDir, mkdirErr)
  855. return
  856. }
  857. pdfCtx, ctxErr := api.ReadContextFile(p)
  858. if nil != ctxErr {
  859. logging.LogErrorf("read pdf context failed: %s", ctxErr)
  860. return
  861. }
  862. processPDFBookmarks(pdfCtx, headings)
  863. processPDFLinkEmbedAssets(pdfCtx, assetDests, removeAssets)
  864. processPDFWatermark(pdfCtx, watermark)
  865. pdfcpu.VersionStr = "SiYuan v" + util.Ver
  866. if writeErr := api.WriteContextFile(pdfCtx, p); nil != writeErr {
  867. logging.LogErrorf("write pdf context failed: %s", writeErr)
  868. return
  869. }
  870. return
  871. }
  872. func processPDFWatermark(pdfCtx *pdfcpu.Context, watermark bool) {
  873. // Support adding the watermark on export PDF https://github.com/siyuan-note/siyuan/issues/9961
  874. // https://pdfcpu.io/core/watermark
  875. if !watermark {
  876. return
  877. }
  878. str := Conf.Export.PDFWatermarkStr
  879. if "" == str {
  880. return
  881. }
  882. if !IsPaidUser() {
  883. return
  884. }
  885. mode := "text"
  886. if gulu.File.IsExist(str) {
  887. if ".pdf" == strings.ToLower(filepath.Ext(str)) {
  888. mode = "pdf"
  889. } else {
  890. mode = "image"
  891. }
  892. }
  893. desc := Conf.Export.PDFWatermarkDesc
  894. if "text" == mode && util.ContainsCJK(str) {
  895. // 中日韩文本水印需要安装字体文件
  896. descParts := strings.Split(desc, ",")
  897. m := map[string]string{}
  898. for _, descPart := range descParts {
  899. kv := strings.Split(descPart, ":")
  900. if 2 != len(kv) {
  901. continue
  902. }
  903. m[kv[0]] = kv[1]
  904. }
  905. m["fontname"] = "LXGWWenKaiLite-Regular"
  906. descBuilder := bytes.Buffer{}
  907. for k, v := range m {
  908. descBuilder.WriteString(k)
  909. descBuilder.WriteString(":")
  910. descBuilder.WriteString(v)
  911. descBuilder.WriteString(",")
  912. }
  913. desc = descBuilder.String()
  914. desc = desc[:len(desc)-1]
  915. fontPath := filepath.Join(util.AppearancePath, "fonts", "LxgwWenKai-Lite-1.311", "LXGWWenKaiLite-Regular.ttf")
  916. err := api.InstallFonts([]string{fontPath})
  917. if nil != err {
  918. logging.LogErrorf("install font [%s] failed: %s", fontPath, err)
  919. }
  920. }
  921. logging.LogInfof("add PDF watermark [mode=%s, str=%s, desc=%s]", mode, str, desc)
  922. var wm *pdfcpu.Watermark
  923. var err error
  924. switch mode {
  925. case "text":
  926. wm, err = pdfcpu.ParseTextWatermarkDetails(str, desc, false, pdfcpu.POINTS)
  927. case "image":
  928. wm, err = pdfcpu.ParseImageWatermarkDetails(str, desc, false, pdfcpu.POINTS)
  929. case "pdf":
  930. wm, err = pdfcpu.ParsePDFWatermarkDetails(str, desc, false, pdfcpu.POINTS)
  931. }
  932. if nil != err {
  933. logging.LogErrorf("parse watermark failed: %s", err)
  934. return
  935. }
  936. wm.OnTop = true // Export PDF and add watermarks no longer covered by images https://github.com/siyuan-note/siyuan/issues/10818
  937. err = pdfCtx.AddWatermarks(nil, wm)
  938. if nil != err {
  939. logging.LogErrorf("add watermark failed: %s", err)
  940. return
  941. }
  942. }
  943. func processPDFBookmarks(pdfCtx *pdfcpu.Context, headings []*ast.Node) {
  944. links, err := api.ListToCLinks(pdfCtx)
  945. if nil != err {
  946. return
  947. }
  948. sort.Slice(links, func(i, j int) bool {
  949. return links[i].Page < links[j].Page
  950. })
  951. bms := map[string]*pdfcpu.Bookmark{}
  952. for _, link := range links {
  953. linkID := link.URI[strings.LastIndex(link.URI, "/")+1:]
  954. b := sql.GetBlock(linkID)
  955. if nil == b {
  956. logging.LogWarnf("pdf outline block [%s] not found", linkID)
  957. continue
  958. }
  959. title := b.Content
  960. title, _ = url.QueryUnescape(title)
  961. bm := &pdfcpu.Bookmark{
  962. Title: title,
  963. PageFrom: link.Page,
  964. AbsPos: link.Rect.UR.Y,
  965. }
  966. bms[linkID] = bm
  967. }
  968. if 1 > len(bms) {
  969. return
  970. }
  971. var topBms []*pdfcpu.Bookmark
  972. stack := linkedliststack.New()
  973. for _, h := range headings {
  974. L:
  975. for ; ; stack.Pop() {
  976. cur, ok := stack.Peek()
  977. if !ok {
  978. bm := bms[h.ID]
  979. if nil == bm {
  980. break L
  981. }
  982. bm.Level = h.HeadingLevel
  983. stack.Push(bm)
  984. topBms = append(topBms, bm)
  985. break L
  986. }
  987. tip := cur.(*pdfcpu.Bookmark)
  988. if tip.Level < h.HeadingLevel {
  989. bm := bms[h.ID]
  990. bm.Level = h.HeadingLevel
  991. bm.Parent = tip
  992. tip.Children = append(tip.Children, bm)
  993. stack.Push(bm)
  994. break L
  995. }
  996. }
  997. }
  998. err = pdfCtx.AddBookmarks(topBms)
  999. if nil != err {
  1000. logging.LogErrorf("add bookmark failed: %s", err)
  1001. return
  1002. }
  1003. }
  1004. // processPDFLinkEmbedAssets 处理资源文件超链接,根据 removeAssets 参数决定是否将资源文件嵌入到 PDF 中。
  1005. // 导出 PDF 时支持将资源文件作为附件嵌入 https://github.com/siyuan-note/siyuan/issues/7414
  1006. func processPDFLinkEmbedAssets(pdfCtx *pdfcpu.Context, assetDests []string, removeAssets bool) {
  1007. var assetAbsPaths []string
  1008. for _, dest := range assetDests {
  1009. if absPath, _ := GetAssetAbsPath(dest); "" != absPath {
  1010. assetAbsPaths = append(assetAbsPaths, absPath)
  1011. }
  1012. }
  1013. if 1 > len(assetAbsPaths) {
  1014. return
  1015. }
  1016. assetLinks, otherLinks, listErr := api.ListLinks(pdfCtx)
  1017. if nil != listErr {
  1018. logging.LogErrorf("list asset links failed: %s", listErr)
  1019. return
  1020. }
  1021. if _, removeErr := pdfCtx.RemoveAnnotations(nil, nil, nil, false); nil != removeErr {
  1022. logging.LogWarnf("remove annotations failed: %s", removeErr)
  1023. }
  1024. linkMap := map[int][]pdfcpu.AnnotationRenderer{}
  1025. for _, link := range otherLinks {
  1026. link.URI, _ = url.PathUnescape(link.URI)
  1027. if 1 > len(linkMap[link.Page]) {
  1028. linkMap[link.Page] = []pdfcpu.AnnotationRenderer{link}
  1029. } else {
  1030. linkMap[link.Page] = append(linkMap[link.Page], link)
  1031. }
  1032. }
  1033. attachmentMap := map[int][]*pdfcpu.IndirectRef{}
  1034. now := pdfcpu.StringLiteral(pdfcpu.DateString(time.Now()))
  1035. for _, link := range assetLinks {
  1036. link.URI = strings.ReplaceAll(link.URI, "http://"+util.LocalHost+":"+util.ServerPort+"/export/temp/", "")
  1037. link.URI = strings.ReplaceAll(link.URI, "http://"+util.LocalHost+":"+util.ServerPort+"/", "") // Exporting PDF embedded asset files as attachments fails https://github.com/siyuan-note/siyuan/issues/7414#issuecomment-1704573557
  1038. link.URI, _ = url.PathUnescape(link.URI)
  1039. if idx := strings.Index(link.URI, "?"); 0 < idx {
  1040. link.URI = link.URI[:idx]
  1041. }
  1042. if !removeAssets {
  1043. // 不移除资源文件夹的话将超链接指向资源文件夹
  1044. if 1 > len(linkMap[link.Page]) {
  1045. linkMap[link.Page] = []pdfcpu.AnnotationRenderer{link}
  1046. } else {
  1047. linkMap[link.Page] = append(linkMap[link.Page], link)
  1048. }
  1049. continue
  1050. }
  1051. // 移除资源文件夹的话使用内嵌附件
  1052. absPath, getErr := GetAssetAbsPath(link.URI)
  1053. if nil != getErr {
  1054. continue
  1055. }
  1056. ir, newErr := pdfCtx.XRefTable.NewEmbeddedFileStreamDict(absPath)
  1057. if nil != newErr {
  1058. logging.LogWarnf("new embedded file stream dict failed: %s", newErr)
  1059. continue
  1060. }
  1061. fn := filepath.Base(absPath)
  1062. fileSpecDict, newErr := pdfCtx.XRefTable.NewFileSpecDict(fn, pdfcpu.EncodeUTF16String(fn), "attached by SiYuan", *ir)
  1063. if nil != newErr {
  1064. logging.LogWarnf("new file spec dict failed: %s", newErr)
  1065. continue
  1066. }
  1067. ir, indErr := pdfCtx.XRefTable.IndRefForNewObject(fileSpecDict)
  1068. if nil != indErr {
  1069. logging.LogWarnf("ind ref for new object failed: %s", indErr)
  1070. continue
  1071. }
  1072. lx := link.Rect.LL.X + link.Rect.Width()
  1073. ly := link.Rect.LL.Y + link.Rect.Height()/2
  1074. ux := lx + link.Rect.Height()/2
  1075. uy := ly + link.Rect.Height()/2
  1076. d := pdfcpu.Dict(
  1077. map[string]pdfcpu.Object{
  1078. "Type": pdfcpu.Name("Annot"),
  1079. "Subtype": pdfcpu.Name("FileAttachment"),
  1080. "Contents": pdfcpu.StringLiteral(""),
  1081. "Rect": pdfcpu.Rect(lx, ly, ux, uy).Array(),
  1082. "P": link.P,
  1083. "M": now,
  1084. "F": pdfcpu.Integer(0),
  1085. "Border": pdfcpu.NewIntegerArray(0, 0, 1),
  1086. "C": pdfcpu.NewNumberArray(0.5, 0.0, 0.5),
  1087. "CA": pdfcpu.Float(0.95),
  1088. "CreationDate": now,
  1089. "Name": pdfcpu.Name("FileAttachment"),
  1090. "FS": *ir,
  1091. "NM": pdfcpu.StringLiteral(""),
  1092. },
  1093. )
  1094. ann, indErr := pdfCtx.XRefTable.IndRefForNewObject(d)
  1095. if nil != indErr {
  1096. logging.LogWarnf("ind ref for new object failed: %s", indErr)
  1097. continue
  1098. }
  1099. pageDictIndRef, pageErr := pdfCtx.PageDictIndRef(link.Page)
  1100. if nil != pageErr {
  1101. logging.LogWarnf("page dict ind ref failed: %s", pageErr)
  1102. continue
  1103. }
  1104. d, defErr := pdfCtx.DereferenceDict(*pageDictIndRef)
  1105. if nil != defErr {
  1106. logging.LogWarnf("dereference dict failed: %s", defErr)
  1107. continue
  1108. }
  1109. if 1 > len(attachmentMap[link.Page]) {
  1110. attachmentMap[link.Page] = []*pdfcpu.IndirectRef{ann}
  1111. } else {
  1112. attachmentMap[link.Page] = append(attachmentMap[link.Page], ann)
  1113. }
  1114. }
  1115. if 0 < len(linkMap) {
  1116. if _, addErr := pdfCtx.AddAnnotationsMap(linkMap, false); nil != addErr {
  1117. logging.LogErrorf("add annotations map failed: %s", addErr)
  1118. }
  1119. }
  1120. // 添加附件注解指向内嵌的附件
  1121. for page, anns := range attachmentMap {
  1122. pageDictIndRef, pageErr := pdfCtx.PageDictIndRef(page)
  1123. if nil != pageErr {
  1124. logging.LogWarnf("page dict ind ref failed: %s", pageErr)
  1125. continue
  1126. }
  1127. pageDict, defErr := pdfCtx.DereferenceDict(*pageDictIndRef)
  1128. if nil != defErr {
  1129. logging.LogWarnf("dereference dict failed: %s", defErr)
  1130. continue
  1131. }
  1132. array := pdfcpu.Array{}
  1133. for _, ann := range anns {
  1134. array = append(array, *ann)
  1135. }
  1136. obj, found := pageDict.Find("Annots")
  1137. if !found {
  1138. pageDict.Insert("Annots", array)
  1139. pdfCtx.EnsureVersionForWriting()
  1140. continue
  1141. }
  1142. ir, ok := obj.(pdfcpu.IndirectRef)
  1143. if !ok {
  1144. pageDict.Update("Annots", append(obj.(pdfcpu.Array), array...))
  1145. pdfCtx.EnsureVersionForWriting()
  1146. continue
  1147. }
  1148. // Annots array is an IndirectReference.
  1149. o, err := pdfCtx.Dereference(ir)
  1150. if err != nil || o == nil {
  1151. continue
  1152. }
  1153. annots, _ := o.(pdfcpu.Array)
  1154. entry, ok := pdfCtx.FindTableEntryForIndRef(&ir)
  1155. if !ok {
  1156. continue
  1157. }
  1158. entry.Object = append(annots, array...)
  1159. pdfCtx.EnsureVersionForWriting()
  1160. }
  1161. }
  1162. func ExportStdMarkdown(id string) string {
  1163. tree, err := LoadTreeByBlockID(id)
  1164. if nil != err {
  1165. logging.LogErrorf("load tree by block id [%s] failed: %s", id, err)
  1166. return ""
  1167. }
  1168. cloudAssetsBase := ""
  1169. if IsSubscriber() {
  1170. cloudAssetsBase = util.GetCloudAssetsServer() + Conf.GetUser().UserId + "/"
  1171. }
  1172. return exportMarkdownContent0(tree, cloudAssetsBase, false,
  1173. Conf.Export.BlockRefMode, Conf.Export.BlockEmbedMode, Conf.Export.FileAnnotationRefMode,
  1174. Conf.Export.TagOpenMarker, Conf.Export.TagCloseMarker,
  1175. Conf.Export.BlockRefTextLeft, Conf.Export.BlockRefTextRight,
  1176. Conf.Export.AddTitle, nil)
  1177. }
  1178. func ExportPandocConvertZip(id, pandocTo, ext string) (name, zipPath string) {
  1179. block := treenode.GetBlockTree(id)
  1180. if nil == block {
  1181. logging.LogErrorf("not found block [%s]", id)
  1182. return
  1183. }
  1184. boxID := block.BoxID
  1185. box := Conf.Box(boxID)
  1186. baseFolderName := path.Base(block.HPath)
  1187. if "." == baseFolderName {
  1188. baseFolderName = path.Base(block.Path)
  1189. }
  1190. docPaths := []string{block.Path}
  1191. docFiles := box.ListFiles(strings.TrimSuffix(block.Path, ".sy"))
  1192. for _, docFile := range docFiles {
  1193. docPaths = append(docPaths, docFile.path)
  1194. }
  1195. zipPath = exportPandocConvertZip(false, boxID, baseFolderName, docPaths, "gfm+footnotes+hard_line_breaks", pandocTo, ext)
  1196. name = strings.TrimSuffix(filepath.Base(block.Path), ".sy")
  1197. return
  1198. }
  1199. func BatchExportMarkdown(boxID, folderPath string) (zipPath string) {
  1200. box := Conf.Box(boxID)
  1201. var baseFolderName string
  1202. if "/" == folderPath {
  1203. baseFolderName = box.Name
  1204. } else {
  1205. block := treenode.GetBlockTreeRootByHPath(box.ID, folderPath)
  1206. if nil == block {
  1207. logging.LogErrorf("not found block")
  1208. return
  1209. }
  1210. baseFolderName = path.Base(block.HPath)
  1211. }
  1212. if "" == baseFolderName {
  1213. baseFolderName = Conf.language(105)
  1214. }
  1215. docFiles := box.ListFiles(folderPath)
  1216. var docPaths []string
  1217. for _, docFile := range docFiles {
  1218. docPaths = append(docPaths, docFile.path)
  1219. }
  1220. zipPath = exportPandocConvertZip(true, boxID, baseFolderName, docPaths, "", "", ".md")
  1221. return
  1222. }
  1223. func yfm(docIAL map[string]string) string {
  1224. // 导出 Markdown 文件时开头附上一些元数据 https://github.com/siyuan-note/siyuan/issues/6880
  1225. // 导出 Markdown 时在文档头添加 YFM 开关https://github.com/siyuan-note/siyuan/issues/7727
  1226. if !Conf.Export.MarkdownYFM {
  1227. return ""
  1228. }
  1229. buf := bytes.Buffer{}
  1230. buf.WriteString("---\n")
  1231. var title, created, updated, tags string
  1232. for k, v := range docIAL {
  1233. if "id" == k {
  1234. createdTime, parseErr := time.Parse("20060102150405", util.TimeFromID(v))
  1235. if nil == parseErr {
  1236. created = createdTime.Format(time.RFC3339)
  1237. }
  1238. continue
  1239. }
  1240. if "title" == k {
  1241. title = v
  1242. continue
  1243. }
  1244. if "updated" == k {
  1245. updatedTime, parseErr := time.Parse("20060102150405", v)
  1246. if nil == parseErr {
  1247. updated = updatedTime.Format(time.RFC3339)
  1248. }
  1249. continue
  1250. }
  1251. if "tags" == k {
  1252. tags = v
  1253. continue
  1254. }
  1255. }
  1256. if "" != title {
  1257. buf.WriteString("title: ")
  1258. buf.WriteString(title)
  1259. buf.WriteString("\n")
  1260. }
  1261. if "" == updated {
  1262. updated = time.Now().Format(time.RFC3339)
  1263. }
  1264. if "" == created {
  1265. created = updated
  1266. }
  1267. buf.WriteString("date: ")
  1268. buf.WriteString(created)
  1269. buf.WriteString("\n")
  1270. buf.WriteString("lastmod: ")
  1271. buf.WriteString(updated)
  1272. buf.WriteString("\n")
  1273. if "" != tags {
  1274. buf.WriteString("tags: [")
  1275. buf.WriteString(tags)
  1276. buf.WriteString("]\n")
  1277. }
  1278. buf.WriteString("---\n\n")
  1279. return buf.String()
  1280. }
  1281. func exportBoxSYZip(boxID string) (zipPath string) {
  1282. box := Conf.Box(boxID)
  1283. if nil == box {
  1284. logging.LogErrorf("not found box [%s]", boxID)
  1285. return
  1286. }
  1287. baseFolderName := box.Name
  1288. var docPaths []string
  1289. docFiles := box.ListFiles("/")
  1290. for _, docFile := range docFiles {
  1291. docPaths = append(docPaths, docFile.path)
  1292. }
  1293. zipPath = exportSYZip(boxID, "/", baseFolderName, docPaths)
  1294. return
  1295. }
  1296. func exportSYZip(boxID, rootDirPath, baseFolderName string, docPaths []string) (zipPath string) {
  1297. dir, name := path.Split(baseFolderName)
  1298. name = util.FilterFileName(name)
  1299. if strings.HasSuffix(name, "..") {
  1300. // 文档标题以 `..` 结尾时无法导出 Markdown https://github.com/siyuan-note/siyuan/issues/4698
  1301. // 似乎是 os.MkdirAll 的 bug,以 .. 结尾的路径无法创建,所以这里加上 _ 结尾
  1302. name += "_"
  1303. }
  1304. baseFolderName = path.Join(dir, name)
  1305. box := Conf.Box(boxID)
  1306. exportFolder := filepath.Join(util.TempDir, "export", baseFolderName)
  1307. if err := os.MkdirAll(exportFolder, 0755); nil != err {
  1308. logging.LogErrorf("create export temp folder failed: %s", err)
  1309. return
  1310. }
  1311. trees := map[string]*parse.Tree{}
  1312. refTrees := map[string]*parse.Tree{}
  1313. for _, p := range docPaths {
  1314. docIAL := box.docIAL(p)
  1315. if nil == docIAL {
  1316. continue
  1317. }
  1318. id := docIAL["id"]
  1319. tree, err := LoadTreeByBlockID(id)
  1320. if nil != err {
  1321. continue
  1322. }
  1323. trees[tree.ID] = tree
  1324. }
  1325. for _, tree := range trees {
  1326. refs := exportRefTrees(tree)
  1327. for refTreeID, refTree := range refs {
  1328. if nil == trees[refTreeID] {
  1329. refTrees[refTreeID] = refTree
  1330. }
  1331. }
  1332. }
  1333. // 按文件夹结构复制选择的树
  1334. for _, tree := range trees {
  1335. readPath := filepath.Join(util.DataDir, tree.Box, tree.Path)
  1336. data, readErr := filelock.ReadFile(readPath)
  1337. if nil != readErr {
  1338. logging.LogErrorf("read file [%s] failed: %s", readPath, readErr)
  1339. continue
  1340. }
  1341. writePath := strings.TrimPrefix(tree.Path, rootDirPath)
  1342. writePath = filepath.Join(exportFolder, writePath)
  1343. writeFolder := filepath.Dir(writePath)
  1344. if mkdirErr := os.MkdirAll(writeFolder, 0755); nil != mkdirErr {
  1345. logging.LogErrorf("create export temp folder [%s] failed: %s", writeFolder, mkdirErr)
  1346. continue
  1347. }
  1348. if writeErr := os.WriteFile(writePath, data, 0644); nil != writeErr {
  1349. logging.LogErrorf("write export file [%s] failed: %s", writePath, writeErr)
  1350. continue
  1351. }
  1352. }
  1353. // 引用树放在导出文件夹根路径下
  1354. for treeID, tree := range refTrees {
  1355. readPath := filepath.Join(util.DataDir, tree.Box, tree.Path)
  1356. data, readErr := filelock.ReadFile(readPath)
  1357. if nil != readErr {
  1358. logging.LogErrorf("read file [%s] failed: %s", readPath, readErr)
  1359. continue
  1360. }
  1361. writePath := strings.TrimPrefix(tree.Path, rootDirPath)
  1362. writePath = filepath.Join(exportFolder, treeID+".sy")
  1363. if writeErr := os.WriteFile(writePath, data, 0644); nil != writeErr {
  1364. logging.LogErrorf("write export file [%s] failed: %s", writePath, writeErr)
  1365. continue
  1366. }
  1367. }
  1368. // 将引用树合并到选择树中,以便后面一次性导出资源文件
  1369. for treeID, tree := range refTrees {
  1370. trees[treeID] = tree
  1371. }
  1372. // 导出引用的资源文件
  1373. copiedAssets := hashset.New()
  1374. for _, tree := range trees {
  1375. var assets []string
  1376. assets = append(assets, assetsLinkDestsInTree(tree)...)
  1377. titleImgPath := treenode.GetDocTitleImgPath(tree.Root) // Export .sy.zip doc title image is not exported https://github.com/siyuan-note/siyuan/issues/8748
  1378. if "" != titleImgPath {
  1379. assets = append(assets, titleImgPath)
  1380. }
  1381. for _, asset := range assets {
  1382. asset = string(html.DecodeDestination([]byte(asset)))
  1383. if strings.Contains(asset, "?") {
  1384. asset = asset[:strings.LastIndex(asset, "?")]
  1385. }
  1386. if copiedAssets.Contains(asset) {
  1387. continue
  1388. }
  1389. srcPath, assetErr := GetAssetAbsPath(asset)
  1390. if nil != assetErr {
  1391. logging.LogWarnf("get asset [%s] abs path failed: %s", asset, assetErr)
  1392. continue
  1393. }
  1394. destPath := filepath.Join(exportFolder, asset)
  1395. assetErr = filelock.Copy(srcPath, destPath)
  1396. if nil != assetErr {
  1397. logging.LogErrorf("copy asset from [%s] to [%s] failed: %s", srcPath, destPath, assetErr)
  1398. continue
  1399. }
  1400. if !gulu.File.IsDir(srcPath) && strings.HasSuffix(strings.ToLower(srcPath), ".pdf") {
  1401. sya := srcPath + ".sya"
  1402. if filelock.IsExist(sya) {
  1403. // Related PDF annotation information is not exported when exporting .sy.zip https://github.com/siyuan-note/siyuan/issues/7836
  1404. if syaErr := filelock.Copy(sya, destPath+".sya"); nil != syaErr {
  1405. logging.LogErrorf("copy sya from [%s] to [%s] failed: %s", sya, destPath+".sya", syaErr)
  1406. }
  1407. }
  1408. }
  1409. copiedAssets.Add(asset)
  1410. }
  1411. }
  1412. // 导出数据库 Attribute View export https://github.com/siyuan-note/siyuan/issues/8710
  1413. exportStorageAvDir := filepath.Join(exportFolder, "storage", "av")
  1414. for _, tree := range trees {
  1415. ast.Walk(tree.Root, func(n *ast.Node, entering bool) ast.WalkStatus {
  1416. if !entering {
  1417. return ast.WalkContinue
  1418. }
  1419. if ast.NodeAttributeView != n.Type {
  1420. return ast.WalkContinue
  1421. }
  1422. avID := n.AttributeViewID
  1423. avJSONPath := av.GetAttributeViewDataPath(avID)
  1424. if !filelock.IsExist(avJSONPath) {
  1425. return ast.WalkContinue
  1426. }
  1427. if copyErr := filelock.Copy(avJSONPath, filepath.Join(exportStorageAvDir, avID+".json")); nil != copyErr {
  1428. logging.LogErrorf("copy av json failed: %s", copyErr)
  1429. }
  1430. attrView, err := av.ParseAttributeView(avID)
  1431. if nil != err {
  1432. logging.LogErrorf("parse attribute view [%s] failed: %s", avID, err)
  1433. return ast.WalkContinue
  1434. }
  1435. for _, keyValues := range attrView.KeyValues {
  1436. switch keyValues.Key.Type {
  1437. case av.KeyTypeMAsset: // 导出资源文件列 https://github.com/siyuan-note/siyuan/issues/9919
  1438. for _, value := range keyValues.Values {
  1439. for _, asset := range value.MAsset {
  1440. if !treenode.IsRelativePath([]byte(asset.Content)) {
  1441. continue
  1442. }
  1443. destPath := filepath.Join(exportFolder, asset.Content)
  1444. srcPath, assetErr := GetAssetAbsPath(asset.Content)
  1445. if nil != assetErr {
  1446. logging.LogWarnf("get asset [%s] abs path failed: %s", asset.Content, assetErr)
  1447. continue
  1448. }
  1449. if copyErr := filelock.Copy(srcPath, destPath); nil != copyErr {
  1450. logging.LogErrorf("copy asset failed: %s", copyErr)
  1451. }
  1452. }
  1453. }
  1454. }
  1455. }
  1456. // 级联导出关联列关联的数据库
  1457. exportRelationAvs(avID, exportStorageAvDir)
  1458. return ast.WalkContinue
  1459. })
  1460. }
  1461. // 导出闪卡 Export related flashcard data when exporting .sy.zip https://github.com/siyuan-note/siyuan/issues/9372
  1462. exportStorageRiffDir := filepath.Join(exportFolder, "storage", "riff")
  1463. deck, loadErr := riff.LoadDeck(exportStorageRiffDir, builtinDeckID, Conf.Flashcard.RequestRetention, Conf.Flashcard.MaximumInterval, Conf.Flashcard.Weights)
  1464. if nil != loadErr {
  1465. logging.LogErrorf("load deck [%s] failed: %s", name, loadErr)
  1466. } else {
  1467. for _, tree := range trees {
  1468. cards := getTreeFlashcards(tree.ID)
  1469. for _, card := range cards {
  1470. deck.AddCard(card.ID(), card.BlockID())
  1471. }
  1472. }
  1473. if 0 < deck.CountCards() {
  1474. if saveErr := deck.Save(); nil != saveErr {
  1475. logging.LogErrorf("save deck [%s] failed: %s", name, saveErr)
  1476. }
  1477. }
  1478. }
  1479. // 导出自定义排序
  1480. sortPath := filepath.Join(util.DataDir, box.ID, ".siyuan", "sort.json")
  1481. fullSortIDs := map[string]int{}
  1482. sortIDs := map[string]int{}
  1483. var sortData []byte
  1484. var sortErr error
  1485. if filelock.IsExist(sortPath) {
  1486. sortData, sortErr = filelock.ReadFile(sortPath)
  1487. if nil != sortErr {
  1488. logging.LogErrorf("read sort conf failed: %s", sortErr)
  1489. }
  1490. if sortErr = gulu.JSON.UnmarshalJSON(sortData, &fullSortIDs); nil != sortErr {
  1491. logging.LogErrorf("unmarshal sort conf failed: %s", sortErr)
  1492. }
  1493. if 0 < len(fullSortIDs) {
  1494. for _, tree := range trees {
  1495. if v, ok := fullSortIDs[tree.ID]; ok {
  1496. sortIDs[tree.ID] = v
  1497. }
  1498. }
  1499. }
  1500. if 0 < len(sortIDs) {
  1501. sortData, sortErr = gulu.JSON.MarshalJSON(sortIDs)
  1502. if nil != sortErr {
  1503. logging.LogErrorf("marshal sort conf failed: %s", sortErr)
  1504. }
  1505. if 0 < len(sortData) {
  1506. confDir := filepath.Join(exportFolder, ".siyuan")
  1507. if mkdirErr := os.MkdirAll(confDir, 0755); nil != mkdirErr {
  1508. logging.LogErrorf("create export conf folder [%s] failed: %s", confDir, mkdirErr)
  1509. } else {
  1510. sortPath = filepath.Join(confDir, "sort.json")
  1511. if writeErr := os.WriteFile(sortPath, sortData, 0644); nil != writeErr {
  1512. logging.LogErrorf("write sort conf failed: %s", writeErr)
  1513. }
  1514. }
  1515. }
  1516. }
  1517. }
  1518. zipPath = exportFolder + ".sy.zip"
  1519. zip, err := gulu.Zip.Create(zipPath)
  1520. if nil != err {
  1521. logging.LogErrorf("create export .sy.zip [%s] failed: %s", exportFolder, err)
  1522. return ""
  1523. }
  1524. if err = zip.AddDirectory(baseFolderName, exportFolder); nil != err {
  1525. logging.LogErrorf("create export .sy.zip [%s] failed: %s", exportFolder, err)
  1526. return ""
  1527. }
  1528. if err = zip.Close(); nil != err {
  1529. logging.LogErrorf("close export .sy.zip failed: %s", err)
  1530. }
  1531. os.RemoveAll(exportFolder)
  1532. zipPath = "/export/" + url.PathEscape(filepath.Base(zipPath))
  1533. return
  1534. }
  1535. func exportRelationAvs(avID, exportStorageAvDir string) {
  1536. avIDs := hashset.New()
  1537. walkRelationAvs(avID, avIDs)
  1538. for _, v := range avIDs.Values() {
  1539. relAvID := v.(string)
  1540. relAvJSONPath := av.GetAttributeViewDataPath(relAvID)
  1541. if !filelock.IsExist(relAvJSONPath) {
  1542. continue
  1543. }
  1544. if copyErr := filelock.Copy(relAvJSONPath, filepath.Join(exportStorageAvDir, relAvID+".json")); nil != copyErr {
  1545. logging.LogErrorf("copy av json failed: %s", copyErr)
  1546. }
  1547. }
  1548. }
  1549. func walkRelationAvs(avID string, exportAvIDs *hashset.Set) {
  1550. if exportAvIDs.Contains(avID) {
  1551. return
  1552. }
  1553. attrView, _ := av.ParseAttributeView(avID)
  1554. if nil == attrView {
  1555. return
  1556. }
  1557. exportAvIDs.Add(avID)
  1558. for _, keyValues := range attrView.KeyValues {
  1559. switch keyValues.Key.Type {
  1560. case av.KeyTypeRelation: // 导出关联列
  1561. if nil == keyValues.Key.Relation {
  1562. break
  1563. }
  1564. walkRelationAvs(keyValues.Key.Relation.AvID, exportAvIDs)
  1565. }
  1566. }
  1567. }
  1568. func ExportMarkdownContent(id string) (hPath, exportedMd string) {
  1569. return exportMarkdownContent(id, Conf.Export.BlockRefMode, nil)
  1570. }
  1571. func exportMarkdownContent(id string, exportRefMode int, defBlockIDs []string) (hPath, exportedMd string) {
  1572. tree, err := LoadTreeByBlockID(id)
  1573. if nil != err {
  1574. logging.LogErrorf("load tree by block id [%s] failed: %s", id, err)
  1575. return
  1576. }
  1577. hPath = tree.HPath
  1578. exportedMd = exportMarkdownContent0(tree, "", false,
  1579. exportRefMode, Conf.Export.BlockEmbedMode, Conf.Export.FileAnnotationRefMode,
  1580. Conf.Export.TagOpenMarker, Conf.Export.TagCloseMarker,
  1581. Conf.Export.BlockRefTextLeft, Conf.Export.BlockRefTextRight,
  1582. Conf.Export.AddTitle, defBlockIDs)
  1583. docIAL := parse.IAL2Map(tree.Root.KramdownIAL)
  1584. exportedMd = yfm(docIAL) + exportedMd
  1585. return
  1586. }
  1587. func exportMarkdownContent0(tree *parse.Tree, cloudAssetsBase string, assetsDestSpace2Underscore bool,
  1588. blockRefMode, blockEmbedMode, fileAnnotationRefMode int,
  1589. tagOpenMarker, tagCloseMarker string,
  1590. blockRefTextLeft, blockRefTextRight string,
  1591. addTitle bool,
  1592. defBlockIDs []string) (ret string) {
  1593. tree = exportTree(tree, false, true, false,
  1594. blockRefMode, blockEmbedMode, fileAnnotationRefMode,
  1595. tagOpenMarker, tagCloseMarker,
  1596. blockRefTextLeft, blockRefTextRight,
  1597. addTitle)
  1598. luteEngine := NewLute()
  1599. luteEngine.SetFootnotes(true)
  1600. luteEngine.SetKramdownIAL(false)
  1601. if "" != cloudAssetsBase {
  1602. luteEngine.RenderOptions.LinkBase = cloudAssetsBase
  1603. }
  1604. if assetsDestSpace2Underscore { // 上传到社区图床的资源文件会将空格转为下划线,所以这里也需要将文档内容做相应的转换
  1605. ast.Walk(tree.Root, func(n *ast.Node, entering bool) ast.WalkStatus {
  1606. if !entering {
  1607. return ast.WalkContinue
  1608. }
  1609. if ast.NodeLinkDest == n.Type {
  1610. if util.IsAssetLinkDest(n.Tokens) {
  1611. n.Tokens = bytes.ReplaceAll(n.Tokens, []byte(" "), []byte("_"))
  1612. }
  1613. } else if n.IsTextMarkType("a") {
  1614. href := n.TextMarkAHref
  1615. if util.IsAssetLinkDest([]byte(href)) {
  1616. n.TextMarkAHref = strings.ReplaceAll(href, " ", "_")
  1617. }
  1618. }
  1619. return ast.WalkContinue
  1620. })
  1621. }
  1622. var unlinks []*ast.Node
  1623. ast.Walk(tree.Root, func(n *ast.Node, entering bool) ast.WalkStatus {
  1624. if !entering {
  1625. return ast.WalkContinue
  1626. }
  1627. if ast.NodeBr == n.Type {
  1628. if !n.ParentIs(ast.NodeTableCell) {
  1629. // When exporting Markdown, `<br />` nodes in non-tables are replaced with `\n` text nodes https://github.com/siyuan-note/siyuan/issues/9509
  1630. n.InsertBefore(&ast.Node{Type: ast.NodeText, Tokens: []byte("\n")})
  1631. unlinks = append(unlinks, n)
  1632. }
  1633. }
  1634. if 5 == blockRefMode { // 锚点哈希
  1635. if n.IsBlock() && gulu.Str.Contains(n.ID, defBlockIDs) {
  1636. // 如果是定义块,则在开头处添加锚点
  1637. anchorSpan := &ast.Node{Type: ast.NodeInlineHTML, Tokens: []byte("<span id=\"" + n.ID + "\"></span>")}
  1638. if ast.NodeDocument != n.Type {
  1639. firstLeaf := treenode.FirstLeafBlock(n)
  1640. if nil != firstLeaf && nil != firstLeaf.FirstChild {
  1641. firstLeaf.FirstChild.InsertBefore(anchorSpan)
  1642. } else {
  1643. n.AppendChild(anchorSpan)
  1644. }
  1645. }
  1646. }
  1647. if treenode.IsBlockRef(n) {
  1648. // 如果是引用元素,则将其转换为超链接,指向 xxx.md#block-id
  1649. defID, linkText := getExportBlockRefLinkText(n, blockRefTextLeft, blockRefTextRight)
  1650. if gulu.Str.Contains(defID, defBlockIDs) {
  1651. var href string
  1652. bt := treenode.GetBlockTree(defID)
  1653. if nil != bt {
  1654. href += strings.TrimPrefix(bt.HPath, "/") + ".md"
  1655. if "d" != bt.Type {
  1656. href += "#" + defID
  1657. }
  1658. }
  1659. blockRefLink := &ast.Node{Type: ast.NodeTextMark, TextMarkType: "a", TextMarkTextContent: linkText, TextMarkAHref: href}
  1660. blockRefLink.KramdownIAL = n.KramdownIAL
  1661. n.InsertBefore(blockRefLink)
  1662. unlinks = append(unlinks, n)
  1663. }
  1664. }
  1665. }
  1666. return ast.WalkContinue
  1667. })
  1668. for _, unlink := range unlinks {
  1669. unlink.Unlink()
  1670. }
  1671. renderer := render.NewProtyleExportMdRenderer(tree, luteEngine.RenderOptions)
  1672. ret = gulu.Str.FromBytes(renderer.Render())
  1673. return
  1674. }
  1675. func processKaTexMacros(n *ast.Node) {
  1676. if ast.NodeMathBlockContent != n.Type && ast.NodeTextMark != n.Type {
  1677. return
  1678. }
  1679. if ast.NodeTextMark == n.Type && !n.IsTextMarkType("inline-math") {
  1680. return
  1681. }
  1682. var mathContent string
  1683. if ast.NodeTextMark == n.Type {
  1684. mathContent = n.TextMarkInlineMathContent
  1685. } else {
  1686. mathContent = string(n.Tokens)
  1687. }
  1688. mathContent = strings.TrimSpace(mathContent)
  1689. if "" == mathContent {
  1690. return
  1691. }
  1692. macros := map[string]string{}
  1693. if err := gulu.JSON.UnmarshalJSON([]byte(Conf.Editor.KaTexMacros), &macros); nil != err {
  1694. logging.LogWarnf("parse katex macros failed: %s", err)
  1695. return
  1696. }
  1697. var keys []string
  1698. for k := range macros {
  1699. keys = append(keys, k)
  1700. }
  1701. useMacro := false
  1702. for k := range macros {
  1703. if strings.Contains(mathContent, k) {
  1704. useMacro = true
  1705. break
  1706. }
  1707. }
  1708. if !useMacro {
  1709. return
  1710. }
  1711. sort.Slice(keys, func(i, j int) bool { return len(keys[i]) > len(keys[j]) })
  1712. mathContent = escapeKaTexSupportedFunctions(mathContent)
  1713. usedMacros := extractUsedMacros(mathContent, &keys)
  1714. for _, usedMacro := range usedMacros {
  1715. depth := 1
  1716. expanded := resolveKaTexMacro(usedMacro, &macros, &keys, &depth)
  1717. expanded = unescapeKaTexSupportedFunctions(expanded)
  1718. mathContent = strings.ReplaceAll(mathContent, usedMacro, expanded)
  1719. }
  1720. mathContent = unescapeKaTexSupportedFunctions(mathContent)
  1721. if ast.NodeTextMark == n.Type {
  1722. n.TextMarkInlineMathContent = mathContent
  1723. } else {
  1724. n.Tokens = []byte(mathContent)
  1725. }
  1726. }
  1727. func exportTree(tree *parse.Tree, wysiwyg, expandKaTexMacros, keepFold bool,
  1728. blockRefMode, blockEmbedMode, fileAnnotationRefMode int,
  1729. tagOpenMarker, tagCloseMarker string,
  1730. blockRefTextLeft, blockRefTextRight string,
  1731. addTitle bool) (ret *parse.Tree) {
  1732. luteEngine := NewLute()
  1733. ret = tree
  1734. id := tree.Root.ID
  1735. var unlinks []*ast.Node
  1736. // 解析查询嵌入节点
  1737. ast.Walk(ret.Root, func(n *ast.Node, entering bool) ast.WalkStatus {
  1738. if !entering || ast.NodeBlockQueryEmbed != n.Type {
  1739. return ast.WalkContinue
  1740. }
  1741. var defMd string
  1742. stmt := n.ChildByType(ast.NodeBlockQueryEmbedScript).TokensStr()
  1743. stmt = html.UnescapeString(stmt)
  1744. stmt = strings.ReplaceAll(stmt, editor.IALValEscNewLine, "\n")
  1745. embedBlocks := searchEmbedBlock(n.ID, stmt, nil, 0, false)
  1746. if 1 > len(embedBlocks) {
  1747. return ast.WalkContinue
  1748. }
  1749. defMdBuf := bytes.Buffer{}
  1750. for _, def := range embedBlocks {
  1751. defMdBuf.WriteString(renderBlockMarkdownR(def.Block.ID))
  1752. defMdBuf.WriteString("\n\n")
  1753. }
  1754. defMd = defMdBuf.String()
  1755. buf := &bytes.Buffer{}
  1756. lines := strings.Split(defMd, "\n")
  1757. for i, line := range lines {
  1758. if 0 == blockEmbedMode { // 原始文本
  1759. buf.WriteString(line)
  1760. } else { // Blockquote
  1761. buf.WriteString("> " + line)
  1762. }
  1763. if i < len(lines)-1 {
  1764. buf.WriteString("\n")
  1765. }
  1766. }
  1767. buf.WriteString("\n\n")
  1768. refTree := parse.Parse("", buf.Bytes(), luteEngine.ParseOptions)
  1769. var children []*ast.Node
  1770. for c := refTree.Root.FirstChild; nil != c; c = c.Next {
  1771. children = append(children, c)
  1772. }
  1773. for _, c := range children {
  1774. if ast.NodeDocument == c.Type {
  1775. continue
  1776. }
  1777. n.InsertBefore(c)
  1778. }
  1779. unlinks = append(unlinks, n)
  1780. return ast.WalkSkipChildren
  1781. })
  1782. for _, n := range unlinks {
  1783. n.Unlink()
  1784. }
  1785. unlinks = nil
  1786. // 收集引用转脚注
  1787. var refFootnotes []*refAsFootnotes
  1788. if 4 == blockRefMode { // 块引转脚注
  1789. treeCache := map[string]*parse.Tree{}
  1790. treeCache[id] = ret
  1791. depth := 0
  1792. collectFootnotesDefs(ret.ID, &refFootnotes, &treeCache, &depth)
  1793. }
  1794. ast.Walk(ret.Root, func(n *ast.Node, entering bool) ast.WalkStatus {
  1795. if !entering {
  1796. return ast.WalkContinue
  1797. }
  1798. switch n.Type {
  1799. case ast.NodeSuperBlockOpenMarker, ast.NodeSuperBlockLayoutMarker, ast.NodeSuperBlockCloseMarker:
  1800. if !wysiwyg {
  1801. unlinks = append(unlinks, n)
  1802. return ast.WalkContinue
  1803. }
  1804. case ast.NodeHeading:
  1805. n.HeadingNormalizedID = n.IALAttr("id")
  1806. n.ID = n.HeadingNormalizedID
  1807. case ast.NodeMathBlockContent:
  1808. n.Tokens = bytes.TrimSpace(n.Tokens) // 导出 Markdown 时去除公式内容中的首尾空格 https://github.com/siyuan-note/siyuan/issues/4666
  1809. return ast.WalkContinue
  1810. case ast.NodeTextMark:
  1811. if n.IsTextMarkType("inline-math") {
  1812. n.TextMarkInlineMathContent = strings.TrimSpace(n.TextMarkInlineMathContent)
  1813. return ast.WalkContinue
  1814. } else if treenode.IsFileAnnotationRef(n) {
  1815. refID := n.TextMarkFileAnnotationRefID
  1816. if !strings.Contains(refID, "/") {
  1817. return ast.WalkSkipChildren
  1818. }
  1819. status := processFileAnnotationRef(refID, n, fileAnnotationRefMode)
  1820. unlinks = append(unlinks, n)
  1821. return status
  1822. } else if n.IsTextMarkType("tag") {
  1823. if !wysiwyg {
  1824. n.Type = ast.NodeText
  1825. n.Tokens = []byte(tagOpenMarker + n.TextMarkTextContent + tagCloseMarker)
  1826. return ast.WalkContinue
  1827. }
  1828. }
  1829. }
  1830. if !treenode.IsBlockRef(n) {
  1831. return ast.WalkContinue
  1832. }
  1833. // 处理引用节点
  1834. defID, linkText := getExportBlockRefLinkText(n, blockRefTextLeft, blockRefTextRight)
  1835. defTree, _ := LoadTreeByBlockID(defID)
  1836. if nil == defTree {
  1837. return ast.WalkContinue
  1838. }
  1839. switch blockRefMode {
  1840. case 2: // 锚文本块链
  1841. blockRefLink := &ast.Node{Type: ast.NodeTextMark, TextMarkType: "a", TextMarkTextContent: linkText, TextMarkAHref: "siyuan://blocks/" + defID}
  1842. blockRefLink.KramdownIAL = n.KramdownIAL
  1843. n.InsertBefore(blockRefLink)
  1844. unlinks = append(unlinks, n)
  1845. case 3: // 仅锚文本
  1846. blockRefLink := &ast.Node{Type: ast.NodeTextMark, TextMarkType: "text", TextMarkTextContent: linkText}
  1847. blockRefLink.KramdownIAL = n.KramdownIAL
  1848. n.InsertBefore(blockRefLink)
  1849. unlinks = append(unlinks, n)
  1850. case 4: // 脚注
  1851. refFoot := getRefAsFootnotes(defID, &refFootnotes)
  1852. n.InsertBefore(&ast.Node{Type: ast.NodeText, Tokens: []byte(linkText)})
  1853. n.InsertBefore(&ast.Node{Type: ast.NodeFootnotesRef, Tokens: []byte("^" + refFoot.refNum), FootnotesRefId: refFoot.refNum, FootnotesRefLabel: []byte("^" + refFoot.refNum)})
  1854. unlinks = append(unlinks, n)
  1855. case 5: // 锚点哈希
  1856. // 此处不做任何处理
  1857. }
  1858. if nil != n.Next && ast.NodeKramdownSpanIAL == n.Next.Type {
  1859. // 引用加排版标记(比如颜色)重叠时丢弃后面的排版属性节点
  1860. unlinks = append(unlinks, n.Next)
  1861. }
  1862. return ast.WalkSkipChildren
  1863. })
  1864. for _, n := range unlinks {
  1865. n.Unlink()
  1866. }
  1867. if 4 == blockRefMode { // 块引转脚注
  1868. unlinks = nil
  1869. if footnotesDefBlock := resolveFootnotesDefs(&refFootnotes, ret.Root.ID, blockRefTextLeft, blockRefTextRight); nil != footnotesDefBlock {
  1870. // 如果是聚焦导出,可能存在没有使用的脚注定义块,在这里进行清理
  1871. // Improve focus export conversion of block refs to footnotes https://github.com/siyuan-note/siyuan/issues/10647
  1872. footnotesRefs := ret.Root.ChildrenByType(ast.NodeFootnotesRef)
  1873. for footnotesDef := footnotesDefBlock.FirstChild; nil != footnotesDef; footnotesDef = footnotesDef.Next {
  1874. exist := false
  1875. for _, ref := range footnotesRefs {
  1876. if ref.FootnotesRefId == footnotesDef.FootnotesRefId {
  1877. exist = true
  1878. break
  1879. }
  1880. }
  1881. if !exist {
  1882. unlinks = append(unlinks, footnotesDef)
  1883. }
  1884. }
  1885. for _, n := range unlinks {
  1886. n.Unlink()
  1887. }
  1888. ret.Root.AppendChild(footnotesDefBlock)
  1889. }
  1890. }
  1891. if addTitle {
  1892. if root, _ := getBlock(id, tree); nil != root {
  1893. root.IAL["type"] = "doc"
  1894. title := &ast.Node{Type: ast.NodeHeading, HeadingLevel: 1, KramdownIAL: parse.Map2IAL(root.IAL)}
  1895. content := html.UnescapeString(root.Content)
  1896. title.AppendChild(&ast.Node{Type: ast.NodeText, Tokens: []byte(content)})
  1897. ret.Root.PrependChild(title)
  1898. }
  1899. }
  1900. // 导出时支持导出题头图 https://github.com/siyuan-note/siyuan/issues/4372
  1901. titleImgPath := treenode.GetDocTitleImgPath(ret.Root)
  1902. if "" != titleImgPath {
  1903. p := &ast.Node{Type: ast.NodeParagraph}
  1904. titleImg := &ast.Node{Type: ast.NodeImage}
  1905. titleImg.AppendChild(&ast.Node{Type: ast.NodeBang})
  1906. titleImg.AppendChild(&ast.Node{Type: ast.NodeOpenBracket})
  1907. titleImg.AppendChild(&ast.Node{Type: ast.NodeLinkText, Tokens: []byte("image")})
  1908. titleImg.AppendChild(&ast.Node{Type: ast.NodeCloseBracket})
  1909. titleImg.AppendChild(&ast.Node{Type: ast.NodeOpenParen})
  1910. titleImg.AppendChild(&ast.Node{Type: ast.NodeLinkDest, Tokens: []byte(titleImgPath)})
  1911. titleImg.AppendChild(&ast.Node{Type: ast.NodeCloseParen})
  1912. p.AppendChild(titleImg)
  1913. ret.Root.PrependChild(p)
  1914. }
  1915. unlinks = nil
  1916. var emptyParagraphs []*ast.Node
  1917. ast.Walk(ret.Root, func(n *ast.Node, entering bool) ast.WalkStatus {
  1918. if !entering {
  1919. return ast.WalkContinue
  1920. }
  1921. // 支持按照现有折叠状态导出 PDF https://github.com/siyuan-note/siyuan/issues/5941
  1922. if !keepFold {
  1923. // 块折叠以后导出 HTML/PDF 固定展开 https://github.com/siyuan-note/siyuan/issues/4064
  1924. n.RemoveIALAttr("fold")
  1925. n.RemoveIALAttr("heading-fold")
  1926. } else {
  1927. if "1" == n.IALAttr("heading-fold") {
  1928. unlinks = append(unlinks, n)
  1929. return ast.WalkContinue
  1930. }
  1931. }
  1932. // 导出时去掉内容块闪卡样式 https://github.com/siyuan-note/siyuan/issues/7374
  1933. if n.IsBlock() {
  1934. n.RemoveIALAttr("custom-riff-decks")
  1935. }
  1936. switch n.Type {
  1937. case ast.NodeParagraph:
  1938. if nil == n.FirstChild {
  1939. // 空的段落块需要补全文本展位,否则后续格式化后再解析树会语义不一致 https://github.com/siyuan-note/siyuan/issues/5806
  1940. emptyParagraphs = append(emptyParagraphs, n)
  1941. }
  1942. case ast.NodeMathBlockContent:
  1943. if expandKaTexMacros {
  1944. processKaTexMacros(n)
  1945. }
  1946. case ast.NodeTextMark:
  1947. if expandKaTexMacros && n.IsTextMarkType("inline-math") {
  1948. processKaTexMacros(n)
  1949. }
  1950. case ast.NodeWidget:
  1951. // 挂件块导出 https://github.com/siyuan-note/siyuan/issues/3834 https://github.com/siyuan-note/siyuan/issues/6188
  1952. if wysiwyg {
  1953. exportHtmlVal := n.IALAttr("data-export-html")
  1954. if "" != exportHtmlVal {
  1955. htmlBlock := &ast.Node{Type: ast.NodeHTMLBlock, Tokens: []byte(exportHtmlVal)}
  1956. n.InsertBefore(htmlBlock)
  1957. unlinks = append(unlinks, n)
  1958. return ast.WalkContinue
  1959. }
  1960. }
  1961. exportMdVal := n.IALAttr("data-export-md")
  1962. exportMdVal = html.UnescapeString(exportMdVal) // 导出 `data-export-md` 时未解析代码块与行内代码内的转义字符 https://github.com/siyuan-note/siyuan/issues/4180
  1963. if "" != exportMdVal {
  1964. luteEngine0 := util.NewLute()
  1965. luteEngine0.SetYamlFrontMatter(true) // 挂件导出属性 `data-export-md` 支持 YFM https://github.com/siyuan-note/siyuan/issues/7752
  1966. exportMdTree := parse.Parse("", []byte(exportMdVal), luteEngine0.ParseOptions)
  1967. var insertNodes []*ast.Node
  1968. for c := exportMdTree.Root.FirstChild; nil != c; c = c.Next {
  1969. if ast.NodeKramdownBlockIAL != c.Type {
  1970. insertNodes = append(insertNodes, c)
  1971. }
  1972. }
  1973. for _, insertNode := range insertNodes {
  1974. n.InsertBefore(insertNode)
  1975. }
  1976. unlinks = append(unlinks, n)
  1977. }
  1978. case ast.NodeSuperBlockOpenMarker, ast.NodeSuperBlockLayoutMarker, ast.NodeSuperBlockCloseMarker:
  1979. if !wysiwyg {
  1980. unlinks = append(unlinks, n)
  1981. }
  1982. }
  1983. if ast.NodeText != n.Type {
  1984. return ast.WalkContinue
  1985. }
  1986. // Shift+Enter 换行在导出为 Markdown 时使用硬换行 https://github.com/siyuan-note/siyuan/issues/3458
  1987. n.Tokens = bytes.ReplaceAll(n.Tokens, []byte("\n"), []byte(" \n"))
  1988. return ast.WalkContinue
  1989. })
  1990. for _, n := range unlinks {
  1991. n.Unlink()
  1992. }
  1993. for _, emptyParagraph := range emptyParagraphs {
  1994. emptyParagraph.AppendChild(&ast.Node{Type: ast.NodeText, Tokens: []byte(editor.Zwj)})
  1995. }
  1996. unlinks = nil
  1997. // Attribute View export https://github.com/siyuan-note/siyuan/issues/8710
  1998. ast.Walk(ret.Root, func(n *ast.Node, entering bool) ast.WalkStatus {
  1999. if !entering {
  2000. return ast.WalkContinue
  2001. }
  2002. if ast.NodeAttributeView != n.Type {
  2003. return ast.WalkContinue
  2004. }
  2005. avID := n.AttributeViewID
  2006. if avJSONPath := av.GetAttributeViewDataPath(avID); !filelock.IsExist(avJSONPath) {
  2007. return ast.WalkContinue
  2008. }
  2009. attrView, err := av.ParseAttributeView(avID)
  2010. if nil != err {
  2011. logging.LogErrorf("parse attribute view [%s] failed: %s", avID, err)
  2012. return ast.WalkContinue
  2013. }
  2014. viewID := n.IALAttr(av.NodeAttrView)
  2015. view, err := attrView.GetCurrentView(viewID)
  2016. if nil != err {
  2017. logging.LogErrorf("get attribute view [%s] failed: %s", avID, err)
  2018. return ast.WalkContinue
  2019. }
  2020. table, err := renderAttributeViewTable(attrView, view, "")
  2021. if nil != err {
  2022. logging.LogErrorf("render attribute view [%s] table failed: %s", avID, err)
  2023. return ast.WalkContinue
  2024. }
  2025. // 遵循视图过滤和排序规则 Use filtering and sorting of current view settings when exporting database blocks https://github.com/siyuan-note/siyuan/issues/10474
  2026. table.FilterRows(attrView)
  2027. table.SortRows(attrView)
  2028. var aligns []int
  2029. for range table.Columns {
  2030. aligns = append(aligns, 0)
  2031. }
  2032. mdTable := &ast.Node{Type: ast.NodeTable, TableAligns: aligns}
  2033. mdTableHead := &ast.Node{Type: ast.NodeTableHead}
  2034. mdTable.AppendChild(mdTableHead)
  2035. mdTableHeadRow := &ast.Node{Type: ast.NodeTableRow, TableAligns: aligns}
  2036. mdTableHead.AppendChild(mdTableHeadRow)
  2037. for _, col := range table.Columns {
  2038. cell := &ast.Node{Type: ast.NodeTableCell}
  2039. cell.AppendChild(&ast.Node{Type: ast.NodeText, Tokens: []byte(col.Name)})
  2040. mdTableHeadRow.AppendChild(cell)
  2041. }
  2042. for _, row := range table.Rows {
  2043. mdTableRow := &ast.Node{Type: ast.NodeTableRow, TableAligns: aligns}
  2044. mdTable.AppendChild(mdTableRow)
  2045. for _, cell := range row.Cells {
  2046. mdTableCell := &ast.Node{Type: ast.NodeTableCell}
  2047. mdTableRow.AppendChild(mdTableCell)
  2048. var val string
  2049. if nil != cell.Value {
  2050. if av.KeyTypeDate == cell.Value.Type {
  2051. if nil != cell.Value.Date {
  2052. cell.Value.Date = av.NewFormattedValueDate(cell.Value.Date.Content, cell.Value.Date.Content2, av.DateFormatNone, cell.Value.Date.IsNotTime, cell.Value.Date.HasEndDate)
  2053. }
  2054. } else if av.KeyTypeCreated == cell.Value.Type {
  2055. if nil != cell.Value.Created {
  2056. cell.Value.Created = av.NewFormattedValueCreated(cell.Value.Created.Content, 0, av.CreatedFormatNone)
  2057. }
  2058. } else if av.KeyTypeUpdated == cell.Value.Type {
  2059. if nil != cell.Value.Updated {
  2060. cell.Value.Updated = av.NewFormattedValueUpdated(cell.Value.Updated.Content, 0, av.UpdatedFormatNone)
  2061. }
  2062. } else if av.KeyTypeMAsset == cell.Value.Type {
  2063. if nil != cell.Value.MAsset {
  2064. buf := &bytes.Buffer{}
  2065. for _, a := range cell.Value.MAsset {
  2066. buf.WriteString("![](")
  2067. buf.WriteString(a.Content)
  2068. buf.WriteString(") ")
  2069. }
  2070. val = strings.TrimSpace(buf.String())
  2071. mdTableCell.AppendChild(&ast.Node{Type: ast.NodeText, Tokens: []byte(val)})
  2072. continue
  2073. }
  2074. }
  2075. val = cell.Value.String()
  2076. }
  2077. mdTableCell.AppendChild(&ast.Node{Type: ast.NodeText, Tokens: []byte(val)})
  2078. }
  2079. }
  2080. n.InsertBefore(mdTable)
  2081. unlinks = append(unlinks, n)
  2082. return ast.WalkContinue
  2083. })
  2084. for _, n := range unlinks {
  2085. n.Unlink()
  2086. }
  2087. return ret
  2088. }
  2089. func resolveFootnotesDefs(refFootnotes *[]*refAsFootnotes, rootID string, blockRefTextLeft, blockRefTextRight string) (footnotesDefBlock *ast.Node) {
  2090. if 1 > len(*refFootnotes) {
  2091. return nil
  2092. }
  2093. footnotesDefBlock = &ast.Node{Type: ast.NodeFootnotesDefBlock}
  2094. var rendered []string
  2095. for _, foot := range *refFootnotes {
  2096. t, err := LoadTreeByBlockID(foot.defID)
  2097. if nil != err {
  2098. continue
  2099. }
  2100. defNode := treenode.GetNodeInTree(t, foot.defID)
  2101. docID := strings.TrimSuffix(path.Base(defNode.Path), ".sy")
  2102. var nodes []*ast.Node
  2103. if ast.NodeHeading == defNode.Type {
  2104. nodes = append(nodes, defNode)
  2105. if rootID != docID {
  2106. // 同文档块引转脚注缩略定义考虑容器块和标题块 https://github.com/siyuan-note/siyuan/issues/5917
  2107. children := treenode.HeadingChildren(defNode)
  2108. nodes = append(nodes, children...)
  2109. }
  2110. } else if ast.NodeDocument == defNode.Type {
  2111. docTitle := &ast.Node{ID: defNode.ID, Type: ast.NodeHeading, HeadingLevel: 1}
  2112. docTitle.AppendChild(&ast.Node{Type: ast.NodeText, Tokens: []byte(defNode.IALAttr("title"))})
  2113. nodes = append(nodes, docTitle)
  2114. for c := defNode.FirstChild; nil != c; c = c.Next {
  2115. nodes = append(nodes, c)
  2116. }
  2117. } else {
  2118. nodes = append(nodes, defNode)
  2119. }
  2120. var newNodes []*ast.Node
  2121. for _, node := range nodes {
  2122. var unlinks []*ast.Node
  2123. ast.Walk(node, func(n *ast.Node, entering bool) ast.WalkStatus {
  2124. if !entering {
  2125. return ast.WalkContinue
  2126. }
  2127. if treenode.IsBlockRef(n) {
  2128. defID, _, _ := treenode.GetBlockRef(n)
  2129. if f := getRefAsFootnotes(defID, refFootnotes); nil != f {
  2130. n.InsertBefore(&ast.Node{Type: ast.NodeText, Tokens: []byte(blockRefTextLeft + f.refAnchorText + blockRefTextRight)})
  2131. n.InsertBefore(&ast.Node{Type: ast.NodeFootnotesRef, Tokens: []byte("^" + f.refNum), FootnotesRefId: f.refNum, FootnotesRefLabel: []byte("^" + f.refNum)})
  2132. unlinks = append(unlinks, n)
  2133. }
  2134. return ast.WalkSkipChildren
  2135. } else if ast.NodeBlockQueryEmbed == n.Type {
  2136. stmt := n.ChildByType(ast.NodeBlockQueryEmbedScript).TokensStr()
  2137. stmt = html.UnescapeString(stmt)
  2138. stmt = strings.ReplaceAll(stmt, editor.IALValEscNewLine, "\n")
  2139. sqlBlocks := sql.SelectBlocksRawStmt(stmt, 1, Conf.Search.Limit)
  2140. for _, b := range sqlBlocks {
  2141. subNodes := renderBlockMarkdownR0(b.ID, &rendered)
  2142. for _, subNode := range subNodes {
  2143. if ast.NodeListItem == subNode.Type {
  2144. parentList := &ast.Node{Type: ast.NodeList, ListData: &ast.ListData{Typ: subNode.ListData.Typ}}
  2145. parentList.AppendChild(subNode)
  2146. newNodes = append(newNodes, parentList)
  2147. } else {
  2148. newNodes = append(newNodes, subNode)
  2149. }
  2150. }
  2151. }
  2152. unlinks = append(unlinks, n)
  2153. return ast.WalkSkipChildren
  2154. }
  2155. return ast.WalkContinue
  2156. })
  2157. for _, n := range unlinks {
  2158. n.Unlink()
  2159. }
  2160. if ast.NodeBlockQueryEmbed != node.Type {
  2161. if ast.NodeListItem == node.Type {
  2162. parentList := &ast.Node{Type: ast.NodeList, ListData: &ast.ListData{Typ: node.ListData.Typ}}
  2163. parentList.AppendChild(node)
  2164. newNodes = append(newNodes, parentList)
  2165. } else {
  2166. newNodes = append(newNodes, node)
  2167. }
  2168. }
  2169. }
  2170. footnotesDef := &ast.Node{Type: ast.NodeFootnotesDef, Tokens: []byte("^" + foot.refNum), FootnotesRefId: foot.refNum, FootnotesRefLabel: []byte("^" + foot.refNum)}
  2171. for _, node := range newNodes {
  2172. ast.Walk(node, func(n *ast.Node, entering bool) ast.WalkStatus {
  2173. if !entering {
  2174. return ast.WalkContinue
  2175. }
  2176. if ast.NodeParagraph != n.Type {
  2177. return ast.WalkContinue
  2178. }
  2179. docID := strings.TrimSuffix(path.Base(n.Path), ".sy")
  2180. if rootID == docID {
  2181. // 同文档块引转脚注缩略定义 https://github.com/siyuan-note/siyuan/issues/3299
  2182. if text := sql.GetRefText(n.ID); 64 < utf8.RuneCountInString(text) {
  2183. var unlinkChildren []*ast.Node
  2184. for c := n.FirstChild; nil != c; c = c.Next {
  2185. unlinkChildren = append(unlinkChildren, c)
  2186. }
  2187. for _, c := range unlinkChildren {
  2188. c.Unlink()
  2189. }
  2190. text = gulu.Str.SubStr(text, 64) + "..."
  2191. n.AppendChild(&ast.Node{Type: ast.NodeText, Tokens: []byte(text)})
  2192. return ast.WalkSkipChildren
  2193. }
  2194. }
  2195. return ast.WalkContinue
  2196. })
  2197. footnotesDef.AppendChild(node)
  2198. }
  2199. footnotesDefBlock.AppendChild(footnotesDef)
  2200. }
  2201. return
  2202. }
  2203. func collectFootnotesDefs(id string, refFootnotes *[]*refAsFootnotes, treeCache *map[string]*parse.Tree, depth *int) {
  2204. *depth++
  2205. if 4096 < *depth {
  2206. return
  2207. }
  2208. b := treenode.GetBlockTree(id)
  2209. if nil == b {
  2210. return
  2211. }
  2212. t := (*treeCache)[b.RootID]
  2213. if nil == t {
  2214. var err error
  2215. if t, err = LoadTreeByBlockID(b.ID); nil != err {
  2216. return
  2217. }
  2218. (*treeCache)[t.ID] = t
  2219. }
  2220. node := treenode.GetNodeInTree(t, b.ID)
  2221. if nil == node {
  2222. logging.LogErrorf("not found node [%s] in tree [%s]", b.ID, t.Root.ID)
  2223. return
  2224. }
  2225. collectFootnotesDefs0(node, refFootnotes, treeCache, depth)
  2226. if ast.NodeHeading == node.Type {
  2227. children := treenode.HeadingChildren(node)
  2228. for _, c := range children {
  2229. collectFootnotesDefs0(c, refFootnotes, treeCache, depth)
  2230. }
  2231. }
  2232. return
  2233. }
  2234. func collectFootnotesDefs0(node *ast.Node, refFootnotes *[]*refAsFootnotes, treeCache *map[string]*parse.Tree, depth *int) {
  2235. ast.Walk(node, func(n *ast.Node, entering bool) ast.WalkStatus {
  2236. if !entering {
  2237. return ast.WalkContinue
  2238. }
  2239. if treenode.IsBlockRef(n) {
  2240. defID, refText, _ := treenode.GetBlockRef(n)
  2241. if nil == getRefAsFootnotes(defID, refFootnotes) {
  2242. anchorText := refText
  2243. if Conf.Editor.BlockRefDynamicAnchorTextMaxLen < utf8.RuneCountInString(anchorText) {
  2244. anchorText = gulu.Str.SubStr(anchorText, Conf.Editor.BlockRefDynamicAnchorTextMaxLen) + "..."
  2245. }
  2246. *refFootnotes = append(*refFootnotes, &refAsFootnotes{
  2247. defID: defID,
  2248. refNum: strconv.Itoa(len(*refFootnotes) + 1),
  2249. refAnchorText: anchorText,
  2250. })
  2251. collectFootnotesDefs(defID, refFootnotes, treeCache, depth)
  2252. }
  2253. return ast.WalkSkipChildren
  2254. }
  2255. return ast.WalkContinue
  2256. })
  2257. }
  2258. func getRefAsFootnotes(defID string, slice *[]*refAsFootnotes) *refAsFootnotes {
  2259. for _, e := range *slice {
  2260. if e.defID == defID {
  2261. return e
  2262. }
  2263. }
  2264. return nil
  2265. }
  2266. type refAsFootnotes struct {
  2267. defID string
  2268. refNum string
  2269. refAnchorText string
  2270. }
  2271. func exportRefTrees(tree *parse.Tree) (ret map[string]*parse.Tree) {
  2272. ret = map[string]*parse.Tree{}
  2273. exportRefTrees0(tree, &ret)
  2274. return
  2275. }
  2276. func exportRefTrees0(tree *parse.Tree, retTrees *map[string]*parse.Tree) {
  2277. if nil != (*retTrees)[tree.ID] {
  2278. return
  2279. }
  2280. (*retTrees)[tree.ID] = tree
  2281. ast.Walk(tree.Root, func(n *ast.Node, entering bool) ast.WalkStatus {
  2282. if !entering {
  2283. return ast.WalkContinue
  2284. }
  2285. if treenode.IsBlockRef(n) {
  2286. defID, _, _ := treenode.GetBlockRef(n)
  2287. if "" == defID {
  2288. return ast.WalkContinue
  2289. }
  2290. defBlock := treenode.GetBlockTree(defID)
  2291. if nil == defBlock {
  2292. return ast.WalkSkipChildren
  2293. }
  2294. defTree, err := LoadTreeByBlockID(defBlock.RootID)
  2295. if nil != err {
  2296. return ast.WalkSkipChildren
  2297. }
  2298. exportRefTrees0(defTree, retTrees)
  2299. }
  2300. return ast.WalkContinue
  2301. })
  2302. }
  2303. func processFileAnnotationRef(refID string, n *ast.Node, fileAnnotationRefMode int) ast.WalkStatus {
  2304. p := refID[:strings.LastIndex(refID, "/")]
  2305. absPath, err := GetAssetAbsPath(p)
  2306. if nil != err {
  2307. logging.LogWarnf("get assets abs path by rel path [%s] failed: %s", p, err)
  2308. return ast.WalkSkipChildren
  2309. }
  2310. sya := absPath + ".sya"
  2311. syaData, err := os.ReadFile(sya)
  2312. if nil != err {
  2313. logging.LogErrorf("read file [%s] failed: %s", sya, err)
  2314. return ast.WalkSkipChildren
  2315. }
  2316. syaJSON := map[string]interface{}{}
  2317. if err = gulu.JSON.UnmarshalJSON(syaData, &syaJSON); nil != err {
  2318. logging.LogErrorf("unmarshal file [%s] failed: %s", sya, err)
  2319. return ast.WalkSkipChildren
  2320. }
  2321. annotationID := refID[strings.LastIndex(refID, "/")+1:]
  2322. annotationData := syaJSON[annotationID]
  2323. if nil == annotationData {
  2324. logging.LogErrorf("not found annotation [%s] in .sya", annotationID)
  2325. return ast.WalkSkipChildren
  2326. }
  2327. pages := annotationData.(map[string]interface{})["pages"].([]interface{})
  2328. page := int(pages[0].(map[string]interface{})["index"].(float64)) + 1
  2329. pageStr := strconv.Itoa(page)
  2330. refText := n.TextMarkTextContent
  2331. ext := filepath.Ext(p)
  2332. file := p[7:len(p)-23-len(ext)] + ext
  2333. fileAnnotationRefLink := &ast.Node{Type: ast.NodeLink}
  2334. fileAnnotationRefLink.AppendChild(&ast.Node{Type: ast.NodeOpenBracket})
  2335. if 0 == fileAnnotationRefMode {
  2336. fileAnnotationRefLink.AppendChild(&ast.Node{Type: ast.NodeLinkText, Tokens: []byte(file + " - p" + pageStr + " - " + refText)})
  2337. } else {
  2338. fileAnnotationRefLink.AppendChild(&ast.Node{Type: ast.NodeLinkText, Tokens: []byte(refText)})
  2339. }
  2340. fileAnnotationRefLink.AppendChild(&ast.Node{Type: ast.NodeCloseBracket})
  2341. fileAnnotationRefLink.AppendChild(&ast.Node{Type: ast.NodeOpenParen})
  2342. fileAnnotationRefLink.AppendChild(&ast.Node{Type: ast.NodeLinkDest, Tokens: []byte(p + "?p=" + pageStr)})
  2343. fileAnnotationRefLink.AppendChild(&ast.Node{Type: ast.NodeCloseParen})
  2344. n.InsertBefore(fileAnnotationRefLink)
  2345. return ast.WalkSkipChildren
  2346. }
  2347. func exportPandocConvertZip(exportNotebook bool, boxID, baseFolderName string, docPaths []string,
  2348. pandocFrom, pandocTo, ext string) (zipPath string) {
  2349. dir, name := path.Split(baseFolderName)
  2350. name = util.FilterFileName(name)
  2351. if strings.HasSuffix(name, "..") {
  2352. // 文档标题以 `..` 结尾时无法导出 Markdown https://github.com/siyuan-note/siyuan/issues/4698
  2353. // 似乎是 os.MkdirAll 的 bug,以 .. 结尾的路径无法创建,所以这里加上 _ 结尾
  2354. name += "_"
  2355. }
  2356. baseFolderName = path.Join(dir, name)
  2357. box := Conf.Box(boxID)
  2358. exportFolder := filepath.Join(util.TempDir, "export", baseFolderName+ext)
  2359. os.RemoveAll(exportFolder)
  2360. if err := os.MkdirAll(exportFolder, 0755); nil != err {
  2361. logging.LogErrorf("create export temp folder failed: %s", err)
  2362. return
  2363. }
  2364. exportRefMode := Conf.Export.BlockRefMode
  2365. if !exportNotebook && 5 == exportRefMode {
  2366. // 非笔记本导出不支持锚点哈希,将其切换为锚文本块链
  2367. exportRefMode = 2
  2368. }
  2369. var defBlockIDs []string
  2370. if exportNotebook && 5 == exportRefMode {
  2371. // Add a Ref export mode `Anchor hash` for notebook Markdown exporting https://github.com/siyuan-note/siyuan/issues/10265
  2372. // 导出笔记本时导出锚点哈希,这里先记录下所有定义块的 ID
  2373. for _, p := range docPaths {
  2374. docIAL := box.docIAL(p)
  2375. if nil == docIAL {
  2376. continue
  2377. }
  2378. id := docIAL["id"]
  2379. tree, err := LoadTreeByBlockID(id)
  2380. if nil != err {
  2381. continue
  2382. }
  2383. ast.Walk(tree.Root, func(n *ast.Node, entering bool) ast.WalkStatus {
  2384. if !entering {
  2385. return ast.WalkContinue
  2386. }
  2387. if treenode.IsBlockRef(n) {
  2388. defID, _, _ := treenode.GetBlockRef(n)
  2389. defBlockIDs = append(defBlockIDs, defID)
  2390. }
  2391. return ast.WalkContinue
  2392. })
  2393. }
  2394. defBlockIDs = gulu.Str.RemoveDuplicatedElem(defBlockIDs)
  2395. }
  2396. luteEngine := util.NewLute()
  2397. for _, p := range docPaths {
  2398. docIAL := box.docIAL(p)
  2399. if nil == docIAL {
  2400. continue
  2401. }
  2402. id := docIAL["id"]
  2403. hPath, md := exportMarkdownContent(id, exportRefMode, defBlockIDs)
  2404. dir, name = path.Split(hPath)
  2405. dir = util.FilterFilePath(dir) // 导出文档时未移除不支持的文件名符号 https://github.com/siyuan-note/siyuan/issues/4590
  2406. name = util.FilterFileName(name)
  2407. hPath = path.Join(dir, name)
  2408. p = hPath + ext
  2409. writePath := filepath.Join(exportFolder, p)
  2410. if gulu.File.IsExist(writePath) {
  2411. // 重名文档加 ID
  2412. p = hPath + "-" + id + ext
  2413. writePath = filepath.Join(exportFolder, p)
  2414. }
  2415. writeFolder := filepath.Dir(writePath)
  2416. if err := os.MkdirAll(writeFolder, 0755); nil != err {
  2417. logging.LogErrorf("create export temp folder [%s] failed: %s", writeFolder, err)
  2418. continue
  2419. }
  2420. // 解析导出后的标准 Markdown,汇总 assets
  2421. tree := parse.Parse("", gulu.Str.ToBytes(md), luteEngine.ParseOptions)
  2422. var assets []string
  2423. assets = append(assets, assetsLinkDestsInTree(tree)...)
  2424. for _, asset := range assets {
  2425. asset = string(html.DecodeDestination([]byte(asset)))
  2426. if strings.Contains(asset, "?") {
  2427. asset = asset[:strings.LastIndex(asset, "?")]
  2428. }
  2429. if !strings.HasPrefix(asset, "assets/") {
  2430. continue
  2431. }
  2432. srcPath, err := GetAssetAbsPath(asset)
  2433. if nil != err {
  2434. logging.LogWarnf("get asset [%s] abs path failed: %s", asset, err)
  2435. continue
  2436. }
  2437. destPath := filepath.Join(writeFolder, asset)
  2438. err = filelock.Copy(srcPath, destPath)
  2439. if nil != err {
  2440. logging.LogErrorf("copy asset from [%s] to [%s] failed: %s", srcPath, destPath, err)
  2441. continue
  2442. }
  2443. }
  2444. // 调用 Pandoc 进行格式转换
  2445. err := util.Pandoc(pandocFrom, pandocTo, writePath, md)
  2446. if nil != err {
  2447. logging.LogErrorf("pandoc failed: %s", err)
  2448. continue
  2449. }
  2450. }
  2451. zipPath = exportFolder + ".zip"
  2452. zip, err := gulu.Zip.Create(zipPath)
  2453. if nil != err {
  2454. logging.LogErrorf("create export markdown zip [%s] failed: %s", exportFolder, err)
  2455. return ""
  2456. }
  2457. // 导出 Markdown zip 包内不带文件夹 https://github.com/siyuan-note/siyuan/issues/6869
  2458. entries, err := os.ReadDir(exportFolder)
  2459. if nil != err {
  2460. logging.LogErrorf("read export markdown folder [%s] failed: %s", exportFolder, err)
  2461. return ""
  2462. }
  2463. for _, entry := range entries {
  2464. entryPath := filepath.Join(exportFolder, entry.Name())
  2465. if gulu.File.IsDir(entryPath) {
  2466. err = zip.AddDirectory(entry.Name(), entryPath)
  2467. } else {
  2468. err = zip.AddEntry(entry.Name(), entryPath)
  2469. }
  2470. if nil != err {
  2471. logging.LogErrorf("add entry [%s] to zip failed: %s", entry.Name(), err)
  2472. return ""
  2473. }
  2474. }
  2475. if err = zip.Close(); nil != err {
  2476. logging.LogErrorf("close export markdown zip failed: %s", err)
  2477. }
  2478. os.RemoveAll(exportFolder)
  2479. zipPath = "/export/" + url.PathEscape(filepath.Base(zipPath))
  2480. return
  2481. }
  2482. func getExportBlockRefLinkText(blockRef *ast.Node, blockRefTextLeft, blockRefTextRight string) (defID, linkText string) {
  2483. defID, linkText, _ = treenode.GetBlockRef(blockRef)
  2484. if "" == linkText {
  2485. linkText = sql.GetRefText(defID)
  2486. }
  2487. linkText = html.UnescapeHTMLStr(linkText) // 块引锚文本导出时 `&` 变为实体 `&amp;` https://github.com/siyuan-note/siyuan/issues/7659
  2488. if Conf.Editor.BlockRefDynamicAnchorTextMaxLen < utf8.RuneCountInString(linkText) {
  2489. linkText = gulu.Str.SubStr(linkText, Conf.Editor.BlockRefDynamicAnchorTextMaxLen) + "..."
  2490. }
  2491. linkText = blockRefTextLeft + linkText + blockRefTextRight
  2492. return
  2493. }