filetree.go 24 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082
  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 removeDocs(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. pathsArg := arg["paths"].([]interface{})
  439. var paths []string
  440. for _, path := range pathsArg {
  441. paths = append(paths, path.(string))
  442. }
  443. model.RemoveDocs(paths)
  444. }
  445. func renameDoc(c *gin.Context) {
  446. ret := gulu.Ret.NewResult()
  447. defer c.JSON(http.StatusOK, ret)
  448. arg, ok := util.JsonArg(c, ret)
  449. if !ok {
  450. return
  451. }
  452. notebook := arg["notebook"].(string)
  453. if util.InvalidIDPattern(notebook, ret) {
  454. return
  455. }
  456. p := arg["path"].(string)
  457. title := arg["title"].(string)
  458. err := model.RenameDoc(notebook, p, title)
  459. if err != nil {
  460. ret.Code = -1
  461. ret.Msg = err.Error()
  462. return
  463. }
  464. return
  465. }
  466. func renameDocByID(c *gin.Context) {
  467. ret := gulu.Ret.NewResult()
  468. defer c.JSON(http.StatusOK, ret)
  469. arg, ok := util.JsonArg(c, ret)
  470. if !ok {
  471. return
  472. }
  473. if nil == arg["id"] {
  474. return
  475. }
  476. id := arg["id"].(string)
  477. title := arg["title"].(string)
  478. tree, err := model.LoadTreeByBlockID(id)
  479. if err != nil {
  480. ret.Code = -1
  481. ret.Msg = err.Error()
  482. ret.Data = map[string]interface{}{"closeTimeout": 7000}
  483. return
  484. }
  485. err = model.RenameDoc(tree.Box, tree.Path, title)
  486. if err != nil {
  487. ret.Code = -1
  488. ret.Msg = err.Error()
  489. return
  490. }
  491. }
  492. func duplicateDoc(c *gin.Context) {
  493. ret := gulu.Ret.NewResult()
  494. defer c.JSON(http.StatusOK, ret)
  495. arg, ok := util.JsonArg(c, ret)
  496. if !ok {
  497. return
  498. }
  499. id := arg["id"].(string)
  500. tree, err := model.LoadTreeByBlockID(id)
  501. if err != nil {
  502. ret.Code = -1
  503. ret.Msg = err.Error()
  504. ret.Data = map[string]interface{}{"closeTimeout": 7000}
  505. return
  506. }
  507. notebook := tree.Box
  508. box := model.Conf.Box(notebook)
  509. model.DuplicateDoc(tree)
  510. pushCreate(box, tree.Path, tree.ID, arg)
  511. ret.Data = map[string]interface{}{
  512. "id": tree.Root.ID,
  513. "notebook": notebook,
  514. "path": tree.Path,
  515. "hPath": tree.HPath,
  516. }
  517. }
  518. func createDoc(c *gin.Context) {
  519. ret := gulu.Ret.NewResult()
  520. defer c.JSON(http.StatusOK, ret)
  521. arg, ok := util.JsonArg(c, ret)
  522. if !ok {
  523. return
  524. }
  525. notebook := arg["notebook"].(string)
  526. p := arg["path"].(string)
  527. title := arg["title"].(string)
  528. md := arg["md"].(string)
  529. sortsArg := arg["sorts"]
  530. var sorts []string
  531. if nil != sortsArg {
  532. for _, sort := range sortsArg.([]interface{}) {
  533. sorts = append(sorts, sort.(string))
  534. }
  535. }
  536. tree, err := model.CreateDocByMd(notebook, p, title, md, sorts)
  537. if err != nil {
  538. ret.Code = -1
  539. ret.Msg = err.Error()
  540. ret.Data = map[string]interface{}{"closeTimeout": 7000}
  541. return
  542. }
  543. model.FlushTxQueue()
  544. box := model.Conf.Box(notebook)
  545. pushCreate(box, p, tree.Root.ID, arg)
  546. ret.Data = map[string]interface{}{
  547. "id": tree.Root.ID,
  548. }
  549. }
  550. func createDailyNote(c *gin.Context) {
  551. ret := gulu.Ret.NewResult()
  552. defer c.JSON(http.StatusOK, ret)
  553. arg, ok := util.JsonArg(c, ret)
  554. if !ok {
  555. return
  556. }
  557. notebook := arg["notebook"].(string)
  558. p, existed, err := model.CreateDailyNote(notebook)
  559. if err != nil {
  560. if model.ErrBoxNotFound == err {
  561. ret.Code = 1
  562. } else {
  563. ret.Code = -1
  564. }
  565. ret.Msg = err.Error()
  566. return
  567. }
  568. model.FlushTxQueue()
  569. box := model.Conf.Box(notebook)
  570. luteEngine := util.NewLute()
  571. tree, err := filesys.LoadTree(box.ID, p, luteEngine)
  572. if err != nil {
  573. ret.Code = -1
  574. ret.Msg = err.Error()
  575. return
  576. }
  577. if !existed {
  578. // 只有创建的情况才推送,已经存在的情况不推送
  579. // Creating a dailynote existed no longer expands the doc tree https://github.com/siyuan-note/siyuan/issues/9959
  580. appArg := arg["app"]
  581. app := ""
  582. if nil != appArg {
  583. app = appArg.(string)
  584. }
  585. evt := util.NewCmdResult("createdailynote", 0, util.PushModeBroadcast)
  586. evt.AppId = app
  587. name := path.Base(p)
  588. files, _, _ := model.ListDocTree(box.ID, path.Dir(p), util.SortModeUnassigned, false, false, model.Conf.FileTree.MaxListCount)
  589. evt.Data = map[string]interface{}{
  590. "box": box,
  591. "path": p,
  592. "files": files,
  593. "name": name,
  594. "id": tree.Root.ID,
  595. }
  596. evt.Callback = arg["callback"]
  597. util.PushEvent(evt)
  598. }
  599. ret.Data = map[string]interface{}{
  600. "id": tree.Root.ID,
  601. }
  602. }
  603. func createDocWithMd(c *gin.Context) {
  604. ret := gulu.Ret.NewResult()
  605. defer c.JSON(http.StatusOK, ret)
  606. arg, ok := util.JsonArg(c, ret)
  607. if !ok {
  608. return
  609. }
  610. notebook := arg["notebook"].(string)
  611. if util.InvalidIDPattern(notebook, ret) {
  612. return
  613. }
  614. tagsArg := arg["tags"]
  615. var tags string
  616. if nil != tagsArg {
  617. tags = tagsArg.(string)
  618. }
  619. var parentID string
  620. parentIDArg := arg["parentID"]
  621. if nil != parentIDArg {
  622. parentID = parentIDArg.(string)
  623. }
  624. id := ast.NewNodeID()
  625. idArg := arg["id"]
  626. if nil != idArg {
  627. id = idArg.(string)
  628. }
  629. hPath := arg["path"].(string)
  630. markdown := arg["markdown"].(string)
  631. baseName := path.Base(hPath)
  632. dir := path.Dir(hPath)
  633. r, _ := regexp.Compile("\r\n|\r|\n|\u2028|\u2029|\t|/")
  634. baseName = r.ReplaceAllString(baseName, "")
  635. if 512 < utf8.RuneCountInString(baseName) {
  636. baseName = gulu.Str.SubStr(baseName, 512)
  637. }
  638. hPath = path.Join(dir, baseName)
  639. if !strings.HasPrefix(hPath, "/") {
  640. hPath = "/" + hPath
  641. }
  642. withMath := false
  643. withMathArg := arg["withMath"]
  644. if nil != withMathArg {
  645. withMath = withMathArg.(bool)
  646. }
  647. clippingHref := ""
  648. clippingHrefArg := arg["clippingHref"]
  649. if nil != clippingHrefArg {
  650. clippingHref = clippingHrefArg.(string)
  651. }
  652. id, err := model.CreateWithMarkdown(tags, notebook, hPath, markdown, parentID, id, withMath, clippingHref)
  653. if err != nil {
  654. ret.Code = -1
  655. ret.Msg = err.Error()
  656. return
  657. }
  658. ret.Data = id
  659. model.FlushTxQueue()
  660. box := model.Conf.Box(notebook)
  661. b, _ := model.GetBlock(id, nil)
  662. p := b.Path
  663. pushCreate(box, p, id, arg)
  664. }
  665. func getDocCreateSavePath(c *gin.Context) {
  666. ret := gulu.Ret.NewResult()
  667. defer c.JSON(http.StatusOK, ret)
  668. arg, ok := util.JsonArg(c, ret)
  669. if !ok {
  670. return
  671. }
  672. notebook := arg["notebook"].(string)
  673. box := model.Conf.Box(notebook)
  674. var docCreateSaveBox string
  675. docCreateSavePathTpl := model.Conf.FileTree.DocCreateSavePath
  676. if nil != box {
  677. boxConf := box.GetConf()
  678. docCreateSaveBox = boxConf.DocCreateSaveBox
  679. docCreateSavePathTpl = boxConf.DocCreateSavePath
  680. }
  681. if "" == docCreateSaveBox && "" == docCreateSavePathTpl {
  682. docCreateSaveBox = model.Conf.FileTree.DocCreateSaveBox
  683. }
  684. if "" != docCreateSaveBox {
  685. if nil == model.Conf.Box(docCreateSaveBox) {
  686. // 如果配置的笔记本未打开或者不存在,则使用当前笔记本
  687. docCreateSaveBox = notebook
  688. }
  689. }
  690. if "" == docCreateSaveBox {
  691. docCreateSaveBox = notebook
  692. }
  693. if "" == docCreateSavePathTpl {
  694. docCreateSavePathTpl = model.Conf.FileTree.DocCreateSavePath
  695. }
  696. docCreateSavePathTpl = strings.TrimSpace(docCreateSavePathTpl)
  697. if docCreateSaveBox != notebook {
  698. if "" != docCreateSavePathTpl && !strings.HasPrefix(docCreateSavePathTpl, "/") {
  699. // 如果配置的笔记本不是当前笔记本,则将相对路径转换为绝对路径
  700. docCreateSavePathTpl = "/" + docCreateSavePathTpl
  701. }
  702. }
  703. docCreateSavePath, err := model.RenderGoTemplate(docCreateSavePathTpl)
  704. if err != nil {
  705. ret.Code = -1
  706. ret.Msg = err.Error()
  707. return
  708. }
  709. ret.Data = map[string]interface{}{
  710. "box": docCreateSaveBox,
  711. "path": docCreateSavePath,
  712. }
  713. }
  714. func getRefCreateSavePath(c *gin.Context) {
  715. ret := gulu.Ret.NewResult()
  716. defer c.JSON(http.StatusOK, ret)
  717. arg, ok := util.JsonArg(c, ret)
  718. if !ok {
  719. return
  720. }
  721. notebook := arg["notebook"].(string)
  722. box := model.Conf.Box(notebook)
  723. var refCreateSaveBox string
  724. refCreateSavePathTpl := model.Conf.FileTree.RefCreateSavePath
  725. if nil != box {
  726. boxConf := box.GetConf()
  727. refCreateSaveBox = boxConf.RefCreateSaveBox
  728. refCreateSavePathTpl = boxConf.RefCreateSavePath
  729. }
  730. if "" == refCreateSaveBox && "" == refCreateSavePathTpl {
  731. refCreateSaveBox = model.Conf.FileTree.RefCreateSaveBox
  732. }
  733. if "" != refCreateSaveBox {
  734. if nil == model.Conf.Box(refCreateSaveBox) {
  735. // 如果配置的笔记本未打开或者不存在,则使用当前笔记本
  736. refCreateSaveBox = notebook
  737. }
  738. }
  739. if "" == refCreateSaveBox {
  740. refCreateSaveBox = notebook
  741. }
  742. if "" == refCreateSavePathTpl {
  743. refCreateSavePathTpl = model.Conf.FileTree.RefCreateSavePath
  744. }
  745. if refCreateSaveBox != notebook {
  746. if "" != refCreateSavePathTpl && !strings.HasPrefix(refCreateSavePathTpl, "/") {
  747. // 如果配置的笔记本不是当前笔记本,则将相对路径转换为绝对路径
  748. refCreateSavePathTpl = "/" + refCreateSavePathTpl
  749. }
  750. }
  751. refCreateSavePath, err := model.RenderGoTemplate(refCreateSavePathTpl)
  752. if err != nil {
  753. ret.Code = -1
  754. ret.Msg = err.Error()
  755. return
  756. }
  757. ret.Data = map[string]interface{}{
  758. "box": refCreateSaveBox,
  759. "path": refCreateSavePath,
  760. }
  761. }
  762. func changeSort(c *gin.Context) {
  763. ret := gulu.Ret.NewResult()
  764. defer c.JSON(http.StatusOK, ret)
  765. arg, ok := util.JsonArg(c, ret)
  766. if !ok {
  767. return
  768. }
  769. notebook := arg["notebook"].(string)
  770. pathsArg := arg["paths"].([]interface{})
  771. var paths []string
  772. for _, p := range pathsArg {
  773. paths = append(paths, p.(string))
  774. }
  775. model.ChangeFileTreeSort(notebook, paths)
  776. }
  777. func searchDocs(c *gin.Context) {
  778. ret := gulu.Ret.NewResult()
  779. defer c.JSON(http.StatusOK, ret)
  780. arg, ok := util.JsonArg(c, ret)
  781. if !ok {
  782. return
  783. }
  784. flashcard := false
  785. if arg["flashcard"] != nil {
  786. flashcard = arg["flashcard"].(bool)
  787. }
  788. k := arg["k"].(string)
  789. ret.Data = model.SearchDocsByKeyword(k, flashcard)
  790. }
  791. func listDocsByPath(c *gin.Context) {
  792. ret := gulu.Ret.NewResult()
  793. defer c.JSON(http.StatusOK, ret)
  794. arg, ok := util.JsonArg(c, ret)
  795. if !ok {
  796. return
  797. }
  798. notebook := arg["notebook"].(string)
  799. p := arg["path"].(string)
  800. sortParam := arg["sort"]
  801. sortMode := util.SortModeUnassigned
  802. if nil != sortParam {
  803. sortMode = int(sortParam.(float64))
  804. }
  805. flashcard := false
  806. if arg["flashcard"] != nil {
  807. flashcard = arg["flashcard"].(bool)
  808. }
  809. maxListCount := model.Conf.FileTree.MaxListCount
  810. if arg["maxListCount"] != nil {
  811. // API `listDocsByPath` add an optional parameter `maxListCount` https://github.com/siyuan-note/siyuan/issues/7993
  812. maxListCount = int(arg["maxListCount"].(float64))
  813. if 0 >= maxListCount {
  814. maxListCount = math.MaxInt
  815. }
  816. }
  817. showHidden := false
  818. if arg["showHidden"] != nil {
  819. showHidden = arg["showHidden"].(bool)
  820. }
  821. files, totals, err := model.ListDocTree(notebook, p, sortMode, flashcard, showHidden, maxListCount)
  822. if err != nil {
  823. ret.Code = -1
  824. ret.Msg = err.Error()
  825. return
  826. }
  827. if maxListCount < totals {
  828. // API `listDocsByPath` add an optional parameter `ignoreMaxListHint` https://github.com/siyuan-note/siyuan/issues/10290
  829. ignoreMaxListHintArg := arg["ignoreMaxListHint"]
  830. if nil == ignoreMaxListHintArg || !ignoreMaxListHintArg.(bool) {
  831. util.PushMsg(fmt.Sprintf(model.Conf.Language(48), len(files)), 7000)
  832. }
  833. }
  834. ret.Data = map[string]interface{}{
  835. "box": notebook,
  836. "path": p,
  837. "files": files,
  838. }
  839. }
  840. func getDoc(c *gin.Context) {
  841. ret := gulu.Ret.NewResult()
  842. defer c.JSON(http.StatusOK, ret)
  843. arg, ok := util.JsonArg(c, ret)
  844. if !ok {
  845. return
  846. }
  847. id := arg["id"].(string)
  848. idx := arg["index"]
  849. index := 0
  850. if nil != idx {
  851. index = int(idx.(float64))
  852. }
  853. var query string
  854. if queryArg := arg["query"]; nil != queryArg {
  855. query = queryArg.(string)
  856. }
  857. var queryMethod int
  858. if queryMethodArg := arg["queryMethod"]; nil != queryMethodArg {
  859. queryMethod = int(queryMethodArg.(float64))
  860. }
  861. var queryTypes map[string]bool
  862. if queryTypesArg := arg["queryTypes"]; nil != queryTypesArg {
  863. typesArg := queryTypesArg.(map[string]interface{})
  864. queryTypes = map[string]bool{}
  865. for t, b := range typesArg {
  866. queryTypes[t] = b.(bool)
  867. }
  868. }
  869. m := arg["mode"] // 0: 仅当前 ID,1:向上 2:向下,3:上下都加载,4:加载末尾
  870. mode := 0
  871. if nil != m {
  872. mode = int(m.(float64))
  873. }
  874. s := arg["size"]
  875. size := 102400 // 默认最大加载块数
  876. if nil != s {
  877. size = int(s.(float64))
  878. }
  879. startID := ""
  880. endID := ""
  881. startIDArg := arg["startID"]
  882. endIDArg := arg["endID"]
  883. if nil != startIDArg && nil != endIDArg {
  884. startID = startIDArg.(string)
  885. endID = endIDArg.(string)
  886. size = model.Conf.Editor.DynamicLoadBlocks
  887. }
  888. isBacklinkArg := arg["isBacklink"]
  889. isBacklink := false
  890. if nil != isBacklinkArg {
  891. isBacklink = isBacklinkArg.(bool)
  892. }
  893. blockCount, content, parentID, parent2ID, rootID, typ, eof, scroll, boxID, docPath, isBacklinkExpand, err := model.GetDoc(startID, endID, id, index, query, queryTypes, queryMethod, mode, size, isBacklink)
  894. if model.ErrBlockNotFound == err {
  895. ret.Code = 3
  896. return
  897. }
  898. if err != nil {
  899. ret.Code = 1
  900. ret.Msg = err.Error()
  901. return
  902. }
  903. // 判断是否正在同步中 https://github.com/siyuan-note/siyuan/issues/6290
  904. isSyncing := model.IsSyncingFile(rootID)
  905. ret.Data = map[string]interface{}{
  906. "id": id,
  907. "mode": mode,
  908. "parentID": parentID,
  909. "parent2ID": parent2ID,
  910. "rootID": rootID,
  911. "type": typ,
  912. "content": content,
  913. "blockCount": blockCount,
  914. "eof": eof,
  915. "scroll": scroll,
  916. "box": boxID,
  917. "path": docPath,
  918. "isSyncing": isSyncing,
  919. "isBacklinkExpand": isBacklinkExpand,
  920. }
  921. }
  922. func pushCreate(box *model.Box, p, treeID string, arg map[string]interface{}) {
  923. evt := util.NewCmdResult("create", 0, util.PushModeBroadcast)
  924. name := path.Base(p)
  925. files, _, _ := model.ListDocTree(box.ID, path.Dir(p), util.SortModeUnassigned, false, false, model.Conf.FileTree.MaxListCount)
  926. evt.Data = map[string]interface{}{
  927. "box": box,
  928. "path": p,
  929. "files": files,
  930. "name": name,
  931. "id": treeID,
  932. }
  933. evt.Callback = arg["callback"]
  934. util.PushEvent(evt)
  935. }