filetree.go 24 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111
  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 api
  17. import (
  18. "fmt"
  19. "math"
  20. "net/http"
  21. "os"
  22. "path"
  23. "path/filepath"
  24. "regexp"
  25. "strings"
  26. "unicode/utf8"
  27. "github.com/88250/gulu"
  28. "github.com/88250/lute/ast"
  29. "github.com/gin-gonic/gin"
  30. "github.com/siyuan-note/siyuan/kernel/filesys"
  31. "github.com/siyuan-note/siyuan/kernel/model"
  32. "github.com/siyuan-note/siyuan/kernel/util"
  33. )
  34. func listDocTree(c *gin.Context) {
  35. // Add kernel API `/api/filetree/listDocTree` https://github.com/siyuan-note/siyuan/issues/10482
  36. ret := gulu.Ret.NewResult()
  37. defer c.JSON(http.StatusOK, ret)
  38. arg, ok := util.JsonArg(c, ret)
  39. if !ok {
  40. return
  41. }
  42. notebook := arg["notebook"].(string)
  43. if util.InvalidIDPattern(notebook, ret) {
  44. return
  45. }
  46. p := arg["path"].(string)
  47. var doctree []*DocFile
  48. root := filepath.Join(util.WorkspaceDir, "data", notebook, p)
  49. dir, err := os.ReadDir(root)
  50. if err != nil {
  51. ret.Code = -1
  52. ret.Msg = err.Error()
  53. return
  54. }
  55. ids := map[string]bool{}
  56. for _, entry := range dir {
  57. if entry.IsDir() {
  58. if strings.HasPrefix(entry.Name(), ".") {
  59. continue
  60. }
  61. if !ast.IsNodeIDPattern(entry.Name()) {
  62. continue
  63. }
  64. parent := &DocFile{ID: entry.Name()}
  65. ids[parent.ID] = true
  66. doctree = append(doctree, parent)
  67. subPath := filepath.Join(root, entry.Name())
  68. if err = walkDocTree(subPath, parent, &ids); err != nil {
  69. ret.Code = -1
  70. ret.Msg = err.Error()
  71. return
  72. }
  73. } else {
  74. doc := &DocFile{ID: strings.TrimSuffix(entry.Name(), ".sy")}
  75. if !ids[doc.ID] {
  76. doctree = append(doctree, doc)
  77. }
  78. ids[doc.ID] = true
  79. }
  80. }
  81. ret.Data = map[string]interface{}{
  82. "tree": doctree,
  83. }
  84. }
  85. type DocFile struct {
  86. ID string `json:"id"`
  87. Children []*DocFile `json:"children,omitempty"`
  88. }
  89. func walkDocTree(p string, docFile *DocFile, ids *map[string]bool) (err error) {
  90. dir, err := os.ReadDir(p)
  91. if err != nil {
  92. return
  93. }
  94. for _, entry := range dir {
  95. if entry.IsDir() {
  96. if strings.HasPrefix(entry.Name(), ".") {
  97. continue
  98. }
  99. if !ast.IsNodeIDPattern(entry.Name()) {
  100. continue
  101. }
  102. parent := &DocFile{ID: entry.Name()}
  103. (*ids)[parent.ID] = true
  104. docFile.Children = append(docFile.Children, parent)
  105. subPath := filepath.Join(p, entry.Name())
  106. if err = walkDocTree(subPath, parent, ids); err != nil {
  107. return
  108. }
  109. } else {
  110. doc := &DocFile{ID: strings.TrimSuffix(entry.Name(), ".sy")}
  111. if !(*ids)[doc.ID] {
  112. docFile.Children = append(docFile.Children, doc)
  113. }
  114. (*ids)[doc.ID] = true
  115. }
  116. }
  117. return
  118. }
  119. func upsertIndexes(c *gin.Context) {
  120. ret := gulu.Ret.NewResult()
  121. defer c.JSON(http.StatusOK, ret)
  122. arg, ok := util.JsonArg(c, ret)
  123. if !ok {
  124. return
  125. }
  126. pathsArg := arg["paths"].([]interface{})
  127. var paths []string
  128. for _, p := range pathsArg {
  129. paths = append(paths, p.(string))
  130. }
  131. model.UpsertIndexes(paths)
  132. }
  133. func removeIndexes(c *gin.Context) {
  134. ret := gulu.Ret.NewResult()
  135. defer c.JSON(http.StatusOK, ret)
  136. arg, ok := util.JsonArg(c, ret)
  137. if !ok {
  138. return
  139. }
  140. pathsArg := arg["paths"].([]interface{})
  141. var paths []string
  142. for _, p := range pathsArg {
  143. paths = append(paths, p.(string))
  144. }
  145. model.RemoveIndexes(paths)
  146. }
  147. func refreshFiletree(c *gin.Context) {
  148. ret := gulu.Ret.NewResult()
  149. defer c.JSON(http.StatusOK, ret)
  150. model.FullReindex()
  151. }
  152. func doc2Heading(c *gin.Context) {
  153. ret := gulu.Ret.NewResult()
  154. defer c.JSON(http.StatusOK, ret)
  155. arg, ok := util.JsonArg(c, ret)
  156. if !ok {
  157. return
  158. }
  159. srcID := arg["srcID"].(string)
  160. targetID := arg["targetID"].(string)
  161. after := arg["after"].(bool)
  162. srcTreeBox, srcTreePath, err := model.Doc2Heading(srcID, targetID, after)
  163. if err != nil {
  164. ret.Code = -1
  165. ret.Msg = err.Error()
  166. ret.Data = map[string]interface{}{"closeTimeout": 5000}
  167. return
  168. }
  169. ret.Data = map[string]interface{}{
  170. "srcTreeBox": srcTreeBox,
  171. "srcTreePath": srcTreePath,
  172. }
  173. }
  174. func heading2Doc(c *gin.Context) {
  175. ret := gulu.Ret.NewResult()
  176. defer c.JSON(http.StatusOK, ret)
  177. arg, ok := util.JsonArg(c, ret)
  178. if !ok {
  179. return
  180. }
  181. srcHeadingID := arg["srcHeadingID"].(string)
  182. targetNotebook := arg["targetNoteBook"].(string)
  183. var targetPath string
  184. if arg["targetPath"] != nil {
  185. targetPath = arg["targetPath"].(string)
  186. }
  187. var previousPath string
  188. if arg["previousPath"] != nil {
  189. previousPath = arg["previousPath"].(string)
  190. }
  191. srcRootBlockID, targetPath, err := model.Heading2Doc(srcHeadingID, targetNotebook, targetPath, previousPath)
  192. if err != nil {
  193. ret.Code = -1
  194. ret.Msg = err.Error()
  195. ret.Data = map[string]interface{}{"closeTimeout": 5000}
  196. return
  197. }
  198. model.FlushTxQueue()
  199. luteEngine := util.NewLute()
  200. tree, err := filesys.LoadTree(targetNotebook, targetPath, luteEngine)
  201. if err != nil {
  202. ret.Code = -1
  203. ret.Msg = err.Error()
  204. return
  205. }
  206. name := path.Base(targetPath)
  207. box := model.Conf.Box(targetNotebook)
  208. files, _, _ := model.ListDocTree(targetNotebook, path.Dir(targetPath), util.SortModeUnassigned, false, false, model.Conf.FileTree.MaxListCount)
  209. evt := util.NewCmdResult("heading2doc", 0, util.PushModeBroadcast)
  210. evt.Data = map[string]interface{}{
  211. "box": box,
  212. "path": targetPath,
  213. "files": files,
  214. "name": name,
  215. "id": tree.Root.ID,
  216. "srcRootBlockID": srcRootBlockID,
  217. }
  218. evt.Callback = arg["callback"]
  219. util.PushEvent(evt)
  220. }
  221. func li2Doc(c *gin.Context) {
  222. ret := gulu.Ret.NewResult()
  223. defer c.JSON(http.StatusOK, ret)
  224. arg, ok := util.JsonArg(c, ret)
  225. if !ok {
  226. return
  227. }
  228. srcListItemID := arg["srcListItemID"].(string)
  229. targetNotebook := arg["targetNoteBook"].(string)
  230. var targetPath string
  231. if arg["targetPath"] != nil {
  232. targetPath = arg["targetPath"].(string)
  233. }
  234. var previousPath string
  235. if arg["previousPath"] != nil {
  236. previousPath = arg["previousPath"].(string)
  237. }
  238. srcRootBlockID, targetPath, err := model.ListItem2Doc(srcListItemID, targetNotebook, targetPath, previousPath)
  239. if err != nil {
  240. ret.Code = -1
  241. ret.Msg = err.Error()
  242. ret.Data = map[string]interface{}{"closeTimeout": 5000}
  243. return
  244. }
  245. model.FlushTxQueue()
  246. luteEngine := util.NewLute()
  247. tree, err := filesys.LoadTree(targetNotebook, targetPath, luteEngine)
  248. if err != nil {
  249. ret.Code = -1
  250. ret.Msg = err.Error()
  251. return
  252. }
  253. name := path.Base(targetPath)
  254. box := model.Conf.Box(targetNotebook)
  255. files, _, _ := model.ListDocTree(targetNotebook, path.Dir(targetPath), util.SortModeUnassigned, false, false, model.Conf.FileTree.MaxListCount)
  256. evt := util.NewCmdResult("li2doc", 0, util.PushModeBroadcast)
  257. evt.Data = map[string]interface{}{
  258. "box": box,
  259. "path": targetPath,
  260. "files": files,
  261. "name": name,
  262. "id": tree.Root.ID,
  263. "srcRootBlockID": srcRootBlockID,
  264. }
  265. evt.Callback = arg["callback"]
  266. util.PushEvent(evt)
  267. }
  268. func getHPathByPath(c *gin.Context) {
  269. ret := gulu.Ret.NewResult()
  270. defer c.JSON(http.StatusOK, ret)
  271. arg, ok := util.JsonArg(c, ret)
  272. if !ok {
  273. return
  274. }
  275. notebook := arg["notebook"].(string)
  276. if util.InvalidIDPattern(notebook, ret) {
  277. return
  278. }
  279. p := arg["path"].(string)
  280. hPath, err := model.GetHPathByPath(notebook, p)
  281. if err != nil {
  282. ret.Code = -1
  283. ret.Msg = err.Error()
  284. return
  285. }
  286. ret.Data = hPath
  287. }
  288. func getHPathsByPaths(c *gin.Context) {
  289. ret := gulu.Ret.NewResult()
  290. defer c.JSON(http.StatusOK, ret)
  291. arg, ok := util.JsonArg(c, ret)
  292. if !ok {
  293. return
  294. }
  295. pathsArg := arg["paths"].([]interface{})
  296. var paths []string
  297. for _, p := range pathsArg {
  298. paths = append(paths, p.(string))
  299. }
  300. hPath, err := model.GetHPathsByPaths(paths)
  301. if err != nil {
  302. ret.Code = -1
  303. ret.Msg = err.Error()
  304. return
  305. }
  306. ret.Data = hPath
  307. }
  308. func getHPathByID(c *gin.Context) {
  309. ret := gulu.Ret.NewResult()
  310. defer c.JSON(http.StatusOK, ret)
  311. arg, ok := util.JsonArg(c, ret)
  312. if !ok {
  313. return
  314. }
  315. id := arg["id"].(string)
  316. if util.InvalidIDPattern(id, ret) {
  317. return
  318. }
  319. hPath, err := model.GetHPathByID(id)
  320. if err != nil {
  321. ret.Code = -1
  322. ret.Msg = err.Error()
  323. return
  324. }
  325. ret.Data = hPath
  326. }
  327. func getPathByID(c *gin.Context) {
  328. ret := gulu.Ret.NewResult()
  329. defer c.JSON(http.StatusOK, ret)
  330. arg, ok := util.JsonArg(c, ret)
  331. if !ok {
  332. return
  333. }
  334. id := arg["id"].(string)
  335. if util.InvalidIDPattern(id, ret) {
  336. return
  337. }
  338. _path, err := model.GetPathByID(id)
  339. if err != nil {
  340. ret.Code = -1
  341. ret.Msg = err.Error()
  342. return
  343. }
  344. ret.Data = _path
  345. }
  346. func getFullHPathByID(c *gin.Context) {
  347. ret := gulu.Ret.NewResult()
  348. defer c.JSON(http.StatusOK, ret)
  349. arg, ok := util.JsonArg(c, ret)
  350. if !ok {
  351. return
  352. }
  353. if nil == arg["id"] {
  354. return
  355. }
  356. id := arg["id"].(string)
  357. hPath, err := model.GetFullHPathByID(id)
  358. if err != nil {
  359. ret.Code = -1
  360. ret.Msg = err.Error()
  361. return
  362. }
  363. ret.Data = hPath
  364. }
  365. func getIDsByHPath(c *gin.Context) {
  366. ret := gulu.Ret.NewResult()
  367. defer c.JSON(http.StatusOK, ret)
  368. arg, ok := util.JsonArg(c, ret)
  369. if !ok {
  370. return
  371. }
  372. if nil == arg["path"] {
  373. return
  374. }
  375. if nil == arg["notebook"] {
  376. return
  377. }
  378. notebook := arg["notebook"].(string)
  379. if util.InvalidIDPattern(notebook, ret) {
  380. return
  381. }
  382. p := arg["path"].(string)
  383. ids, err := model.GetIDsByHPath(p, notebook)
  384. if err != nil {
  385. ret.Code = -1
  386. ret.Msg = err.Error()
  387. return
  388. }
  389. ret.Data = ids
  390. }
  391. func moveDocs(c *gin.Context) {
  392. ret := gulu.Ret.NewResult()
  393. defer c.JSON(http.StatusOK, ret)
  394. arg, ok := util.JsonArg(c, ret)
  395. if !ok {
  396. return
  397. }
  398. var fromPaths []string
  399. fromPathsArg := arg["fromPaths"].([]interface{})
  400. for _, fromPath := range fromPathsArg {
  401. fromPaths = append(fromPaths, fromPath.(string))
  402. }
  403. toPath := arg["toPath"].(string)
  404. toNotebook := arg["toNotebook"].(string)
  405. if util.InvalidIDPattern(toNotebook, ret) {
  406. return
  407. }
  408. callback := arg["callback"]
  409. err := model.MoveDocs(fromPaths, toNotebook, toPath, callback)
  410. if err != nil {
  411. ret.Code = -1
  412. ret.Msg = err.Error()
  413. ret.Data = map[string]interface{}{"closeTimeout": 7000}
  414. return
  415. }
  416. }
  417. func removeDoc(c *gin.Context) {
  418. ret := gulu.Ret.NewResult()
  419. defer c.JSON(http.StatusOK, ret)
  420. arg, ok := util.JsonArg(c, ret)
  421. if !ok {
  422. return
  423. }
  424. notebook := arg["notebook"].(string)
  425. if util.InvalidIDPattern(notebook, ret) {
  426. return
  427. }
  428. p := arg["path"].(string)
  429. model.RemoveDoc(notebook, p)
  430. }
  431. func removeDocByID(c *gin.Context) {
  432. ret := gulu.Ret.NewResult()
  433. defer c.JSON(http.StatusOK, ret)
  434. arg, ok := util.JsonArg(c, ret)
  435. if !ok {
  436. return
  437. }
  438. id := arg["id"].(string)
  439. if util.InvalidIDPattern(id, ret) {
  440. return
  441. }
  442. tree, err := model.LoadTreeByBlockID(id)
  443. if err != nil {
  444. ret.Code = -1
  445. ret.Msg = err.Error()
  446. ret.Data = map[string]interface{}{"closeTimeout": 7000}
  447. return
  448. }
  449. model.RemoveDoc(tree.Box, tree.Path)
  450. }
  451. func removeDocs(c *gin.Context) {
  452. ret := gulu.Ret.NewResult()
  453. defer c.JSON(http.StatusOK, ret)
  454. arg, ok := util.JsonArg(c, ret)
  455. if !ok {
  456. return
  457. }
  458. pathsArg := arg["paths"].([]interface{})
  459. var paths []string
  460. for _, path := range pathsArg {
  461. paths = append(paths, path.(string))
  462. }
  463. model.RemoveDocs(paths)
  464. }
  465. func renameDoc(c *gin.Context) {
  466. ret := gulu.Ret.NewResult()
  467. defer c.JSON(http.StatusOK, ret)
  468. arg, ok := util.JsonArg(c, ret)
  469. if !ok {
  470. return
  471. }
  472. notebook := arg["notebook"].(string)
  473. if util.InvalidIDPattern(notebook, ret) {
  474. return
  475. }
  476. p := arg["path"].(string)
  477. title := arg["title"].(string)
  478. err := model.RenameDoc(notebook, p, title)
  479. if err != nil {
  480. ret.Code = -1
  481. ret.Msg = err.Error()
  482. return
  483. }
  484. return
  485. }
  486. func renameDocByID(c *gin.Context) {
  487. ret := gulu.Ret.NewResult()
  488. defer c.JSON(http.StatusOK, ret)
  489. arg, ok := util.JsonArg(c, ret)
  490. if !ok {
  491. return
  492. }
  493. if nil == arg["id"] {
  494. return
  495. }
  496. id := arg["id"].(string)
  497. if util.InvalidIDPattern(id, ret) {
  498. return
  499. }
  500. title := arg["title"].(string)
  501. tree, err := model.LoadTreeByBlockID(id)
  502. if err != nil {
  503. ret.Code = -1
  504. ret.Msg = err.Error()
  505. ret.Data = map[string]interface{}{"closeTimeout": 7000}
  506. return
  507. }
  508. err = model.RenameDoc(tree.Box, tree.Path, title)
  509. if err != nil {
  510. ret.Code = -1
  511. ret.Msg = err.Error()
  512. return
  513. }
  514. }
  515. func duplicateDoc(c *gin.Context) {
  516. ret := gulu.Ret.NewResult()
  517. defer c.JSON(http.StatusOK, ret)
  518. arg, ok := util.JsonArg(c, ret)
  519. if !ok {
  520. return
  521. }
  522. id := arg["id"].(string)
  523. tree, err := model.LoadTreeByBlockID(id)
  524. if err != nil {
  525. ret.Code = -1
  526. ret.Msg = err.Error()
  527. ret.Data = map[string]interface{}{"closeTimeout": 7000}
  528. return
  529. }
  530. notebook := tree.Box
  531. box := model.Conf.Box(notebook)
  532. model.DuplicateDoc(tree)
  533. pushCreate(box, tree.Path, tree.ID, arg)
  534. ret.Data = map[string]interface{}{
  535. "id": tree.Root.ID,
  536. "notebook": notebook,
  537. "path": tree.Path,
  538. "hPath": tree.HPath,
  539. }
  540. }
  541. func createDoc(c *gin.Context) {
  542. ret := gulu.Ret.NewResult()
  543. defer c.JSON(http.StatusOK, ret)
  544. arg, ok := util.JsonArg(c, ret)
  545. if !ok {
  546. return
  547. }
  548. notebook := arg["notebook"].(string)
  549. p := arg["path"].(string)
  550. title := arg["title"].(string)
  551. md := arg["md"].(string)
  552. sortsArg := arg["sorts"]
  553. var sorts []string
  554. if nil != sortsArg {
  555. for _, sort := range sortsArg.([]interface{}) {
  556. sorts = append(sorts, sort.(string))
  557. }
  558. }
  559. tree, err := model.CreateDocByMd(notebook, p, title, md, sorts)
  560. if err != nil {
  561. ret.Code = -1
  562. ret.Msg = err.Error()
  563. ret.Data = map[string]interface{}{"closeTimeout": 7000}
  564. return
  565. }
  566. model.FlushTxQueue()
  567. box := model.Conf.Box(notebook)
  568. pushCreate(box, p, tree.Root.ID, arg)
  569. ret.Data = map[string]interface{}{
  570. "id": tree.Root.ID,
  571. }
  572. }
  573. func createDailyNote(c *gin.Context) {
  574. ret := gulu.Ret.NewResult()
  575. defer c.JSON(http.StatusOK, ret)
  576. arg, ok := util.JsonArg(c, ret)
  577. if !ok {
  578. return
  579. }
  580. notebook := arg["notebook"].(string)
  581. p, existed, err := model.CreateDailyNote(notebook)
  582. if err != nil {
  583. if model.ErrBoxNotFound == err {
  584. ret.Code = 1
  585. } else {
  586. ret.Code = -1
  587. }
  588. ret.Msg = err.Error()
  589. return
  590. }
  591. model.FlushTxQueue()
  592. box := model.Conf.Box(notebook)
  593. luteEngine := util.NewLute()
  594. tree, err := filesys.LoadTree(box.ID, p, luteEngine)
  595. if err != nil {
  596. ret.Code = -1
  597. ret.Msg = err.Error()
  598. return
  599. }
  600. if !existed {
  601. // 只有创建的情况才推送,已经存在的情况不推送
  602. // Creating a dailynote existed no longer expands the doc tree https://github.com/siyuan-note/siyuan/issues/9959
  603. appArg := arg["app"]
  604. app := ""
  605. if nil != appArg {
  606. app = appArg.(string)
  607. }
  608. evt := util.NewCmdResult("createdailynote", 0, util.PushModeBroadcast)
  609. evt.AppId = app
  610. name := path.Base(p)
  611. files, _, _ := model.ListDocTree(box.ID, path.Dir(p), util.SortModeUnassigned, false, false, model.Conf.FileTree.MaxListCount)
  612. evt.Data = map[string]interface{}{
  613. "box": box,
  614. "path": p,
  615. "files": files,
  616. "name": name,
  617. "id": tree.Root.ID,
  618. }
  619. evt.Callback = arg["callback"]
  620. util.PushEvent(evt)
  621. }
  622. ret.Data = map[string]interface{}{
  623. "id": tree.Root.ID,
  624. }
  625. }
  626. func createDocWithMd(c *gin.Context) {
  627. ret := gulu.Ret.NewResult()
  628. defer c.JSON(http.StatusOK, ret)
  629. arg, ok := util.JsonArg(c, ret)
  630. if !ok {
  631. return
  632. }
  633. notebook := arg["notebook"].(string)
  634. if util.InvalidIDPattern(notebook, ret) {
  635. return
  636. }
  637. tagsArg := arg["tags"]
  638. var tags string
  639. if nil != tagsArg {
  640. tags = tagsArg.(string)
  641. }
  642. var parentID string
  643. parentIDArg := arg["parentID"]
  644. if nil != parentIDArg {
  645. parentID = parentIDArg.(string)
  646. }
  647. id := ast.NewNodeID()
  648. idArg := arg["id"]
  649. if nil != idArg {
  650. id = idArg.(string)
  651. }
  652. hPath := arg["path"].(string)
  653. markdown := arg["markdown"].(string)
  654. baseName := path.Base(hPath)
  655. dir := path.Dir(hPath)
  656. r, _ := regexp.Compile("\r\n|\r|\n|\u2028|\u2029|\t|/")
  657. baseName = r.ReplaceAllString(baseName, "")
  658. if 512 < utf8.RuneCountInString(baseName) {
  659. baseName = gulu.Str.SubStr(baseName, 512)
  660. }
  661. hPath = path.Join(dir, baseName)
  662. if !strings.HasPrefix(hPath, "/") {
  663. hPath = "/" + hPath
  664. }
  665. withMath := false
  666. withMathArg := arg["withMath"]
  667. if nil != withMathArg {
  668. withMath = withMathArg.(bool)
  669. }
  670. clippingHref := ""
  671. clippingHrefArg := arg["clippingHref"]
  672. if nil != clippingHrefArg {
  673. clippingHref = clippingHrefArg.(string)
  674. }
  675. id, err := model.CreateWithMarkdown(tags, notebook, hPath, markdown, parentID, id, withMath, clippingHref)
  676. if err != nil {
  677. ret.Code = -1
  678. ret.Msg = err.Error()
  679. return
  680. }
  681. ret.Data = id
  682. model.FlushTxQueue()
  683. box := model.Conf.Box(notebook)
  684. b, _ := model.GetBlock(id, nil)
  685. p := b.Path
  686. pushCreate(box, p, id, arg)
  687. }
  688. func getDocCreateSavePath(c *gin.Context) {
  689. ret := gulu.Ret.NewResult()
  690. defer c.JSON(http.StatusOK, ret)
  691. arg, ok := util.JsonArg(c, ret)
  692. if !ok {
  693. return
  694. }
  695. notebook := arg["notebook"].(string)
  696. box := model.Conf.Box(notebook)
  697. var docCreateSaveBox string
  698. docCreateSavePathTpl := model.Conf.FileTree.DocCreateSavePath
  699. if nil != box {
  700. boxConf := box.GetConf()
  701. docCreateSaveBox = boxConf.DocCreateSaveBox
  702. docCreateSavePathTpl = boxConf.DocCreateSavePath
  703. }
  704. if "" == docCreateSaveBox && "" == docCreateSavePathTpl {
  705. docCreateSaveBox = model.Conf.FileTree.DocCreateSaveBox
  706. }
  707. if "" != docCreateSaveBox {
  708. if nil == model.Conf.Box(docCreateSaveBox) {
  709. // 如果配置的笔记本未打开或者不存在,则使用当前笔记本
  710. docCreateSaveBox = notebook
  711. }
  712. }
  713. if "" == docCreateSaveBox {
  714. docCreateSaveBox = notebook
  715. }
  716. if "" == docCreateSavePathTpl {
  717. docCreateSavePathTpl = model.Conf.FileTree.DocCreateSavePath
  718. }
  719. docCreateSavePathTpl = strings.TrimSpace(docCreateSavePathTpl)
  720. if docCreateSaveBox != notebook {
  721. if "" != docCreateSavePathTpl && !strings.HasPrefix(docCreateSavePathTpl, "/") {
  722. // 如果配置的笔记本不是当前笔记本,则将相对路径转换为绝对路径
  723. docCreateSavePathTpl = "/" + docCreateSavePathTpl
  724. }
  725. }
  726. docCreateSavePath, err := model.RenderGoTemplate(docCreateSavePathTpl)
  727. if err != nil {
  728. ret.Code = -1
  729. ret.Msg = err.Error()
  730. return
  731. }
  732. ret.Data = map[string]interface{}{
  733. "box": docCreateSaveBox,
  734. "path": docCreateSavePath,
  735. }
  736. }
  737. func getRefCreateSavePath(c *gin.Context) {
  738. ret := gulu.Ret.NewResult()
  739. defer c.JSON(http.StatusOK, ret)
  740. arg, ok := util.JsonArg(c, ret)
  741. if !ok {
  742. return
  743. }
  744. notebook := arg["notebook"].(string)
  745. box := model.Conf.Box(notebook)
  746. var refCreateSaveBox string
  747. refCreateSavePathTpl := model.Conf.FileTree.RefCreateSavePath
  748. if nil != box {
  749. boxConf := box.GetConf()
  750. refCreateSaveBox = boxConf.RefCreateSaveBox
  751. refCreateSavePathTpl = boxConf.RefCreateSavePath
  752. }
  753. if "" == refCreateSaveBox && "" == refCreateSavePathTpl {
  754. refCreateSaveBox = model.Conf.FileTree.RefCreateSaveBox
  755. }
  756. if "" != refCreateSaveBox {
  757. if nil == model.Conf.Box(refCreateSaveBox) {
  758. // 如果配置的笔记本未打开或者不存在,则使用当前笔记本
  759. refCreateSaveBox = notebook
  760. }
  761. }
  762. if "" == refCreateSaveBox {
  763. refCreateSaveBox = notebook
  764. }
  765. if "" == refCreateSavePathTpl {
  766. refCreateSavePathTpl = model.Conf.FileTree.RefCreateSavePath
  767. }
  768. if refCreateSaveBox != notebook {
  769. if "" != refCreateSavePathTpl && !strings.HasPrefix(refCreateSavePathTpl, "/") {
  770. // 如果配置的笔记本不是当前笔记本,则将相对路径转换为绝对路径
  771. refCreateSavePathTpl = "/" + refCreateSavePathTpl
  772. }
  773. }
  774. refCreateSavePath, err := model.RenderGoTemplate(refCreateSavePathTpl)
  775. if err != nil {
  776. ret.Code = -1
  777. ret.Msg = err.Error()
  778. return
  779. }
  780. ret.Data = map[string]interface{}{
  781. "box": refCreateSaveBox,
  782. "path": refCreateSavePath,
  783. }
  784. }
  785. func changeSort(c *gin.Context) {
  786. ret := gulu.Ret.NewResult()
  787. defer c.JSON(http.StatusOK, ret)
  788. arg, ok := util.JsonArg(c, ret)
  789. if !ok {
  790. return
  791. }
  792. notebook := arg["notebook"].(string)
  793. pathsArg := arg["paths"].([]interface{})
  794. var paths []string
  795. for _, p := range pathsArg {
  796. paths = append(paths, p.(string))
  797. }
  798. model.ChangeFileTreeSort(notebook, paths)
  799. }
  800. func searchDocs(c *gin.Context) {
  801. ret := gulu.Ret.NewResult()
  802. defer c.JSON(http.StatusOK, ret)
  803. arg, ok := util.JsonArg(c, ret)
  804. if !ok {
  805. return
  806. }
  807. flashcard := false
  808. if arg["flashcard"] != nil {
  809. flashcard = arg["flashcard"].(bool)
  810. }
  811. k := arg["k"].(string)
  812. ret.Data = model.SearchDocsByKeyword(k, flashcard)
  813. }
  814. func listDocsByPath(c *gin.Context) {
  815. ret := gulu.Ret.NewResult()
  816. defer c.JSON(http.StatusOK, ret)
  817. arg, ok := util.JsonArg(c, ret)
  818. if !ok {
  819. return
  820. }
  821. notebook := arg["notebook"].(string)
  822. p := arg["path"].(string)
  823. sortParam := arg["sort"]
  824. sortMode := util.SortModeUnassigned
  825. if nil != sortParam {
  826. sortMode = int(sortParam.(float64))
  827. }
  828. flashcard := false
  829. if arg["flashcard"] != nil {
  830. flashcard = arg["flashcard"].(bool)
  831. }
  832. maxListCount := model.Conf.FileTree.MaxListCount
  833. if arg["maxListCount"] != nil {
  834. // API `listDocsByPath` add an optional parameter `maxListCount` https://github.com/siyuan-note/siyuan/issues/7993
  835. maxListCount = int(arg["maxListCount"].(float64))
  836. if 0 >= maxListCount {
  837. maxListCount = math.MaxInt
  838. }
  839. }
  840. showHidden := false
  841. if arg["showHidden"] != nil {
  842. showHidden = arg["showHidden"].(bool)
  843. }
  844. files, totals, err := model.ListDocTree(notebook, p, sortMode, flashcard, showHidden, maxListCount)
  845. if err != nil {
  846. ret.Code = -1
  847. ret.Msg = err.Error()
  848. return
  849. }
  850. if maxListCount < totals {
  851. // API `listDocsByPath` add an optional parameter `ignoreMaxListHint` https://github.com/siyuan-note/siyuan/issues/10290
  852. ignoreMaxListHintArg := arg["ignoreMaxListHint"]
  853. if nil == ignoreMaxListHintArg || !ignoreMaxListHintArg.(bool) {
  854. util.PushMsg(fmt.Sprintf(model.Conf.Language(48), len(files)), 7000)
  855. }
  856. }
  857. ret.Data = map[string]interface{}{
  858. "box": notebook,
  859. "path": p,
  860. "files": files,
  861. }
  862. }
  863. func getDoc(c *gin.Context) {
  864. ret := gulu.Ret.NewResult()
  865. defer c.JSON(http.StatusOK, ret)
  866. arg, ok := util.JsonArg(c, ret)
  867. if !ok {
  868. return
  869. }
  870. id := arg["id"].(string)
  871. idx := arg["index"]
  872. index := 0
  873. if nil != idx {
  874. index = int(idx.(float64))
  875. }
  876. var query string
  877. if queryArg := arg["query"]; nil != queryArg {
  878. query = queryArg.(string)
  879. }
  880. var queryMethod int
  881. if queryMethodArg := arg["queryMethod"]; nil != queryMethodArg {
  882. queryMethod = int(queryMethodArg.(float64))
  883. }
  884. var queryTypes map[string]bool
  885. if queryTypesArg := arg["queryTypes"]; nil != queryTypesArg {
  886. typesArg := queryTypesArg.(map[string]interface{})
  887. queryTypes = map[string]bool{}
  888. for t, b := range typesArg {
  889. queryTypes[t] = b.(bool)
  890. }
  891. }
  892. m := arg["mode"] // 0: 仅当前 ID,1:向上 2:向下,3:上下都加载,4:加载末尾
  893. mode := 0
  894. if nil != m {
  895. mode = int(m.(float64))
  896. }
  897. s := arg["size"]
  898. size := 102400 // 默认最大加载块数
  899. if nil != s {
  900. size = int(s.(float64))
  901. }
  902. startID := ""
  903. endID := ""
  904. startIDArg := arg["startID"]
  905. endIDArg := arg["endID"]
  906. if nil != startIDArg && nil != endIDArg {
  907. startID = startIDArg.(string)
  908. endID = endIDArg.(string)
  909. size = model.Conf.Editor.DynamicLoadBlocks
  910. }
  911. isBacklinkArg := arg["isBacklink"]
  912. isBacklink := false
  913. if nil != isBacklinkArg {
  914. isBacklink = isBacklinkArg.(bool)
  915. }
  916. blockCount, content, parentID, parent2ID, rootID, typ, eof, scroll, boxID, docPath, isBacklinkExpand, err := model.GetDoc(startID, endID, id, index, query, queryTypes, queryMethod, mode, size, isBacklink)
  917. if model.ErrBlockNotFound == err {
  918. ret.Code = 3
  919. return
  920. }
  921. if err != nil {
  922. ret.Code = 1
  923. ret.Msg = err.Error()
  924. return
  925. }
  926. // 判断是否正在同步中 https://github.com/siyuan-note/siyuan/issues/6290
  927. isSyncing := model.IsSyncingFile(rootID)
  928. ret.Data = map[string]interface{}{
  929. "id": id,
  930. "mode": mode,
  931. "parentID": parentID,
  932. "parent2ID": parent2ID,
  933. "rootID": rootID,
  934. "type": typ,
  935. "content": content,
  936. "blockCount": blockCount,
  937. "eof": eof,
  938. "scroll": scroll,
  939. "box": boxID,
  940. "path": docPath,
  941. "isSyncing": isSyncing,
  942. "isBacklinkExpand": isBacklinkExpand,
  943. }
  944. }
  945. func pushCreate(box *model.Box, p, treeID string, arg map[string]interface{}) {
  946. evt := util.NewCmdResult("create", 0, util.PushModeBroadcast)
  947. name := path.Base(p)
  948. files, _, _ := model.ListDocTree(box.ID, path.Dir(p), util.SortModeUnassigned, false, false, model.Conf.FileTree.MaxListCount)
  949. evt.Data = map[string]interface{}{
  950. "box": box,
  951. "path": p,
  952. "files": files,
  953. "name": name,
  954. "id": treeID,
  955. }
  956. evt.Callback = arg["callback"]
  957. util.PushEvent(evt)
  958. }