node.go 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024
  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. }
  255. lastSpace = false
  256. return ast.WalkContinue
  257. })
  258. return strings.TrimSpace(buf.String())
  259. }
  260. func FirstLeafBlock(node *ast.Node) (ret *ast.Node) {
  261. ast.Walk(node, func(n *ast.Node, entering bool) ast.WalkStatus {
  262. if !entering || n.IsMarker() {
  263. return ast.WalkContinue
  264. }
  265. if !n.IsContainerBlock() {
  266. ret = n
  267. return ast.WalkStop
  268. }
  269. return ast.WalkContinue
  270. })
  271. return
  272. }
  273. func CountBlockNodes(node *ast.Node) (ret int) {
  274. ast.Walk(node, func(n *ast.Node, entering bool) ast.WalkStatus {
  275. if !entering || !n.IsBlock() || ast.NodeList == n.Type || ast.NodeBlockquote == n.Type || ast.NodeSuperBlock == n.Type {
  276. return ast.WalkContinue
  277. }
  278. if "1" == n.IALAttr("fold") {
  279. ret++
  280. return ast.WalkSkipChildren
  281. }
  282. ret++
  283. return ast.WalkContinue
  284. })
  285. return
  286. }
  287. func ParentNodes(node *ast.Node) (parents []*ast.Node) {
  288. const maxDepth = 256
  289. i := 0
  290. for n := node.Parent; nil != n; n = n.Parent {
  291. i++
  292. parents = append(parents, n)
  293. if ast.NodeDocument == n.Type {
  294. return
  295. }
  296. if maxDepth < i {
  297. logging.LogWarnf("parent nodes of node [%s] is too deep", node.ID)
  298. return
  299. }
  300. }
  301. return
  302. }
  303. func ChildBlockNodes(node *ast.Node) (children []*ast.Node) {
  304. children = []*ast.Node{}
  305. if !node.IsContainerBlock() || ast.NodeDocument == node.Type {
  306. children = append(children, node)
  307. return
  308. }
  309. ast.Walk(node, func(n *ast.Node, entering bool) ast.WalkStatus {
  310. if !entering || !n.IsBlock() {
  311. return ast.WalkContinue
  312. }
  313. children = append(children, n)
  314. return ast.WalkContinue
  315. })
  316. return
  317. }
  318. func ParentBlock(node *ast.Node) *ast.Node {
  319. for p := node.Parent; nil != p; p = p.Parent {
  320. if "" != p.ID && p.IsBlock() {
  321. return p
  322. }
  323. }
  324. return nil
  325. }
  326. func GetNodeInTree(tree *parse.Tree, id string) (ret *ast.Node) {
  327. ast.Walk(tree.Root, func(n *ast.Node, entering bool) ast.WalkStatus {
  328. if !entering {
  329. return ast.WalkContinue
  330. }
  331. if id == n.ID {
  332. ret = n
  333. ret.Box = tree.Box
  334. ret.Path = tree.Path
  335. return ast.WalkStop
  336. }
  337. return ast.WalkContinue
  338. })
  339. return
  340. }
  341. func GetDocTitleImgPath(root *ast.Node) (ret string) {
  342. if nil == root {
  343. return
  344. }
  345. const background = "background-image: url("
  346. titleImg := root.IALAttr("title-img")
  347. titleImg = strings.TrimSpace(titleImg)
  348. titleImg = html.UnescapeString(titleImg)
  349. titleImg = strings.ReplaceAll(titleImg, "background-image:url(", background)
  350. if !strings.Contains(titleImg, background) {
  351. return
  352. }
  353. start := strings.Index(titleImg, background) + len(background)
  354. end := strings.LastIndex(titleImg, ")")
  355. ret = titleImg[start:end]
  356. ret = strings.TrimPrefix(ret, "\"")
  357. ret = strings.TrimPrefix(ret, "'")
  358. ret = strings.TrimSuffix(ret, "\"")
  359. ret = strings.TrimSuffix(ret, "'")
  360. return ret
  361. }
  362. var typeAbbrMap = map[string]string{
  363. // 块级元素
  364. "NodeDocument": "d",
  365. "NodeHeading": "h",
  366. "NodeList": "l",
  367. "NodeListItem": "i",
  368. "NodeCodeBlock": "c",
  369. "NodeMathBlock": "m",
  370. "NodeTable": "t",
  371. "NodeBlockquote": "b",
  372. "NodeSuperBlock": "s",
  373. "NodeParagraph": "p",
  374. "NodeHTMLBlock": "html",
  375. "NodeBlockQueryEmbed": "query_embed",
  376. "NodeAttributeView": "av",
  377. "NodeKramdownBlockIAL": "ial",
  378. "NodeIFrame": "iframe",
  379. "NodeWidget": "widget",
  380. "NodeThematicBreak": "tb",
  381. "NodeVideo": "video",
  382. "NodeAudio": "audio",
  383. // 行级元素
  384. "NodeText": "text",
  385. "NodeImage": "img",
  386. "NodeLinkText": "link_text",
  387. "NodeLinkDest": "link_dest",
  388. "NodeTextMark": "textmark",
  389. }
  390. var abbrTypeMap = map[string]string{}
  391. func init() {
  392. for typ, abbr := range typeAbbrMap {
  393. abbrTypeMap[abbr] = typ
  394. }
  395. }
  396. func TypeAbbr(nodeType string) string {
  397. return typeAbbrMap[nodeType]
  398. }
  399. func FromAbbrType(abbrType string) string {
  400. return abbrTypeMap[abbrType]
  401. }
  402. func SubTypeAbbr(n *ast.Node) string {
  403. switch n.Type {
  404. case ast.NodeList, ast.NodeListItem:
  405. if 0 == n.ListData.Typ {
  406. return "u"
  407. }
  408. if 1 == n.ListData.Typ {
  409. return "o"
  410. }
  411. if 3 == n.ListData.Typ {
  412. return "t"
  413. }
  414. case ast.NodeHeading:
  415. if 1 == n.HeadingLevel {
  416. return "h1"
  417. }
  418. if 2 == n.HeadingLevel {
  419. return "h2"
  420. }
  421. if 3 == n.HeadingLevel {
  422. return "h3"
  423. }
  424. if 4 == n.HeadingLevel {
  425. return "h4"
  426. }
  427. if 5 == n.HeadingLevel {
  428. return "h5"
  429. }
  430. if 6 == n.HeadingLevel {
  431. return "h6"
  432. }
  433. }
  434. return ""
  435. }
  436. var DynamicRefTexts = sync.Map{}
  437. func SetDynamicBlockRefText(blockRef *ast.Node, refText string) {
  438. if !IsBlockRef(blockRef) {
  439. return
  440. }
  441. blockRef.TextMarkBlockRefSubtype = "d"
  442. blockRef.TextMarkTextContent = refText
  443. // 偶发编辑文档标题后引用处的动态锚文本不更新 https://github.com/siyuan-note/siyuan/issues/5891
  444. DynamicRefTexts.Store(blockRef.TextMarkBlockRefID, refText)
  445. }
  446. func IsChartCodeBlockCode(code *ast.Node) bool {
  447. if nil == code.Previous || ast.NodeCodeBlockFenceInfoMarker != code.Previous.Type || 1 > len(code.Previous.CodeBlockInfo) {
  448. return false
  449. }
  450. language := gulu.Str.FromBytes(code.Previous.CodeBlockInfo)
  451. language = strings.ReplaceAll(language, editor.Caret, "")
  452. return render.NoHighlight(language)
  453. }
  454. func GetAttributeViewName(avID string) (name string) {
  455. if "" == avID {
  456. return
  457. }
  458. attrView, err := av.ParseAttributeView(avID)
  459. if nil != err {
  460. logging.LogErrorf("parse attribute view [%s] failed: %s", avID, err)
  461. return
  462. }
  463. buf := bytes.Buffer{}
  464. for _, v := range attrView.Views {
  465. buf.WriteString(v.Name)
  466. buf.WriteByte(' ')
  467. }
  468. name = strings.TrimSpace(buf.String())
  469. return
  470. }
  471. func getAttributeViewName(avID string) (name string) {
  472. if "" == avID {
  473. return
  474. }
  475. attrView, err := av.ParseAttributeView(avID)
  476. if nil != err {
  477. logging.LogErrorf("parse attribute view [%s] failed: %s", avID, err)
  478. return
  479. }
  480. buf := bytes.Buffer{}
  481. buf.WriteString(attrView.Name)
  482. buf.WriteByte(' ')
  483. for _, v := range attrView.Views {
  484. buf.WriteString(v.Name)
  485. buf.WriteByte(' ')
  486. }
  487. name = strings.TrimSpace(buf.String())
  488. return
  489. }
  490. func getAttributeViewContent(avID string) (content string) {
  491. if "" == avID {
  492. return
  493. }
  494. attrView, err := av.ParseAttributeView(avID)
  495. if nil != err {
  496. logging.LogErrorf("parse attribute view [%s] failed: %s", avID, err)
  497. return
  498. }
  499. buf := bytes.Buffer{}
  500. buf.WriteString(attrView.Name)
  501. buf.WriteByte(' ')
  502. for _, v := range attrView.Views {
  503. buf.WriteString(v.Name)
  504. buf.WriteByte(' ')
  505. }
  506. if 1 > len(attrView.Views) {
  507. content = strings.TrimSpace(buf.String())
  508. return
  509. }
  510. var view *av.View
  511. for _, v := range attrView.Views {
  512. if av.LayoutTypeTable == v.LayoutType {
  513. view = v
  514. break
  515. }
  516. }
  517. if nil == view {
  518. content = strings.TrimSpace(buf.String())
  519. return
  520. }
  521. table, err := renderAttributeViewTable(attrView, view)
  522. if nil != err {
  523. content = strings.TrimSpace(buf.String())
  524. return
  525. }
  526. for _, col := range table.Columns {
  527. buf.WriteString(col.Name)
  528. buf.WriteByte(' ')
  529. }
  530. for _, row := range table.Rows {
  531. for _, cell := range row.Cells {
  532. if nil == cell.Value {
  533. continue
  534. }
  535. buf.WriteString(cell.Value.String())
  536. buf.WriteByte(' ')
  537. }
  538. }
  539. content = strings.TrimSpace(buf.String())
  540. return
  541. }
  542. func renderAttributeViewTable(attrView *av.AttributeView, view *av.View) (ret *av.Table, err error) {
  543. ret = &av.Table{
  544. ID: view.ID,
  545. Icon: view.Icon,
  546. Name: view.Name,
  547. Columns: []*av.TableColumn{},
  548. Rows: []*av.TableRow{},
  549. }
  550. // 组装列
  551. for _, col := range view.Table.Columns {
  552. key, _ := attrView.GetKey(col.ID)
  553. if nil == key {
  554. continue
  555. }
  556. ret.Columns = append(ret.Columns, &av.TableColumn{
  557. ID: key.ID,
  558. Name: key.Name,
  559. Type: key.Type,
  560. Icon: key.Icon,
  561. Options: key.Options,
  562. NumberFormat: key.NumberFormat,
  563. Template: key.Template,
  564. Relation: key.Relation,
  565. Rollup: key.Rollup,
  566. Wrap: col.Wrap,
  567. Hidden: col.Hidden,
  568. Width: col.Width,
  569. Pin: col.Pin,
  570. Calc: col.Calc,
  571. })
  572. }
  573. // 生成行
  574. rows := map[string][]*av.KeyValues{}
  575. for _, keyValues := range attrView.KeyValues {
  576. for _, val := range keyValues.Values {
  577. values := rows[val.BlockID]
  578. if nil == values {
  579. values = []*av.KeyValues{{Key: keyValues.Key, Values: []*av.Value{val}}}
  580. } else {
  581. values = append(values, &av.KeyValues{Key: keyValues.Key, Values: []*av.Value{val}})
  582. }
  583. rows[val.BlockID] = values
  584. }
  585. }
  586. // 过滤掉不存在的行
  587. var notFound []string
  588. for blockID, keyValues := range rows {
  589. blockValue := getRowBlockValue(keyValues)
  590. if nil == blockValue {
  591. notFound = append(notFound, blockID)
  592. continue
  593. }
  594. if blockValue.IsDetached {
  595. continue
  596. }
  597. if nil != blockValue.Block && "" == blockValue.Block.ID {
  598. notFound = append(notFound, blockID)
  599. continue
  600. }
  601. if GetBlockTree(blockID) == nil {
  602. notFound = append(notFound, blockID)
  603. }
  604. }
  605. for _, blockID := range notFound {
  606. delete(rows, blockID)
  607. }
  608. // 生成行单元格
  609. for rowID, row := range rows {
  610. var tableRow av.TableRow
  611. for _, col := range ret.Columns {
  612. var tableCell *av.TableCell
  613. for _, keyValues := range row {
  614. if keyValues.Key.ID == col.ID {
  615. tableCell = &av.TableCell{
  616. ID: keyValues.Values[0].ID,
  617. Value: keyValues.Values[0],
  618. ValueType: col.Type,
  619. }
  620. break
  621. }
  622. }
  623. if nil == tableCell {
  624. tableCell = &av.TableCell{
  625. ID: ast.NewNodeID(),
  626. ValueType: col.Type,
  627. }
  628. }
  629. tableRow.ID = rowID
  630. switch tableCell.ValueType {
  631. case av.KeyTypeNumber: // 格式化数字
  632. if nil != tableCell.Value && nil != tableCell.Value.Number && tableCell.Value.Number.IsNotEmpty {
  633. tableCell.Value.Number.Format = col.NumberFormat
  634. tableCell.Value.Number.FormatNumber()
  635. }
  636. case av.KeyTypeTemplate: // 渲染模板列
  637. tableCell.Value = &av.Value{ID: tableCell.ID, KeyID: col.ID, BlockID: rowID, Type: av.KeyTypeTemplate, Template: &av.ValueTemplate{Content: col.Template}}
  638. case av.KeyTypeCreated: // 填充创建时间列值,后面再渲染
  639. tableCell.Value = &av.Value{ID: tableCell.ID, KeyID: col.ID, BlockID: rowID, Type: av.KeyTypeCreated}
  640. case av.KeyTypeUpdated: // 填充更新时间列值,后面再渲染
  641. tableCell.Value = &av.Value{ID: tableCell.ID, KeyID: col.ID, BlockID: rowID, Type: av.KeyTypeUpdated}
  642. case av.KeyTypeRelation: // 清空关联列值,后面再渲染 https://ld246.com/article/1703831044435
  643. if nil != tableCell.Value && nil != tableCell.Value.Relation {
  644. tableCell.Value.Relation.Contents = nil
  645. }
  646. }
  647. FillAttributeViewTableCellNilValue(tableCell, rowID, col.ID)
  648. tableRow.Cells = append(tableRow.Cells, tableCell)
  649. }
  650. ret.Rows = append(ret.Rows, &tableRow)
  651. }
  652. // 渲染自动生成的列值,比如模板列、关联列、汇总列、创建时间列和更新时间列
  653. for _, row := range ret.Rows {
  654. for _, cell := range row.Cells {
  655. switch cell.ValueType {
  656. case av.KeyTypeTemplate: // 渲染模板列
  657. keyValues := rows[row.ID]
  658. ial := map[string]string{}
  659. block := row.GetBlockValue()
  660. if nil != block && !block.IsDetached {
  661. ial = cache.GetBlockIAL(row.ID)
  662. if nil == ial {
  663. ial = map[string]string{}
  664. }
  665. }
  666. content := renderTemplateCol(ial, cell.Value.Template.Content, keyValues)
  667. cell.Value.Template.Content = content
  668. case av.KeyTypeRollup: // 渲染汇总列
  669. rollupKey, _ := attrView.GetKey(cell.Value.KeyID)
  670. if nil == rollupKey || nil == rollupKey.Rollup {
  671. break
  672. }
  673. relKey, _ := attrView.GetKey(rollupKey.Rollup.RelationKeyID)
  674. if nil == relKey || nil == relKey.Relation {
  675. break
  676. }
  677. relVal := attrView.GetValue(relKey.ID, row.ID)
  678. if nil == relVal || nil == relVal.Relation {
  679. break
  680. }
  681. destAv, _ := av.ParseAttributeView(relKey.Relation.AvID)
  682. if nil == destAv {
  683. break
  684. }
  685. destKey, _ := destAv.GetKey(rollupKey.Rollup.KeyID)
  686. if nil == destKey {
  687. continue
  688. }
  689. for _, blockID := range relVal.Relation.BlockIDs {
  690. destVal := destAv.GetValue(rollupKey.Rollup.KeyID, blockID)
  691. if nil == destVal {
  692. destVal = GetAttributeViewDefaultValue(ast.NewNodeID(), rollupKey.Rollup.KeyID, blockID, destKey.Type)
  693. }
  694. if av.KeyTypeNumber == destKey.Type {
  695. destVal.Number.Format = destKey.NumberFormat
  696. destVal.Number.FormatNumber()
  697. }
  698. cell.Value.Rollup.Contents = append(cell.Value.Rollup.Contents, destVal.Clone())
  699. }
  700. cell.Value.Rollup.RenderContents(rollupKey.Rollup.Calc, destKey)
  701. case av.KeyTypeRelation: // 渲染关联列
  702. relKey, _ := attrView.GetKey(cell.Value.KeyID)
  703. if nil != relKey && nil != relKey.Relation {
  704. destAv, _ := av.ParseAttributeView(relKey.Relation.AvID)
  705. if nil != destAv {
  706. blocks := map[string]string{}
  707. for _, blockValue := range destAv.GetBlockKeyValues().Values {
  708. blocks[blockValue.BlockID] = blockValue.Block.Content
  709. }
  710. for _, blockID := range cell.Value.Relation.BlockIDs {
  711. cell.Value.Relation.Contents = append(cell.Value.Relation.Contents, blocks[blockID])
  712. }
  713. }
  714. }
  715. case av.KeyTypeCreated: // 渲染创建时间
  716. createdStr := row.ID[:len("20060102150405")]
  717. created, parseErr := time.ParseInLocation("20060102150405", createdStr, time.Local)
  718. if nil == parseErr {
  719. cell.Value.Created = av.NewFormattedValueCreated(created.UnixMilli(), 0, av.CreatedFormatNone)
  720. cell.Value.Created.IsNotEmpty = true
  721. } else {
  722. cell.Value.Created = av.NewFormattedValueCreated(time.Now().UnixMilli(), 0, av.CreatedFormatNone)
  723. }
  724. case av.KeyTypeUpdated: // 渲染更新时间
  725. ial := map[string]string{}
  726. block := row.GetBlockValue()
  727. if nil != block && !block.IsDetached {
  728. ial = cache.GetBlockIAL(row.ID)
  729. if nil == ial {
  730. ial = map[string]string{}
  731. }
  732. }
  733. updatedStr := ial["updated"]
  734. if "" == updatedStr && nil != block {
  735. cell.Value.Updated = av.NewFormattedValueUpdated(block.Block.Updated, 0, av.UpdatedFormatNone)
  736. cell.Value.Updated.IsNotEmpty = true
  737. } else {
  738. updated, parseErr := time.ParseInLocation("20060102150405", updatedStr, time.Local)
  739. if nil == parseErr {
  740. cell.Value.Updated = av.NewFormattedValueUpdated(updated.UnixMilli(), 0, av.UpdatedFormatNone)
  741. cell.Value.Updated.IsNotEmpty = true
  742. } else {
  743. cell.Value.Updated = av.NewFormattedValueUpdated(time.Now().UnixMilli(), 0, av.UpdatedFormatNone)
  744. }
  745. }
  746. }
  747. }
  748. }
  749. return
  750. }
  751. func FillAttributeViewTableCellNilValue(tableCell *av.TableCell, rowID, colID string) {
  752. if nil == tableCell.Value {
  753. tableCell.Value = GetAttributeViewDefaultValue(tableCell.ID, colID, rowID, tableCell.ValueType)
  754. return
  755. }
  756. tableCell.Value.Type = tableCell.ValueType
  757. switch tableCell.ValueType {
  758. case av.KeyTypeText:
  759. if nil == tableCell.Value.Text {
  760. tableCell.Value.Text = &av.ValueText{}
  761. }
  762. case av.KeyTypeNumber:
  763. if nil == tableCell.Value.Number {
  764. tableCell.Value.Number = &av.ValueNumber{}
  765. }
  766. case av.KeyTypeDate:
  767. if nil == tableCell.Value.Date {
  768. tableCell.Value.Date = &av.ValueDate{}
  769. }
  770. case av.KeyTypeSelect:
  771. if 1 > len(tableCell.Value.MSelect) {
  772. tableCell.Value.MSelect = []*av.ValueSelect{}
  773. }
  774. case av.KeyTypeMSelect:
  775. if 1 > len(tableCell.Value.MSelect) {
  776. tableCell.Value.MSelect = []*av.ValueSelect{}
  777. }
  778. case av.KeyTypeURL:
  779. if nil == tableCell.Value.URL {
  780. tableCell.Value.URL = &av.ValueURL{}
  781. }
  782. case av.KeyTypeEmail:
  783. if nil == tableCell.Value.Email {
  784. tableCell.Value.Email = &av.ValueEmail{}
  785. }
  786. case av.KeyTypePhone:
  787. if nil == tableCell.Value.Phone {
  788. tableCell.Value.Phone = &av.ValuePhone{}
  789. }
  790. case av.KeyTypeMAsset:
  791. if 1 > len(tableCell.Value.MAsset) {
  792. tableCell.Value.MAsset = []*av.ValueAsset{}
  793. }
  794. case av.KeyTypeTemplate:
  795. if nil == tableCell.Value.Template {
  796. tableCell.Value.Template = &av.ValueTemplate{}
  797. }
  798. case av.KeyTypeCreated:
  799. if nil == tableCell.Value.Created {
  800. tableCell.Value.Created = &av.ValueCreated{}
  801. }
  802. case av.KeyTypeUpdated:
  803. if nil == tableCell.Value.Updated {
  804. tableCell.Value.Updated = &av.ValueUpdated{}
  805. }
  806. case av.KeyTypeCheckbox:
  807. if nil == tableCell.Value.Checkbox {
  808. tableCell.Value.Checkbox = &av.ValueCheckbox{}
  809. }
  810. case av.KeyTypeRelation:
  811. if nil == tableCell.Value.Relation {
  812. tableCell.Value.Relation = &av.ValueRelation{}
  813. }
  814. case av.KeyTypeRollup:
  815. if nil == tableCell.Value.Rollup {
  816. tableCell.Value.Rollup = &av.ValueRollup{}
  817. }
  818. }
  819. }
  820. func GetAttributeViewDefaultValue(valueID, keyID, blockID string, typ av.KeyType) (ret *av.Value) {
  821. ret = &av.Value{ID: valueID, KeyID: keyID, BlockID: blockID, Type: typ}
  822. switch typ {
  823. case av.KeyTypeText:
  824. ret.Text = &av.ValueText{}
  825. case av.KeyTypeNumber:
  826. ret.Number = &av.ValueNumber{}
  827. case av.KeyTypeDate:
  828. ret.Date = &av.ValueDate{}
  829. case av.KeyTypeSelect:
  830. ret.MSelect = []*av.ValueSelect{}
  831. case av.KeyTypeMSelect:
  832. ret.MSelect = []*av.ValueSelect{}
  833. case av.KeyTypeURL:
  834. ret.URL = &av.ValueURL{}
  835. case av.KeyTypeEmail:
  836. ret.Email = &av.ValueEmail{}
  837. case av.KeyTypePhone:
  838. ret.Phone = &av.ValuePhone{}
  839. case av.KeyTypeMAsset:
  840. ret.MAsset = []*av.ValueAsset{}
  841. case av.KeyTypeTemplate:
  842. ret.Template = &av.ValueTemplate{}
  843. case av.KeyTypeCreated:
  844. ret.Created = &av.ValueCreated{}
  845. case av.KeyTypeUpdated:
  846. ret.Updated = &av.ValueUpdated{}
  847. case av.KeyTypeCheckbox:
  848. ret.Checkbox = &av.ValueCheckbox{}
  849. case av.KeyTypeRelation:
  850. ret.Relation = &av.ValueRelation{}
  851. case av.KeyTypeRollup:
  852. ret.Rollup = &av.ValueRollup{}
  853. }
  854. return
  855. }
  856. func renderTemplateCol(ial map[string]string, tplContent string, rowValues []*av.KeyValues) string {
  857. if "" == ial["id"] {
  858. block := getRowBlockValue(rowValues)
  859. ial["id"] = block.Block.ID
  860. }
  861. if "" == ial["updated"] {
  862. block := getRowBlockValue(rowValues)
  863. ial["updated"] = time.UnixMilli(block.Block.Updated).Format("20060102150405")
  864. }
  865. funcMap := util.BuiltInTemplateFuncs()
  866. goTpl := template.New("").Delims(".action{", "}")
  867. tpl, tplErr := goTpl.Funcs(funcMap).Parse(tplContent)
  868. if nil != tplErr {
  869. logging.LogWarnf("parse template [%s] failed: %s", tplContent, tplErr)
  870. return ""
  871. }
  872. buf := &bytes.Buffer{}
  873. dataModel := map[string]interface{}{} // 复制一份 IAL 以避免修改原始数据
  874. for k, v := range ial {
  875. dataModel[k] = v
  876. // Database template column supports `created` and `updated` built-in variables https://github.com/siyuan-note/siyuan/issues/9364
  877. createdStr := ial["id"]
  878. if "" != createdStr {
  879. createdStr = createdStr[:len("20060102150405")]
  880. }
  881. created, parseErr := time.ParseInLocation("20060102150405", createdStr, time.Local)
  882. if nil == parseErr {
  883. dataModel["created"] = created
  884. } else {
  885. logging.LogWarnf("parse created [%s] failed: %s", createdStr, parseErr)
  886. dataModel["created"] = time.Now()
  887. }
  888. updatedStr := ial["updated"]
  889. updated, parseErr := time.ParseInLocation("20060102150405", updatedStr, time.Local)
  890. if nil == parseErr {
  891. dataModel["updated"] = updated
  892. } else {
  893. dataModel["updated"] = time.Now()
  894. }
  895. }
  896. for _, rowValue := range rowValues {
  897. if 0 < len(rowValue.Values) {
  898. v := rowValue.Values[0]
  899. if av.KeyTypeNumber == v.Type {
  900. dataModel[rowValue.Key.Name] = v.Number.Content
  901. } else if av.KeyTypeDate == v.Type {
  902. dataModel[rowValue.Key.Name] = time.UnixMilli(v.Date.Content)
  903. } else {
  904. dataModel[rowValue.Key.Name] = v.String()
  905. }
  906. }
  907. }
  908. if err := tpl.Execute(buf, dataModel); nil != err {
  909. logging.LogWarnf("execute template [%s] failed: %s", tplContent, err)
  910. }
  911. return buf.String()
  912. }
  913. func getRowBlockValue(keyValues []*av.KeyValues) (ret *av.Value) {
  914. for _, kv := range keyValues {
  915. if av.KeyTypeBlock == kv.Key.Type && 0 < len(kv.Values) {
  916. ret = kv.Values[0]
  917. break
  918. }
  919. }
  920. return
  921. }