node.go 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102
  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 treenode
  17. import (
  18. "bytes"
  19. "github.com/siyuan-note/siyuan/kernel/av"
  20. "github.com/siyuan-note/siyuan/kernel/cache"
  21. "strings"
  22. "sync"
  23. "text/template"
  24. "time"
  25. "github.com/88250/gulu"
  26. "github.com/88250/lute"
  27. "github.com/88250/lute/ast"
  28. "github.com/88250/lute/editor"
  29. "github.com/88250/lute/html"
  30. "github.com/88250/lute/lex"
  31. "github.com/88250/lute/parse"
  32. "github.com/88250/lute/render"
  33. "github.com/88250/vitess-sqlparser/sqlparser"
  34. "github.com/siyuan-note/logging"
  35. "github.com/siyuan-note/siyuan/kernel/util"
  36. )
  37. func GetEmbedBlockRef(embedNode *ast.Node) (blockRefID string) {
  38. if nil == embedNode || ast.NodeBlockQueryEmbed != embedNode.Type {
  39. return
  40. }
  41. scriptNode := embedNode.ChildByType(ast.NodeBlockQueryEmbedScript)
  42. if nil == scriptNode {
  43. return
  44. }
  45. stmt := scriptNode.TokensStr()
  46. parsedStmt, err := sqlparser.Parse(stmt)
  47. if nil != err {
  48. return
  49. }
  50. switch parsedStmt.(type) {
  51. case *sqlparser.Select:
  52. slct := parsedStmt.(*sqlparser.Select)
  53. if nil == slct.Where || nil == slct.Where.Expr {
  54. return
  55. }
  56. switch slct.Where.Expr.(type) {
  57. case *sqlparser.ComparisonExpr: // WHERE id = '20060102150405-1a2b3c4'
  58. comp := slct.Where.Expr.(*sqlparser.ComparisonExpr)
  59. switch comp.Left.(type) {
  60. case *sqlparser.ColName:
  61. col := comp.Left.(*sqlparser.ColName)
  62. if nil == col || "id" != col.Name.Lowered() {
  63. return
  64. }
  65. }
  66. switch comp.Right.(type) {
  67. case *sqlparser.SQLVal:
  68. val := comp.Right.(*sqlparser.SQLVal)
  69. if nil == val || sqlparser.StrVal != val.Type {
  70. return
  71. }
  72. idVal := string(val.Val)
  73. if !ast.IsNodeIDPattern(idVal) {
  74. return
  75. }
  76. blockRefID = idVal
  77. }
  78. }
  79. }
  80. return
  81. }
  82. func GetBlockRef(n *ast.Node) (blockRefID, blockRefText, blockRefSubtype string) {
  83. if !IsBlockRef(n) {
  84. return
  85. }
  86. blockRefID = n.TextMarkBlockRefID
  87. blockRefText = n.TextMarkTextContent
  88. blockRefSubtype = n.TextMarkBlockRefSubtype
  89. return
  90. }
  91. func IsBlockRef(n *ast.Node) bool {
  92. if nil == n {
  93. return false
  94. }
  95. return ast.NodeTextMark == n.Type && n.IsTextMarkType("block-ref")
  96. }
  97. func IsFileAnnotationRef(n *ast.Node) bool {
  98. if nil == n {
  99. return false
  100. }
  101. return ast.NodeTextMark == n.Type && n.IsTextMarkType("file-annotation-ref")
  102. }
  103. func IsEmbedBlockRef(n *ast.Node) bool {
  104. return "" != GetEmbedBlockRef(n)
  105. }
  106. func FormatNode(node *ast.Node, luteEngine *lute.Lute) string {
  107. markdown, err := lute.FormatNodeSync(node, luteEngine.ParseOptions, luteEngine.RenderOptions)
  108. if nil != err {
  109. root := TreeRoot(node)
  110. logging.LogFatalf(logging.ExitCodeFatal, "format node [%s] in tree [%s] failed: %s", node.ID, root.ID, err)
  111. }
  112. return markdown
  113. }
  114. func ExportNodeStdMd(node *ast.Node, luteEngine *lute.Lute) string {
  115. markdown, err := lute.ProtyleExportMdNodeSync(node, luteEngine.ParseOptions, luteEngine.RenderOptions)
  116. if nil != err {
  117. root := TreeRoot(node)
  118. logging.LogFatalf(logging.ExitCodeFatal, "export markdown for node [%s] in tree [%s] failed: %s", node.ID, root.ID, err)
  119. }
  120. return markdown
  121. }
  122. func IsNodeOCRed(node *ast.Node) (ret bool) {
  123. if !util.TesseractEnabled || nil == node {
  124. return true
  125. }
  126. ret = true
  127. ast.Walk(node, func(n *ast.Node, entering bool) ast.WalkStatus {
  128. if !entering {
  129. return ast.WalkContinue
  130. }
  131. if ast.NodeImage == n.Type {
  132. linkDest := n.ChildByType(ast.NodeLinkDest)
  133. if nil == linkDest {
  134. return ast.WalkContinue
  135. }
  136. linkDestStr := linkDest.TokensStr()
  137. if !cache.ExistAsset(linkDestStr) {
  138. return ast.WalkContinue
  139. }
  140. if !util.ExistsAssetText(linkDestStr) {
  141. ret = false
  142. return ast.WalkStop
  143. }
  144. }
  145. return ast.WalkContinue
  146. })
  147. return
  148. }
  149. func NodeStaticContent(node *ast.Node, excludeTypes []string, includeTextMarkATitleURL, includeAssetPath, fullAttrView bool) string {
  150. if nil == node {
  151. return ""
  152. }
  153. if ast.NodeDocument == node.Type {
  154. return node.IALAttr("title")
  155. }
  156. if ast.NodeAttributeView == node.Type {
  157. if fullAttrView {
  158. return getAttributeViewContent(node.AttributeViewID)
  159. }
  160. return getAttributeViewName(node.AttributeViewID)
  161. }
  162. buf := bytes.Buffer{}
  163. buf.Grow(4096)
  164. lastSpace := false
  165. ast.Walk(node, func(n *ast.Node, entering bool) ast.WalkStatus {
  166. if !entering {
  167. return ast.WalkContinue
  168. }
  169. if n.IsContainerBlock() {
  170. if !lastSpace {
  171. buf.WriteByte(' ')
  172. lastSpace = true
  173. }
  174. return ast.WalkContinue
  175. }
  176. if gulu.Str.Contains(n.Type.String(), excludeTypes) {
  177. return ast.WalkContinue
  178. }
  179. switch n.Type {
  180. case ast.NodeTableCell:
  181. // 表格块写入数据库表时在单元格之间添加空格 https://github.com/siyuan-note/siyuan/issues/7654
  182. if 0 < buf.Len() && ' ' != buf.Bytes()[buf.Len()-1] {
  183. buf.WriteByte(' ')
  184. }
  185. case ast.NodeImage:
  186. linkDest := n.ChildByType(ast.NodeLinkDest)
  187. var linkDestStr, ocrText string
  188. if nil != linkDest {
  189. linkDestStr = linkDest.TokensStr()
  190. ocrText = util.GetAssetText(linkDestStr, false)
  191. }
  192. linkText := n.ChildByType(ast.NodeLinkText)
  193. if nil != linkText {
  194. buf.Write(linkText.Tokens)
  195. buf.WriteByte(' ')
  196. }
  197. if "" != ocrText {
  198. buf.WriteString(ocrText)
  199. buf.WriteByte(' ')
  200. }
  201. if nil != linkDest {
  202. if !bytes.HasPrefix(linkDest.Tokens, []byte("assets/")) || includeAssetPath {
  203. buf.Write(linkDest.Tokens)
  204. buf.WriteByte(' ')
  205. }
  206. }
  207. if linkTitle := n.ChildByType(ast.NodeLinkTitle); nil != linkTitle {
  208. buf.Write(linkTitle.Tokens)
  209. }
  210. return ast.WalkSkipChildren
  211. case ast.NodeLinkText:
  212. buf.Write(n.Tokens)
  213. buf.WriteByte(' ')
  214. case ast.NodeLinkDest:
  215. buf.Write(n.Tokens)
  216. buf.WriteByte(' ')
  217. case ast.NodeLinkTitle:
  218. buf.Write(n.Tokens)
  219. case ast.NodeText, ast.NodeCodeBlockCode, ast.NodeMathBlockContent, ast.NodeHTMLBlock:
  220. tokens := n.Tokens
  221. if IsChartCodeBlockCode(n) {
  222. // 图表块的内容在数据库 `blocks` 表 `content` 字段中被转义 https://github.com/siyuan-note/siyuan/issues/6326
  223. tokens = html.UnescapeHTML(tokens)
  224. }
  225. buf.Write(tokens)
  226. case ast.NodeTextMark:
  227. for _, excludeType := range excludeTypes {
  228. if strings.HasPrefix(excludeType, "NodeTextMark-") {
  229. if n.IsTextMarkType(excludeType[len("NodeTextMark-"):]) {
  230. return ast.WalkContinue
  231. }
  232. }
  233. }
  234. if n.IsTextMarkType("tag") {
  235. buf.WriteByte('#')
  236. }
  237. buf.WriteString(n.Content())
  238. if n.IsTextMarkType("tag") {
  239. buf.WriteByte('#')
  240. }
  241. if n.IsTextMarkType("a") && includeTextMarkATitleURL {
  242. // 搜索不到超链接元素的 URL 和标题 https://github.com/siyuan-note/siyuan/issues/7352
  243. if "" != n.TextMarkATitle {
  244. buf.WriteString(" " + html.UnescapeHTMLStr(n.TextMarkATitle))
  245. }
  246. if !strings.HasPrefix(n.TextMarkAHref, "assets/") || includeAssetPath {
  247. buf.WriteString(" " + html.UnescapeHTMLStr(n.TextMarkAHref))
  248. }
  249. }
  250. case ast.NodeBackslash:
  251. buf.WriteByte(lex.ItemBackslash)
  252. case ast.NodeBackslashContent:
  253. buf.Write(n.Tokens)
  254. case ast.NodeAudio, ast.NodeVideo:
  255. buf.WriteString(GetNodeSrcTokens(n))
  256. buf.WriteByte(' ')
  257. }
  258. lastSpace = false
  259. return ast.WalkContinue
  260. })
  261. // 这里不要 trim,否则无法搜索首尾空格
  262. // Improve search and replace for spaces https://github.com/siyuan-note/siyuan/issues/10231
  263. return buf.String()
  264. }
  265. func GetNodeSrcTokens(n *ast.Node) (ret string) {
  266. if index := bytes.Index(n.Tokens, []byte("src=\"")); 0 < index {
  267. src := n.Tokens[index+len("src=\""):]
  268. if index = bytes.Index(src, []byte("\"")); 0 < index {
  269. src = src[:bytes.Index(src, []byte("\""))]
  270. if !IsRelativePath(src) {
  271. return
  272. }
  273. ret = strings.TrimSpace(string(src))
  274. return
  275. }
  276. logging.LogWarnf("src is missing the closing double quote in tree [%s] ", n.Box+n.Path)
  277. }
  278. return
  279. }
  280. func IsRelativePath(dest []byte) bool {
  281. if 1 > len(dest) {
  282. return false
  283. }
  284. if '/' == dest[0] {
  285. return false
  286. }
  287. return !bytes.Contains(dest, []byte(":"))
  288. }
  289. func FirstLeafBlock(node *ast.Node) (ret *ast.Node) {
  290. ast.Walk(node, func(n *ast.Node, entering bool) ast.WalkStatus {
  291. if !entering || n.IsMarker() {
  292. return ast.WalkContinue
  293. }
  294. if !n.IsContainerBlock() {
  295. ret = n
  296. return ast.WalkStop
  297. }
  298. return ast.WalkContinue
  299. })
  300. return
  301. }
  302. func CountBlockNodes(node *ast.Node) (ret int) {
  303. ast.Walk(node, func(n *ast.Node, entering bool) ast.WalkStatus {
  304. if !entering || !n.IsBlock() || ast.NodeList == n.Type || ast.NodeBlockquote == n.Type || ast.NodeSuperBlock == n.Type {
  305. return ast.WalkContinue
  306. }
  307. if "1" == n.IALAttr("fold") {
  308. ret++
  309. return ast.WalkSkipChildren
  310. }
  311. ret++
  312. return ast.WalkContinue
  313. })
  314. return
  315. }
  316. func ParentNodes(node *ast.Node) (parents []*ast.Node) {
  317. const maxDepth = 256
  318. i := 0
  319. for n := node.Parent; nil != n; n = n.Parent {
  320. i++
  321. parents = append(parents, n)
  322. if ast.NodeDocument == n.Type {
  323. return
  324. }
  325. if maxDepth < i {
  326. logging.LogWarnf("parent nodes of node [%s] is too deep", node.ID)
  327. return
  328. }
  329. }
  330. return
  331. }
  332. func ChildBlockNodes(node *ast.Node) (children []*ast.Node) {
  333. children = []*ast.Node{}
  334. if !node.IsContainerBlock() || ast.NodeDocument == node.Type {
  335. children = append(children, node)
  336. return
  337. }
  338. ast.Walk(node, func(n *ast.Node, entering bool) ast.WalkStatus {
  339. if !entering || !n.IsBlock() {
  340. return ast.WalkContinue
  341. }
  342. children = append(children, n)
  343. return ast.WalkContinue
  344. })
  345. return
  346. }
  347. func ParentBlock(node *ast.Node) *ast.Node {
  348. for p := node.Parent; nil != p; p = p.Parent {
  349. if "" != p.ID && p.IsBlock() {
  350. return p
  351. }
  352. }
  353. return nil
  354. }
  355. func GetNodeInTree(tree *parse.Tree, id string) (ret *ast.Node) {
  356. ast.Walk(tree.Root, func(n *ast.Node, entering bool) ast.WalkStatus {
  357. if !entering {
  358. return ast.WalkContinue
  359. }
  360. if id == n.ID {
  361. ret = n
  362. ret.Box = tree.Box
  363. ret.Path = tree.Path
  364. return ast.WalkStop
  365. }
  366. return ast.WalkContinue
  367. })
  368. return
  369. }
  370. func GetDocTitleImgPath(root *ast.Node) (ret string) {
  371. if nil == root {
  372. return
  373. }
  374. const background = "background-image: url("
  375. titleImg := root.IALAttr("title-img")
  376. titleImg = strings.TrimSpace(titleImg)
  377. titleImg = html.UnescapeString(titleImg)
  378. titleImg = strings.ReplaceAll(titleImg, "background-image:url(", background)
  379. if !strings.Contains(titleImg, background) {
  380. return
  381. }
  382. start := strings.Index(titleImg, background) + len(background)
  383. end := strings.LastIndex(titleImg, ")")
  384. ret = titleImg[start:end]
  385. ret = strings.TrimPrefix(ret, "\"")
  386. ret = strings.TrimPrefix(ret, "'")
  387. ret = strings.TrimSuffix(ret, "\"")
  388. ret = strings.TrimSuffix(ret, "'")
  389. return ret
  390. }
  391. var typeAbbrMap = map[string]string{
  392. // 块级元素
  393. "NodeDocument": "d",
  394. "NodeHeading": "h",
  395. "NodeList": "l",
  396. "NodeListItem": "i",
  397. "NodeCodeBlock": "c",
  398. "NodeMathBlock": "m",
  399. "NodeTable": "t",
  400. "NodeBlockquote": "b",
  401. "NodeSuperBlock": "s",
  402. "NodeParagraph": "p",
  403. "NodeHTMLBlock": "html",
  404. "NodeBlockQueryEmbed": "query_embed",
  405. "NodeAttributeView": "av",
  406. "NodeKramdownBlockIAL": "ial",
  407. "NodeIFrame": "iframe",
  408. "NodeWidget": "widget",
  409. "NodeThematicBreak": "tb",
  410. "NodeVideo": "video",
  411. "NodeAudio": "audio",
  412. // 行级元素
  413. "NodeText": "text",
  414. "NodeImage": "img",
  415. "NodeLinkText": "link_text",
  416. "NodeLinkDest": "link_dest",
  417. "NodeTextMark": "textmark",
  418. }
  419. var abbrTypeMap = map[string]string{}
  420. func init() {
  421. for typ, abbr := range typeAbbrMap {
  422. abbrTypeMap[abbr] = typ
  423. }
  424. }
  425. func TypeAbbr(nodeType string) string {
  426. return typeAbbrMap[nodeType]
  427. }
  428. func FromAbbrType(abbrType string) string {
  429. return abbrTypeMap[abbrType]
  430. }
  431. func SubTypeAbbr(n *ast.Node) string {
  432. switch n.Type {
  433. case ast.NodeList, ast.NodeListItem:
  434. if 0 == n.ListData.Typ {
  435. return "u"
  436. }
  437. if 1 == n.ListData.Typ {
  438. return "o"
  439. }
  440. if 3 == n.ListData.Typ {
  441. return "t"
  442. }
  443. case ast.NodeHeading:
  444. if 1 == n.HeadingLevel {
  445. return "h1"
  446. }
  447. if 2 == n.HeadingLevel {
  448. return "h2"
  449. }
  450. if 3 == n.HeadingLevel {
  451. return "h3"
  452. }
  453. if 4 == n.HeadingLevel {
  454. return "h4"
  455. }
  456. if 5 == n.HeadingLevel {
  457. return "h5"
  458. }
  459. if 6 == n.HeadingLevel {
  460. return "h6"
  461. }
  462. }
  463. return ""
  464. }
  465. var DynamicRefTexts = sync.Map{}
  466. func SetDynamicBlockRefText(blockRef *ast.Node, refText string) {
  467. if !IsBlockRef(blockRef) {
  468. return
  469. }
  470. blockRef.TextMarkBlockRefSubtype = "d"
  471. blockRef.TextMarkTextContent = refText
  472. // 偶发编辑文档标题后引用处的动态锚文本不更新 https://github.com/siyuan-note/siyuan/issues/5891
  473. DynamicRefTexts.Store(blockRef.TextMarkBlockRefID, refText)
  474. }
  475. func IsChartCodeBlockCode(code *ast.Node) bool {
  476. if nil == code.Previous || ast.NodeCodeBlockFenceInfoMarker != code.Previous.Type || 1 > len(code.Previous.CodeBlockInfo) {
  477. return false
  478. }
  479. language := gulu.Str.FromBytes(code.Previous.CodeBlockInfo)
  480. language = strings.ReplaceAll(language, editor.Caret, "")
  481. return render.NoHighlight(language)
  482. }
  483. func GetAttributeViewName(avID string) (name string) {
  484. if "" == avID {
  485. return
  486. }
  487. attrView, err := av.ParseAttributeView(avID)
  488. if nil != err {
  489. logging.LogErrorf("parse attribute view [%s] failed: %s", avID, err)
  490. return
  491. }
  492. buf := bytes.Buffer{}
  493. for _, v := range attrView.Views {
  494. buf.WriteString(v.Name)
  495. buf.WriteByte(' ')
  496. }
  497. name = strings.TrimSpace(buf.String())
  498. return
  499. }
  500. func getAttributeViewName(avID string) (name string) {
  501. if "" == avID {
  502. return
  503. }
  504. attrView, err := av.ParseAttributeView(avID)
  505. if nil != err {
  506. logging.LogErrorf("parse attribute view [%s] failed: %s", avID, err)
  507. return
  508. }
  509. buf := bytes.Buffer{}
  510. buf.WriteString(attrView.Name)
  511. buf.WriteByte(' ')
  512. for _, v := range attrView.Views {
  513. buf.WriteString(v.Name)
  514. buf.WriteByte(' ')
  515. }
  516. name = strings.TrimSpace(buf.String())
  517. return
  518. }
  519. func getAttributeViewContent(avID string) (content string) {
  520. if "" == avID {
  521. return
  522. }
  523. attrView, err := av.ParseAttributeView(avID)
  524. if nil != err {
  525. logging.LogErrorf("parse attribute view [%s] failed: %s", avID, err)
  526. return
  527. }
  528. buf := bytes.Buffer{}
  529. buf.WriteString(attrView.Name)
  530. buf.WriteByte(' ')
  531. for _, v := range attrView.Views {
  532. buf.WriteString(v.Name)
  533. buf.WriteByte(' ')
  534. }
  535. if 1 > len(attrView.Views) {
  536. content = strings.TrimSpace(buf.String())
  537. return
  538. }
  539. var view *av.View
  540. for _, v := range attrView.Views {
  541. if av.LayoutTypeTable == v.LayoutType {
  542. view = v
  543. break
  544. }
  545. }
  546. if nil == view {
  547. content = strings.TrimSpace(buf.String())
  548. return
  549. }
  550. table, err := renderAttributeViewTable(attrView, view)
  551. if nil != err {
  552. content = strings.TrimSpace(buf.String())
  553. return
  554. }
  555. for _, col := range table.Columns {
  556. buf.WriteString(col.Name)
  557. buf.WriteByte(' ')
  558. }
  559. for _, row := range table.Rows {
  560. for _, cell := range row.Cells {
  561. if nil == cell.Value {
  562. continue
  563. }
  564. buf.WriteString(cell.Value.String())
  565. buf.WriteByte(' ')
  566. }
  567. }
  568. content = strings.TrimSpace(buf.String())
  569. return
  570. }
  571. func renderAttributeViewTable(attrView *av.AttributeView, view *av.View) (ret *av.Table, err error) {
  572. ret = &av.Table{
  573. ID: view.ID,
  574. Icon: view.Icon,
  575. Name: view.Name,
  576. HideAttrViewName: view.HideAttrViewName,
  577. Columns: []*av.TableColumn{},
  578. Rows: []*av.TableRow{},
  579. }
  580. // 组装列
  581. for _, col := range view.Table.Columns {
  582. key, _ := attrView.GetKey(col.ID)
  583. if nil == key {
  584. continue
  585. }
  586. ret.Columns = append(ret.Columns, &av.TableColumn{
  587. ID: key.ID,
  588. Name: key.Name,
  589. Type: key.Type,
  590. Icon: key.Icon,
  591. Options: key.Options,
  592. NumberFormat: key.NumberFormat,
  593. Template: key.Template,
  594. Relation: key.Relation,
  595. Rollup: key.Rollup,
  596. Wrap: col.Wrap,
  597. Hidden: col.Hidden,
  598. Width: col.Width,
  599. Pin: col.Pin,
  600. Calc: col.Calc,
  601. })
  602. }
  603. // 生成行
  604. rows := map[string][]*av.KeyValues{}
  605. for _, keyValues := range attrView.KeyValues {
  606. for _, val := range keyValues.Values {
  607. values := rows[val.BlockID]
  608. if nil == values {
  609. values = []*av.KeyValues{{Key: keyValues.Key, Values: []*av.Value{val}}}
  610. } else {
  611. values = append(values, &av.KeyValues{Key: keyValues.Key, Values: []*av.Value{val}})
  612. }
  613. rows[val.BlockID] = values
  614. }
  615. }
  616. // 过滤掉不存在的行
  617. var notFound []string
  618. for blockID, keyValues := range rows {
  619. blockValue := getRowBlockValue(keyValues)
  620. if nil == blockValue {
  621. notFound = append(notFound, blockID)
  622. continue
  623. }
  624. if blockValue.IsDetached {
  625. continue
  626. }
  627. if nil != blockValue.Block && "" == blockValue.Block.ID {
  628. notFound = append(notFound, blockID)
  629. continue
  630. }
  631. if GetBlockTree(blockID) == nil {
  632. notFound = append(notFound, blockID)
  633. }
  634. }
  635. for _, blockID := range notFound {
  636. delete(rows, blockID)
  637. }
  638. // 生成行单元格
  639. for rowID, row := range rows {
  640. var tableRow av.TableRow
  641. for _, col := range ret.Columns {
  642. var tableCell *av.TableCell
  643. for _, keyValues := range row {
  644. if keyValues.Key.ID == col.ID {
  645. tableCell = &av.TableCell{
  646. ID: keyValues.Values[0].ID,
  647. Value: keyValues.Values[0],
  648. ValueType: col.Type,
  649. }
  650. break
  651. }
  652. }
  653. if nil == tableCell {
  654. tableCell = &av.TableCell{
  655. ID: ast.NewNodeID(),
  656. ValueType: col.Type,
  657. }
  658. }
  659. tableRow.ID = rowID
  660. switch tableCell.ValueType {
  661. case av.KeyTypeNumber: // 格式化数字
  662. if nil != tableCell.Value && nil != tableCell.Value.Number && tableCell.Value.Number.IsNotEmpty {
  663. tableCell.Value.Number.Format = col.NumberFormat
  664. tableCell.Value.Number.FormatNumber()
  665. }
  666. case av.KeyTypeTemplate: // 渲染模板列
  667. tableCell.Value = &av.Value{ID: tableCell.ID, KeyID: col.ID, BlockID: rowID, Type: av.KeyTypeTemplate, Template: &av.ValueTemplate{Content: col.Template}}
  668. case av.KeyTypeCreated: // 填充创建时间列值,后面再渲染
  669. tableCell.Value = &av.Value{ID: tableCell.ID, KeyID: col.ID, BlockID: rowID, Type: av.KeyTypeCreated}
  670. case av.KeyTypeUpdated: // 填充更新时间列值,后面再渲染
  671. tableCell.Value = &av.Value{ID: tableCell.ID, KeyID: col.ID, BlockID: rowID, Type: av.KeyTypeUpdated}
  672. case av.KeyTypeRelation: // 清空关联列值,后面再渲染 https://ld246.com/article/1703831044435
  673. if nil != tableCell.Value && nil != tableCell.Value.Relation {
  674. tableCell.Value.Relation.Contents = nil
  675. }
  676. }
  677. FillAttributeViewTableCellNilValue(tableCell, rowID, col.ID)
  678. tableRow.Cells = append(tableRow.Cells, tableCell)
  679. }
  680. ret.Rows = append(ret.Rows, &tableRow)
  681. }
  682. // 渲染自动生成的列值,比如关联列、汇总列、创建时间列和更新时间列
  683. for _, row := range ret.Rows {
  684. for _, cell := range row.Cells {
  685. switch cell.ValueType {
  686. case av.KeyTypeRollup: // 渲染汇总列
  687. rollupKey, _ := attrView.GetKey(cell.Value.KeyID)
  688. if nil == rollupKey || nil == rollupKey.Rollup {
  689. break
  690. }
  691. relKey, _ := attrView.GetKey(rollupKey.Rollup.RelationKeyID)
  692. if nil == relKey || nil == relKey.Relation {
  693. break
  694. }
  695. relVal := attrView.GetValue(relKey.ID, row.ID)
  696. if nil == relVal || nil == relVal.Relation {
  697. break
  698. }
  699. destAv, _ := av.ParseAttributeView(relKey.Relation.AvID)
  700. if nil == destAv {
  701. break
  702. }
  703. destKey, _ := destAv.GetKey(rollupKey.Rollup.KeyID)
  704. if nil == destKey {
  705. continue
  706. }
  707. for _, blockID := range relVal.Relation.BlockIDs {
  708. destVal := destAv.GetValue(rollupKey.Rollup.KeyID, blockID)
  709. if nil == destVal {
  710. destVal = GetAttributeViewDefaultValue(ast.NewNodeID(), rollupKey.Rollup.KeyID, blockID, destKey.Type)
  711. }
  712. if av.KeyTypeNumber == destKey.Type {
  713. destVal.Number.Format = destKey.NumberFormat
  714. destVal.Number.FormatNumber()
  715. }
  716. cell.Value.Rollup.Contents = append(cell.Value.Rollup.Contents, destVal.Clone())
  717. }
  718. cell.Value.Rollup.RenderContents(rollupKey.Rollup.Calc, destKey)
  719. // 将汇总列的值保存到 rows 中,后续渲染模板列的时候会用到,下同
  720. // Database table view template columns support reading relation, rollup, created and updated columns https://github.com/siyuan-note/siyuan/issues/10442
  721. keyValues := rows[row.ID]
  722. keyValues = append(keyValues, &av.KeyValues{Key: rollupKey, Values: []*av.Value{{ID: cell.Value.ID, KeyID: rollupKey.ID, BlockID: row.ID, Type: av.KeyTypeRollup, Rollup: cell.Value.Rollup}}})
  723. rows[row.ID] = keyValues
  724. case av.KeyTypeRelation: // 渲染关联列
  725. relKey, _ := attrView.GetKey(cell.Value.KeyID)
  726. if nil != relKey && nil != relKey.Relation {
  727. destAv, _ := av.ParseAttributeView(relKey.Relation.AvID)
  728. if nil != destAv {
  729. blocks := map[string]string{}
  730. for _, blockValue := range destAv.GetBlockKeyValues().Values {
  731. blocks[blockValue.BlockID] = blockValue.Block.Content
  732. }
  733. for _, blockID := range cell.Value.Relation.BlockIDs {
  734. cell.Value.Relation.Contents = append(cell.Value.Relation.Contents, blocks[blockID])
  735. }
  736. }
  737. }
  738. case av.KeyTypeCreated: // 渲染创建时间
  739. createdStr := row.ID[:len("20060102150405")]
  740. created, parseErr := time.ParseInLocation("20060102150405", createdStr, time.Local)
  741. if nil == parseErr {
  742. cell.Value.Created = av.NewFormattedValueCreated(created.UnixMilli(), 0, av.CreatedFormatNone)
  743. cell.Value.Created.IsNotEmpty = true
  744. } else {
  745. cell.Value.Created = av.NewFormattedValueCreated(time.Now().UnixMilli(), 0, av.CreatedFormatNone)
  746. }
  747. keyValues := rows[row.ID]
  748. createdKey, _ := attrView.GetKey(cell.Value.KeyID)
  749. keyValues = append(keyValues, &av.KeyValues{Key: createdKey, Values: []*av.Value{{ID: cell.Value.ID, KeyID: createdKey.ID, BlockID: row.ID, Type: av.KeyTypeCreated, Created: cell.Value.Created}}})
  750. rows[row.ID] = keyValues
  751. case av.KeyTypeUpdated: // 渲染更新时间
  752. ial := map[string]string{}
  753. block := row.GetBlockValue()
  754. if nil != block && !block.IsDetached {
  755. ial = cache.GetBlockIAL(row.ID)
  756. if nil == ial {
  757. ial = map[string]string{}
  758. }
  759. }
  760. updatedStr := ial["updated"]
  761. if "" == updatedStr && nil != block {
  762. cell.Value.Updated = av.NewFormattedValueUpdated(block.Block.Updated, 0, av.UpdatedFormatNone)
  763. cell.Value.Updated.IsNotEmpty = true
  764. } else {
  765. updated, parseErr := time.ParseInLocation("20060102150405", updatedStr, time.Local)
  766. if nil == parseErr {
  767. cell.Value.Updated = av.NewFormattedValueUpdated(updated.UnixMilli(), 0, av.UpdatedFormatNone)
  768. cell.Value.Updated.IsNotEmpty = true
  769. } else {
  770. cell.Value.Updated = av.NewFormattedValueUpdated(time.Now().UnixMilli(), 0, av.UpdatedFormatNone)
  771. }
  772. }
  773. keyValues := rows[row.ID]
  774. updatedKey, _ := attrView.GetKey(cell.Value.KeyID)
  775. keyValues = append(keyValues, &av.KeyValues{Key: updatedKey, Values: []*av.Value{{ID: cell.Value.ID, KeyID: updatedKey.ID, BlockID: row.ID, Type: av.KeyTypeUpdated, Updated: cell.Value.Updated}}})
  776. rows[row.ID] = keyValues
  777. }
  778. }
  779. }
  780. // 最后单独渲染模板列,这样模板列就可以使用汇总、关联、创建时间和更新时间列的值了
  781. // Database table view template columns support reading relation, rollup, created and updated columns https://github.com/siyuan-note/siyuan/issues/10442
  782. for _, row := range ret.Rows {
  783. for _, cell := range row.Cells {
  784. switch cell.ValueType {
  785. case av.KeyTypeTemplate: // 渲染模板列
  786. keyValues := rows[row.ID]
  787. ial := map[string]string{}
  788. block := row.GetBlockValue()
  789. if nil != block && !block.IsDetached {
  790. ial = cache.GetBlockIAL(row.ID)
  791. if nil == ial {
  792. ial = map[string]string{}
  793. }
  794. }
  795. content := renderTemplateCol(ial, cell.Value.Template.Content, keyValues)
  796. cell.Value.Template.Content = content
  797. }
  798. }
  799. }
  800. return
  801. }
  802. func FillAttributeViewTableCellNilValue(tableCell *av.TableCell, rowID, colID string) {
  803. if nil == tableCell.Value {
  804. tableCell.Value = GetAttributeViewDefaultValue(tableCell.ID, colID, rowID, tableCell.ValueType)
  805. return
  806. }
  807. tableCell.Value.Type = tableCell.ValueType
  808. switch tableCell.ValueType {
  809. case av.KeyTypeText:
  810. if nil == tableCell.Value.Text {
  811. tableCell.Value.Text = &av.ValueText{}
  812. }
  813. case av.KeyTypeNumber:
  814. if nil == tableCell.Value.Number {
  815. tableCell.Value.Number = &av.ValueNumber{}
  816. }
  817. case av.KeyTypeDate:
  818. if nil == tableCell.Value.Date {
  819. tableCell.Value.Date = &av.ValueDate{}
  820. }
  821. case av.KeyTypeSelect:
  822. if 1 > len(tableCell.Value.MSelect) {
  823. tableCell.Value.MSelect = []*av.ValueSelect{}
  824. }
  825. case av.KeyTypeMSelect:
  826. if 1 > len(tableCell.Value.MSelect) {
  827. tableCell.Value.MSelect = []*av.ValueSelect{}
  828. }
  829. case av.KeyTypeURL:
  830. if nil == tableCell.Value.URL {
  831. tableCell.Value.URL = &av.ValueURL{}
  832. }
  833. case av.KeyTypeEmail:
  834. if nil == tableCell.Value.Email {
  835. tableCell.Value.Email = &av.ValueEmail{}
  836. }
  837. case av.KeyTypePhone:
  838. if nil == tableCell.Value.Phone {
  839. tableCell.Value.Phone = &av.ValuePhone{}
  840. }
  841. case av.KeyTypeMAsset:
  842. if 1 > len(tableCell.Value.MAsset) {
  843. tableCell.Value.MAsset = []*av.ValueAsset{}
  844. }
  845. case av.KeyTypeTemplate:
  846. if nil == tableCell.Value.Template {
  847. tableCell.Value.Template = &av.ValueTemplate{}
  848. }
  849. case av.KeyTypeCreated:
  850. if nil == tableCell.Value.Created {
  851. tableCell.Value.Created = &av.ValueCreated{}
  852. }
  853. case av.KeyTypeUpdated:
  854. if nil == tableCell.Value.Updated {
  855. tableCell.Value.Updated = &av.ValueUpdated{}
  856. }
  857. case av.KeyTypeCheckbox:
  858. if nil == tableCell.Value.Checkbox {
  859. tableCell.Value.Checkbox = &av.ValueCheckbox{}
  860. }
  861. case av.KeyTypeRelation:
  862. if nil == tableCell.Value.Relation {
  863. tableCell.Value.Relation = &av.ValueRelation{}
  864. }
  865. case av.KeyTypeRollup:
  866. if nil == tableCell.Value.Rollup {
  867. tableCell.Value.Rollup = &av.ValueRollup{}
  868. }
  869. }
  870. }
  871. func GetAttributeViewDefaultValue(valueID, keyID, blockID string, typ av.KeyType) (ret *av.Value) {
  872. if "" == valueID {
  873. valueID = ast.NewNodeID()
  874. }
  875. ret = &av.Value{ID: valueID, KeyID: keyID, BlockID: blockID, Type: typ}
  876. createdStr := valueID[:len("20060102150405")]
  877. created, parseErr := time.ParseInLocation("20060102150405", createdStr, time.Local)
  878. if nil == parseErr {
  879. ret.CreatedAt = created.UnixMilli()
  880. } else {
  881. ret.CreatedAt = time.Now().UnixMilli()
  882. }
  883. if 0 == ret.UpdatedAt {
  884. ret.UpdatedAt = ret.CreatedAt
  885. }
  886. switch typ {
  887. case av.KeyTypeText:
  888. ret.Text = &av.ValueText{}
  889. case av.KeyTypeNumber:
  890. ret.Number = &av.ValueNumber{}
  891. case av.KeyTypeDate:
  892. ret.Date = &av.ValueDate{}
  893. case av.KeyTypeSelect:
  894. ret.MSelect = []*av.ValueSelect{}
  895. case av.KeyTypeMSelect:
  896. ret.MSelect = []*av.ValueSelect{}
  897. case av.KeyTypeURL:
  898. ret.URL = &av.ValueURL{}
  899. case av.KeyTypeEmail:
  900. ret.Email = &av.ValueEmail{}
  901. case av.KeyTypePhone:
  902. ret.Phone = &av.ValuePhone{}
  903. case av.KeyTypeMAsset:
  904. ret.MAsset = []*av.ValueAsset{}
  905. case av.KeyTypeTemplate:
  906. ret.Template = &av.ValueTemplate{}
  907. case av.KeyTypeCreated:
  908. ret.Created = &av.ValueCreated{}
  909. case av.KeyTypeUpdated:
  910. ret.Updated = &av.ValueUpdated{}
  911. case av.KeyTypeCheckbox:
  912. ret.Checkbox = &av.ValueCheckbox{}
  913. case av.KeyTypeRelation:
  914. ret.Relation = &av.ValueRelation{}
  915. case av.KeyTypeRollup:
  916. ret.Rollup = &av.ValueRollup{}
  917. }
  918. return
  919. }
  920. func renderTemplateCol(ial map[string]string, tplContent string, rowValues []*av.KeyValues) string {
  921. if "" == ial["id"] {
  922. block := getRowBlockValue(rowValues)
  923. ial["id"] = block.Block.ID
  924. }
  925. if "" == ial["updated"] {
  926. block := getRowBlockValue(rowValues)
  927. ial["updated"] = time.UnixMilli(block.Block.Updated).Format("20060102150405")
  928. }
  929. goTpl := template.New("").Delims(".action{", "}")
  930. tplFuncMap := util.BuiltInTemplateFuncs()
  931. // 这里存在依赖问题所以不支持 SQLTemplateFuncs(&tplFuncMap)
  932. goTpl = goTpl.Funcs(tplFuncMap)
  933. tpl, tplErr := goTpl.Funcs(tplFuncMap).Parse(tplContent)
  934. if nil != tplErr {
  935. logging.LogWarnf("parse template [%s] failed: %s", tplContent, tplErr)
  936. return ""
  937. }
  938. buf := &bytes.Buffer{}
  939. dataModel := map[string]interface{}{} // 复制一份 IAL 以避免修改原始数据
  940. for k, v := range ial {
  941. dataModel[k] = v
  942. // Database template column supports `created` and `updated` built-in variables https://github.com/siyuan-note/siyuan/issues/9364
  943. createdStr := ial["id"]
  944. if "" != createdStr {
  945. createdStr = createdStr[:len("20060102150405")]
  946. }
  947. created, parseErr := time.ParseInLocation("20060102150405", createdStr, time.Local)
  948. if nil == parseErr {
  949. dataModel["created"] = created
  950. } else {
  951. logging.LogWarnf("parse created [%s] failed: %s", createdStr, parseErr)
  952. dataModel["created"] = time.Now()
  953. }
  954. updatedStr := ial["updated"]
  955. updated, parseErr := time.ParseInLocation("20060102150405", updatedStr, time.Local)
  956. if nil == parseErr {
  957. dataModel["updated"] = updated
  958. } else {
  959. dataModel["updated"] = time.Now()
  960. }
  961. }
  962. for _, rowValue := range rowValues {
  963. if 0 < len(rowValue.Values) {
  964. v := rowValue.Values[0]
  965. if av.KeyTypeNumber == v.Type {
  966. dataModel[rowValue.Key.Name] = v.Number.Content
  967. } else if av.KeyTypeDate == v.Type {
  968. dataModel[rowValue.Key.Name] = time.UnixMilli(v.Date.Content)
  969. } else {
  970. dataModel[rowValue.Key.Name] = v.String()
  971. }
  972. }
  973. }
  974. if err := tpl.Execute(buf, dataModel); nil != err {
  975. logging.LogWarnf("execute template [%s] failed: %s", tplContent, err)
  976. }
  977. return buf.String()
  978. }
  979. func getRowBlockValue(keyValues []*av.KeyValues) (ret *av.Value) {
  980. for _, kv := range keyValues {
  981. if av.KeyTypeBlock == kv.Key.Type && 0 < len(kv.Values) {
  982. ret = kv.Values[0]
  983. break
  984. }
  985. }
  986. return
  987. }