node.go 22 KB

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