node.go 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480
  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. "strings"
  21. "sync"
  22. "github.com/88250/gulu"
  23. "github.com/88250/lute"
  24. "github.com/88250/lute/ast"
  25. "github.com/88250/lute/editor"
  26. "github.com/88250/lute/html"
  27. "github.com/88250/lute/lex"
  28. "github.com/88250/lute/parse"
  29. "github.com/88250/lute/render"
  30. "github.com/88250/vitess-sqlparser/sqlparser"
  31. "github.com/siyuan-note/logging"
  32. "github.com/siyuan-note/siyuan/kernel/util"
  33. )
  34. func GetEmbedBlockRef(embedNode *ast.Node) (blockRefID string) {
  35. if nil == embedNode || ast.NodeBlockQueryEmbed != embedNode.Type {
  36. return
  37. }
  38. scriptNode := embedNode.ChildByType(ast.NodeBlockQueryEmbedScript)
  39. if nil == scriptNode {
  40. return
  41. }
  42. stmt := scriptNode.TokensStr()
  43. parsedStmt, err := sqlparser.Parse(stmt)
  44. if nil != err {
  45. return
  46. }
  47. switch parsedStmt.(type) {
  48. case *sqlparser.Select:
  49. slct := parsedStmt.(*sqlparser.Select)
  50. if nil == slct.Where || nil == slct.Where.Expr {
  51. return
  52. }
  53. switch slct.Where.Expr.(type) {
  54. case *sqlparser.ComparisonExpr: // WHERE id = '20060102150405-1a2b3c4'
  55. comp := slct.Where.Expr.(*sqlparser.ComparisonExpr)
  56. switch comp.Left.(type) {
  57. case *sqlparser.ColName:
  58. col := comp.Left.(*sqlparser.ColName)
  59. if nil == col || "id" != col.Name.Lowered() {
  60. return
  61. }
  62. }
  63. switch comp.Right.(type) {
  64. case *sqlparser.SQLVal:
  65. val := comp.Right.(*sqlparser.SQLVal)
  66. if nil == val || sqlparser.StrVal != val.Type {
  67. return
  68. }
  69. idVal := string(val.Val)
  70. if !ast.IsNodeIDPattern(idVal) {
  71. return
  72. }
  73. blockRefID = idVal
  74. }
  75. }
  76. }
  77. return
  78. }
  79. func GetBlockRef(n *ast.Node) (blockRefID, blockRefText, blockRefSubtype string) {
  80. if !IsBlockRef(n) {
  81. return
  82. }
  83. blockRefID = n.TextMarkBlockRefID
  84. blockRefText = n.TextMarkTextContent
  85. blockRefSubtype = n.TextMarkBlockRefSubtype
  86. return
  87. }
  88. func IsBlockRef(n *ast.Node) bool {
  89. if nil == n {
  90. return false
  91. }
  92. return ast.NodeTextMark == n.Type && n.IsTextMarkType("block-ref")
  93. }
  94. func IsFileAnnotationRef(n *ast.Node) bool {
  95. if nil == n {
  96. return false
  97. }
  98. return ast.NodeTextMark == n.Type && n.IsTextMarkType("file-annotation-ref")
  99. }
  100. func IsEmbedBlockRef(n *ast.Node) bool {
  101. return "" != GetEmbedBlockRef(n)
  102. }
  103. func FormatNode(node *ast.Node, luteEngine *lute.Lute) string {
  104. markdown, err := lute.FormatNodeSync(node, luteEngine.ParseOptions, luteEngine.RenderOptions)
  105. if nil != err {
  106. root := TreeRoot(node)
  107. logging.LogFatalf(logging.ExitCodeFatal, "format node [%s] in tree [%s] failed: %s", node.ID, root.ID, err)
  108. }
  109. return markdown
  110. }
  111. func ExportNodeStdMd(node *ast.Node, luteEngine *lute.Lute) string {
  112. markdown, err := lute.ProtyleExportMdNodeSync(node, luteEngine.ParseOptions, luteEngine.RenderOptions)
  113. if nil != err {
  114. root := TreeRoot(node)
  115. logging.LogFatalf(logging.ExitCodeFatal, "export markdown for node [%s] in tree [%s] failed: %s", node.ID, root.ID, err)
  116. }
  117. return markdown
  118. }
  119. func NodeStaticContent(node *ast.Node, excludeTypes []string, includeTextMarkATitleURL, includeAssetPath bool) string {
  120. if nil == node {
  121. return ""
  122. }
  123. if ast.NodeDocument == node.Type {
  124. return node.IALAttr("title")
  125. } else if ast.NodeAttributeView == node.Type {
  126. if "" != node.AttributeViewID {
  127. attrView, err := av.ParseAttributeView(node.AttributeViewID)
  128. if nil == err {
  129. buf := bytes.Buffer{}
  130. for _, v := range attrView.Views {
  131. buf.WriteString(v.Name)
  132. buf.WriteString(" ")
  133. }
  134. return strings.TrimSpace(buf.String())
  135. }
  136. }
  137. return ""
  138. }
  139. buf := bytes.Buffer{}
  140. buf.Grow(4096)
  141. lastSpace := false
  142. ast.Walk(node, func(n *ast.Node, entering bool) ast.WalkStatus {
  143. if !entering {
  144. return ast.WalkContinue
  145. }
  146. if n.IsContainerBlock() {
  147. if !lastSpace {
  148. buf.WriteString(" ")
  149. lastSpace = true
  150. }
  151. return ast.WalkContinue
  152. }
  153. if gulu.Str.Contains(n.Type.String(), excludeTypes) {
  154. return ast.WalkContinue
  155. }
  156. switch n.Type {
  157. case ast.NodeTableCell:
  158. // 表格块写入数据库表时在单元格之间添加空格 https://github.com/siyuan-note/siyuan/issues/7654
  159. if 0 < buf.Len() && ' ' != buf.Bytes()[buf.Len()-1] {
  160. buf.WriteByte(' ')
  161. }
  162. case ast.NodeImage:
  163. linkDest := n.ChildByType(ast.NodeLinkDest)
  164. var linkDestStr, ocrText string
  165. if nil != linkDest {
  166. linkDestStr = linkDest.TokensStr()
  167. ocrText = util.GetAssetText(linkDestStr, false)
  168. }
  169. linkText := n.ChildByType(ast.NodeLinkText)
  170. if nil != linkText {
  171. buf.Write(linkText.Tokens)
  172. buf.WriteByte(' ')
  173. }
  174. if "" != ocrText {
  175. buf.WriteString(ocrText)
  176. buf.WriteByte(' ')
  177. }
  178. if nil != linkDest {
  179. if !bytes.HasPrefix(linkDest.Tokens, []byte("assets/")) || includeAssetPath {
  180. buf.Write(linkDest.Tokens)
  181. buf.WriteByte(' ')
  182. }
  183. }
  184. if linkTitle := n.ChildByType(ast.NodeLinkTitle); nil != linkTitle {
  185. buf.Write(linkTitle.Tokens)
  186. }
  187. return ast.WalkSkipChildren
  188. case ast.NodeLinkText:
  189. buf.Write(n.Tokens)
  190. buf.WriteByte(' ')
  191. case ast.NodeLinkDest:
  192. buf.Write(n.Tokens)
  193. buf.WriteByte(' ')
  194. case ast.NodeLinkTitle:
  195. buf.Write(n.Tokens)
  196. case ast.NodeText, ast.NodeCodeBlockCode, ast.NodeMathBlockContent, ast.NodeHTMLBlock:
  197. tokens := n.Tokens
  198. if IsChartCodeBlockCode(n) {
  199. // 图表块的内容在数据库 `blocks` 表 `content` 字段中被转义 https://github.com/siyuan-note/siyuan/issues/6326
  200. tokens = html.UnescapeHTML(tokens)
  201. }
  202. buf.Write(tokens)
  203. case ast.NodeTextMark:
  204. for _, excludeType := range excludeTypes {
  205. if strings.HasPrefix(excludeType, "NodeTextMark-") {
  206. if n.IsTextMarkType(excludeType[len("NodeTextMark-"):]) {
  207. return ast.WalkContinue
  208. }
  209. }
  210. }
  211. if n.IsTextMarkType("tag") {
  212. buf.WriteByte('#')
  213. }
  214. buf.WriteString(n.Content())
  215. if n.IsTextMarkType("tag") {
  216. buf.WriteByte('#')
  217. }
  218. if n.IsTextMarkType("a") && includeTextMarkATitleURL {
  219. // 搜索不到超链接元素的 URL 和标题 https://github.com/siyuan-note/siyuan/issues/7352
  220. if "" != n.TextMarkATitle {
  221. buf.WriteString(" " + html.UnescapeHTMLStr(n.TextMarkATitle))
  222. }
  223. if !strings.HasPrefix(n.TextMarkAHref, "assets/") || includeAssetPath {
  224. buf.WriteString(" " + html.UnescapeHTMLStr(n.TextMarkAHref))
  225. }
  226. }
  227. case ast.NodeBackslash:
  228. buf.WriteByte(lex.ItemBackslash)
  229. case ast.NodeBackslashContent:
  230. buf.Write(n.Tokens)
  231. }
  232. lastSpace = false
  233. return ast.WalkContinue
  234. })
  235. return strings.TrimSpace(buf.String())
  236. }
  237. func FirstLeafBlock(node *ast.Node) (ret *ast.Node) {
  238. ast.Walk(node, func(n *ast.Node, entering bool) ast.WalkStatus {
  239. if !entering || n.IsMarker() {
  240. return ast.WalkContinue
  241. }
  242. if !n.IsContainerBlock() {
  243. ret = n
  244. return ast.WalkStop
  245. }
  246. return ast.WalkContinue
  247. })
  248. return
  249. }
  250. func CountBlockNodes(node *ast.Node) (ret int) {
  251. ast.Walk(node, func(n *ast.Node, entering bool) ast.WalkStatus {
  252. if !entering || !n.IsBlock() || ast.NodeList == n.Type || ast.NodeBlockquote == n.Type || ast.NodeSuperBlock == n.Type {
  253. return ast.WalkContinue
  254. }
  255. if "1" == n.IALAttr("fold") {
  256. ret++
  257. return ast.WalkSkipChildren
  258. }
  259. ret++
  260. return ast.WalkContinue
  261. })
  262. return
  263. }
  264. func ParentNodes(node *ast.Node) (parents []*ast.Node) {
  265. const maxDepth = 256
  266. i := 0
  267. for n := node.Parent; nil != n; n = n.Parent {
  268. i++
  269. parents = append(parents, n)
  270. if ast.NodeDocument == n.Type {
  271. return
  272. }
  273. if maxDepth < i {
  274. logging.LogWarnf("parent nodes of node [%s] is too deep", node.ID)
  275. return
  276. }
  277. }
  278. return
  279. }
  280. func ChildBlockNodes(node *ast.Node) (children []*ast.Node) {
  281. children = []*ast.Node{}
  282. if !node.IsContainerBlock() || ast.NodeDocument == node.Type {
  283. children = append(children, node)
  284. return
  285. }
  286. ast.Walk(node, func(n *ast.Node, entering bool) ast.WalkStatus {
  287. if !entering || !n.IsBlock() {
  288. return ast.WalkContinue
  289. }
  290. children = append(children, n)
  291. return ast.WalkContinue
  292. })
  293. return
  294. }
  295. func ParentBlock(node *ast.Node) *ast.Node {
  296. for p := node.Parent; nil != p; p = p.Parent {
  297. if "" != p.ID && p.IsBlock() {
  298. return p
  299. }
  300. }
  301. return nil
  302. }
  303. func GetNodeInTree(tree *parse.Tree, id string) (ret *ast.Node) {
  304. ast.Walk(tree.Root, func(n *ast.Node, entering bool) ast.WalkStatus {
  305. if !entering {
  306. return ast.WalkContinue
  307. }
  308. if id == n.ID {
  309. ret = n
  310. ret.Box = tree.Box
  311. ret.Path = tree.Path
  312. return ast.WalkStop
  313. }
  314. return ast.WalkContinue
  315. })
  316. return
  317. }
  318. func GetDocTitleImgPath(root *ast.Node) (ret string) {
  319. if nil == root {
  320. return
  321. }
  322. const background = "background-image: url("
  323. titleImg := root.IALAttr("title-img")
  324. titleImg = strings.TrimSpace(titleImg)
  325. titleImg = html.UnescapeString(titleImg)
  326. titleImg = strings.ReplaceAll(titleImg, "background-image:url(", background)
  327. if !strings.Contains(titleImg, background) {
  328. return
  329. }
  330. start := strings.Index(titleImg, background) + len(background)
  331. end := strings.LastIndex(titleImg, ")")
  332. ret = titleImg[start:end]
  333. ret = strings.TrimPrefix(ret, "\"")
  334. ret = strings.TrimPrefix(ret, "'")
  335. ret = strings.TrimSuffix(ret, "\"")
  336. ret = strings.TrimSuffix(ret, "'")
  337. return ret
  338. }
  339. var typeAbbrMap = map[string]string{
  340. // 块级元素
  341. "NodeDocument": "d",
  342. "NodeHeading": "h",
  343. "NodeList": "l",
  344. "NodeListItem": "i",
  345. "NodeCodeBlock": "c",
  346. "NodeMathBlock": "m",
  347. "NodeTable": "t",
  348. "NodeBlockquote": "b",
  349. "NodeSuperBlock": "s",
  350. "NodeParagraph": "p",
  351. "NodeHTMLBlock": "html",
  352. "NodeBlockQueryEmbed": "query_embed",
  353. "NodeKramdownBlockIAL": "ial",
  354. "NodeIFrame": "iframe",
  355. "NodeWidget": "widget",
  356. "NodeThematicBreak": "tb",
  357. "NodeVideo": "video",
  358. "NodeAudio": "audio",
  359. "NodeText": "text",
  360. "NodeImage": "img",
  361. "NodeLinkText": "link_text",
  362. "NodeLinkDest": "link_dest",
  363. "NodeTextMark": "textmark",
  364. }
  365. var abbrTypeMap = map[string]string{}
  366. func init() {
  367. for typ, abbr := range typeAbbrMap {
  368. abbrTypeMap[abbr] = typ
  369. }
  370. }
  371. func TypeAbbr(nodeType string) string {
  372. return typeAbbrMap[nodeType]
  373. }
  374. func FromAbbrType(abbrType string) string {
  375. return abbrTypeMap[abbrType]
  376. }
  377. func SubTypeAbbr(n *ast.Node) string {
  378. switch n.Type {
  379. case ast.NodeList, ast.NodeListItem:
  380. if 0 == n.ListData.Typ {
  381. return "u"
  382. }
  383. if 1 == n.ListData.Typ {
  384. return "o"
  385. }
  386. if 3 == n.ListData.Typ {
  387. return "t"
  388. }
  389. case ast.NodeHeading:
  390. if 1 == n.HeadingLevel {
  391. return "h1"
  392. }
  393. if 2 == n.HeadingLevel {
  394. return "h2"
  395. }
  396. if 3 == n.HeadingLevel {
  397. return "h3"
  398. }
  399. if 4 == n.HeadingLevel {
  400. return "h4"
  401. }
  402. if 5 == n.HeadingLevel {
  403. return "h5"
  404. }
  405. if 6 == n.HeadingLevel {
  406. return "h6"
  407. }
  408. }
  409. return ""
  410. }
  411. var DynamicRefTexts = sync.Map{}
  412. func SetDynamicBlockRefText(blockRef *ast.Node, refText string) {
  413. if !IsBlockRef(blockRef) {
  414. return
  415. }
  416. blockRef.TextMarkBlockRefSubtype = "d"
  417. blockRef.TextMarkTextContent = refText
  418. // 偶发编辑文档标题后引用处的动态锚文本不更新 https://github.com/siyuan-note/siyuan/issues/5891
  419. DynamicRefTexts.Store(blockRef.TextMarkBlockRefID, refText)
  420. }
  421. func IsChartCodeBlockCode(code *ast.Node) bool {
  422. if nil == code.Previous || ast.NodeCodeBlockFenceInfoMarker != code.Previous.Type || 1 > len(code.Previous.CodeBlockInfo) {
  423. return false
  424. }
  425. language := gulu.Str.FromBytes(code.Previous.CodeBlockInfo)
  426. language = strings.ReplaceAll(language, editor.Caret, "")
  427. return render.NoHighlight(language)
  428. }