export.go 84 KB

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