node.go 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  1. // SiYuan - Build Your Eternal Digital Garden
  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/88250/gulu"
  20. "strings"
  21. "sync"
  22. "github.com/88250/lute"
  23. "github.com/88250/lute/ast"
  24. "github.com/88250/lute/editor"
  25. "github.com/88250/lute/html"
  26. "github.com/88250/lute/lex"
  27. "github.com/88250/lute/parse"
  28. "github.com/88250/lute/render"
  29. "github.com/88250/lute/util"
  30. "github.com/siyuan-note/logging"
  31. )
  32. func GetBlockRef(n *ast.Node) (blockRefID, blockRefText, blockRefSubtype string) {
  33. if !IsBlockRef(n) {
  34. return
  35. }
  36. blockRefID = n.TextMarkBlockRefID
  37. blockRefText = n.TextMarkTextContent
  38. blockRefSubtype = n.TextMarkBlockRefSubtype
  39. return
  40. }
  41. func IsBlockRef(n *ast.Node) bool {
  42. if nil == n {
  43. return false
  44. }
  45. return ast.NodeTextMark == n.Type && n.IsTextMarkType("block-ref")
  46. }
  47. func NodeStaticMdContent(node *ast.Node, luteEngine *lute.Lute) (md, content string) {
  48. md = ExportNodeStdMd(node, luteEngine)
  49. content = NodeStaticContent(node, nil)
  50. return
  51. }
  52. func FormatNode(node *ast.Node, luteEngine *lute.Lute) string {
  53. markdown, err := lute.FormatNodeSync(node, luteEngine.ParseOptions, luteEngine.RenderOptions)
  54. if nil != err {
  55. root := TreeRoot(node)
  56. logging.LogFatalf("format node [%s] in tree [%s] failed: %s", node.ID, root.ID, err)
  57. }
  58. return markdown
  59. }
  60. func ExportNodeStdMd(node *ast.Node, luteEngine *lute.Lute) string {
  61. markdown, err := lute.ProtyleExportMdNodeSync(node, luteEngine.ParseOptions, luteEngine.RenderOptions)
  62. if nil != err {
  63. root := TreeRoot(node)
  64. logging.LogFatalf("export markdown for node [%s] in tree [%s] failed: %s", node.ID, root.ID, err)
  65. }
  66. return markdown
  67. }
  68. func NodeStaticContent(node *ast.Node, excludeTypes []string) string {
  69. if nil == node {
  70. return ""
  71. }
  72. if ast.NodeDocument == node.Type {
  73. return node.IALAttr("title")
  74. }
  75. buf := bytes.Buffer{}
  76. buf.Grow(4096)
  77. lastSpace := false
  78. ast.Walk(node, func(n *ast.Node, entering bool) ast.WalkStatus {
  79. if !entering {
  80. return ast.WalkContinue
  81. }
  82. if n.IsContainerBlock() {
  83. if !lastSpace {
  84. buf.WriteString(" ")
  85. lastSpace = true
  86. }
  87. return ast.WalkContinue
  88. }
  89. if gulu.Str.Contains(n.Type.String(), excludeTypes) {
  90. return ast.WalkContinue
  91. }
  92. switch n.Type {
  93. case ast.NodeLinkText:
  94. buf.Write(n.Tokens)
  95. buf.WriteByte(' ')
  96. case ast.NodeLinkDest:
  97. buf.Write(n.Tokens)
  98. buf.WriteByte(' ')
  99. case ast.NodeLinkTitle:
  100. buf.Write(n.Tokens)
  101. case ast.NodeText, ast.NodeCodeBlockCode, ast.NodeMathBlockContent, ast.NodeHTMLBlock:
  102. tokens := n.Tokens
  103. if IsChartCodeBlockCode(n) {
  104. // 图表块的内容在数据库 `blocks` 表 `content` 字段中被转义 https://github.com/siyuan-note/siyuan/issues/6326
  105. tokens = html.UnescapeHTML(tokens)
  106. }
  107. buf.Write(tokens)
  108. case ast.NodeTextMark:
  109. for _, excludeType := range excludeTypes {
  110. if strings.HasPrefix(excludeType, "TextMark-") {
  111. if n.IsTextMarkType(excludeType[9:]) {
  112. return ast.WalkContinue
  113. }
  114. }
  115. }
  116. if n.IsTextMarkType("tag") {
  117. buf.WriteByte('#')
  118. }
  119. buf.WriteString(n.Content())
  120. if n.IsTextMarkType("tag") {
  121. buf.WriteByte('#')
  122. }
  123. case ast.NodeBackslash:
  124. buf.WriteByte(lex.ItemBackslash)
  125. case ast.NodeBackslashContent:
  126. buf.Write(n.Tokens)
  127. }
  128. lastSpace = false
  129. return ast.WalkContinue
  130. })
  131. return strings.TrimSpace(buf.String())
  132. }
  133. func FirstLeafBlock(node *ast.Node) (ret *ast.Node) {
  134. ast.Walk(node, func(n *ast.Node, entering bool) ast.WalkStatus {
  135. if !entering || n.IsMarker() {
  136. return ast.WalkContinue
  137. }
  138. if !n.IsContainerBlock() {
  139. ret = n
  140. return ast.WalkStop
  141. }
  142. return ast.WalkContinue
  143. })
  144. return
  145. }
  146. func CountBlockNodes(node *ast.Node) (ret int) {
  147. ast.Walk(node, func(n *ast.Node, entering bool) ast.WalkStatus {
  148. if !entering || !n.IsBlock() || ast.NodeList == n.Type || ast.NodeBlockquote == n.Type || ast.NodeSuperBlock == n.Type {
  149. return ast.WalkContinue
  150. }
  151. if "1" == n.IALAttr("fold") {
  152. ret++
  153. return ast.WalkSkipChildren
  154. }
  155. ret++
  156. return ast.WalkContinue
  157. })
  158. return
  159. }
  160. func ParentNodes(node *ast.Node) (parents []*ast.Node) {
  161. const maxDepth = 256
  162. i := 0
  163. for n := node.Parent; nil != n; n = n.Parent {
  164. i++
  165. parents = append(parents, n)
  166. if ast.NodeDocument == n.Type {
  167. return
  168. }
  169. if maxDepth < i {
  170. logging.LogWarnf("parent nodes of node [%s] is too deep", node.ID)
  171. return
  172. }
  173. }
  174. return
  175. }
  176. func ParentBlock(node *ast.Node) *ast.Node {
  177. for p := node.Parent; nil != p; p = p.Parent {
  178. if "" != p.ID && p.IsBlock() {
  179. return p
  180. }
  181. }
  182. return nil
  183. }
  184. func GetNodeInTree(tree *parse.Tree, id string) (ret *ast.Node) {
  185. ast.Walk(tree.Root, func(n *ast.Node, entering bool) ast.WalkStatus {
  186. if !entering {
  187. return ast.WalkContinue
  188. }
  189. if id == n.ID {
  190. ret = n
  191. ret.Box = tree.Box
  192. ret.Path = tree.Path
  193. return ast.WalkStop
  194. }
  195. return ast.WalkContinue
  196. })
  197. return
  198. }
  199. func GetDocTitleImgPath(root *ast.Node) (ret string) {
  200. if nil == root {
  201. return
  202. }
  203. const background = "background-image: url("
  204. titleImg := root.IALAttr("title-img")
  205. titleImg = strings.TrimSpace(titleImg)
  206. titleImg = html.UnescapeString(titleImg)
  207. titleImg = strings.ReplaceAll(titleImg, "background-image:url(", background)
  208. if !strings.Contains(titleImg, background) {
  209. return
  210. }
  211. start := strings.Index(titleImg, background) + len(background)
  212. end := strings.LastIndex(titleImg, ")")
  213. ret = titleImg[start:end]
  214. ret = strings.TrimPrefix(ret, "\"")
  215. ret = strings.TrimPrefix(ret, "'")
  216. ret = strings.TrimSuffix(ret, "\"")
  217. ret = strings.TrimSuffix(ret, "'")
  218. return ret
  219. }
  220. var typeAbbrMap = map[string]string{
  221. // 块级元素
  222. "NodeDocument": "d",
  223. "NodeHeading": "h",
  224. "NodeList": "l",
  225. "NodeListItem": "i",
  226. "NodeCodeBlock": "c",
  227. "NodeMathBlock": "m",
  228. "NodeTable": "t",
  229. "NodeBlockquote": "b",
  230. "NodeSuperBlock": "s",
  231. "NodeParagraph": "p",
  232. "NodeHTMLBlock": "html",
  233. "NodeBlockQueryEmbed": "query_embed",
  234. "NodeKramdownBlockIAL": "ial",
  235. "NodeIFrame": "iframe",
  236. "NodeWidget": "widget",
  237. "NodeThematicBreak": "tb",
  238. "NodeVideo": "video",
  239. "NodeAudio": "audio",
  240. // 行级元素 TODO: 移除旧版中的行级元素实现代码 https://github.com/siyuan-note/siyuan/issues/6819
  241. "NodeText": "text",
  242. "NodeImage": "img",
  243. "NodeLinkText": "link_text",
  244. "NodeLinkDest": "link_dest",
  245. "NodeTextMark": "textmark",
  246. }
  247. var abbrTypeMap = map[string]string{}
  248. func init() {
  249. for typ, abbr := range typeAbbrMap {
  250. abbrTypeMap[abbr] = typ
  251. }
  252. }
  253. func TypeAbbr(nodeType string) string {
  254. return typeAbbrMap[nodeType]
  255. }
  256. func FromAbbrType(abbrType string) string {
  257. return abbrTypeMap[abbrType]
  258. }
  259. func SubTypeAbbr(n *ast.Node) string {
  260. switch n.Type {
  261. case ast.NodeList, ast.NodeListItem:
  262. if 0 == n.ListData.Typ {
  263. return "u"
  264. }
  265. if 1 == n.ListData.Typ {
  266. return "o"
  267. }
  268. if 3 == n.ListData.Typ {
  269. return "t"
  270. }
  271. case ast.NodeHeading:
  272. if 1 == n.HeadingLevel {
  273. return "h1"
  274. }
  275. if 2 == n.HeadingLevel {
  276. return "h2"
  277. }
  278. if 3 == n.HeadingLevel {
  279. return "h3"
  280. }
  281. if 4 == n.HeadingLevel {
  282. return "h4"
  283. }
  284. if 5 == n.HeadingLevel {
  285. return "h5"
  286. }
  287. if 6 == n.HeadingLevel {
  288. return "h6"
  289. }
  290. }
  291. return ""
  292. }
  293. var DynamicRefTexts = sync.Map{}
  294. func SetDynamicBlockRefText(blockRef *ast.Node, refText string) {
  295. if !IsBlockRef(blockRef) {
  296. return
  297. }
  298. blockRef.TextMarkBlockRefSubtype = "d"
  299. blockRef.TextMarkTextContent = refText
  300. // 偶发编辑文档标题后引用处的动态锚文本不更新 https://github.com/siyuan-note/siyuan/issues/5891
  301. DynamicRefTexts.Store(blockRef.TextMarkBlockRefID, refText)
  302. }
  303. func IsChartCodeBlockCode(code *ast.Node) bool {
  304. if nil == code.Previous || ast.NodeCodeBlockFenceInfoMarker != code.Previous.Type || 1 > len(code.Previous.CodeBlockInfo) {
  305. return false
  306. }
  307. language := util.BytesToStr(code.Previous.CodeBlockInfo)
  308. language = strings.ReplaceAll(language, editor.Caret, "")
  309. return render.NoHighlight(language)
  310. }