node.go 29 KB

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