@@ -86,12 +86,12 @@ func (lute *Lute) genASTByDOM(n *html.Node, tree *parse.Tree) {
86
86
return
87
87
}
88
88
89
- dataRender := lute . domAttrValue (n , "data-render" )
89
+ dataRender := util . DomAttrValue (n , "data-render" )
90
90
if "1" == dataRender {
91
91
return
92
92
}
93
93
94
- class := lute . domAttrValue (n , "class" )
94
+ class := util . DomAttrValue (n , "class" )
95
95
if strings .HasPrefix (class , "line-number" ) &&
96
96
! strings .HasPrefix (class , "line-numbers" /* 简书代码块 https://github.com/siyuan-note/siyuan/issues/4361 */ ) {
97
97
return
@@ -120,7 +120,7 @@ func (lute *Lute) genASTByDOM(n *html.Node, tree *parse.Tree) {
120
120
node .Tokens = bytes .ReplaceAll (node .Tokens , []byte ("\n " ), []byte ("" ))
121
121
}
122
122
node .Tokens = bytes .ReplaceAll (node .Tokens , []byte {194 , 160 }, []byte {' ' }) // 将 转换为空格
123
- if nil != n .Parent && atom .Span == n .Parent .DataAtom && ("" != lute . domAttrValue (n .Parent , "class" )) {
123
+ if nil != n .Parent && atom .Span == n .Parent .DataAtom && ("" != util . DomAttrValue (n .Parent , "class" )) {
124
124
if lastc := tree .Context .Tip .LastChild ; nil == lastc || (ast .NodeText == lastc .Type && ! bytes .HasSuffix (lastc .Tokens , []byte ("**" ))) {
125
125
node .Tokens = []byte ("**" + util .BytesToStr (node .Tokens ) + "**" )
126
126
}
@@ -133,7 +133,7 @@ func (lute *Lute) genASTByDOM(n *html.Node, tree *parse.Tree) {
133
133
134
134
if atom .Div == n .DataAtom {
135
135
// 解析 GitHub 语法高亮代码块
136
- class := lute . domAttrValue (n , "class" )
136
+ class := util . DomAttrValue (n , "class" )
137
137
language := ""
138
138
if strings .Contains (class , "-source-" ) {
139
139
language = class [strings .LastIndex (class , "-source-" )+ len ("-source-" ):]
@@ -147,7 +147,7 @@ func (lute *Lute) genASTByDOM(n *html.Node, tree *parse.Tree) {
147
147
node .AppendChild (& ast.Node {Type : ast .NodeCodeBlockFenceInfoMarker })
148
148
buf := & bytes.Buffer {}
149
149
node .LastChild .CodeBlockInfo = []byte (language )
150
- buf .WriteString (lute . domText (n ))
150
+ buf .WriteString (util . DomText (n ))
151
151
content := & ast.Node {Type : ast .NodeCodeBlockCode , Tokens : buf .Bytes ()}
152
152
node .AppendChild (content )
153
153
node .AppendChild (& ast.Node {Type : ast .NodeCodeBlockFenceCloseMarker , Tokens : util .StrToBytes ("```" ), CodeBlockFenceLen : 3 })
@@ -191,11 +191,11 @@ func (lute *Lute) genASTByDOM(n *html.Node, tree *parse.Tree) {
191
191
defer tree .Context .ParentTip ()
192
192
case atom .Li :
193
193
node .Type = ast .NodeListItem
194
- marker := lute . domAttrValue (n , "data-marker" )
194
+ marker := util . DomAttrValue (n , "data-marker" )
195
195
var bullet byte
196
196
if "" == marker {
197
197
if nil != n .Parent && atom .Ol == n .Parent .DataAtom {
198
- start := lute . domAttrValue (n .Parent , "start" )
198
+ start := util . DomAttrValue (n .Parent , "start" )
199
199
if "" == start {
200
200
marker = "1."
201
201
} else {
@@ -223,9 +223,9 @@ func (lute *Lute) genASTByDOM(n *html.Node, tree *parse.Tree) {
223
223
node .AppendChild (& ast.Node {Type : ast .NodeCodeBlockFenceOpenMarker , Tokens : util .StrToBytes ("```" ), CodeBlockFenceLen : 3 })
224
224
node .AppendChild (& ast.Node {Type : ast .NodeCodeBlockFenceInfoMarker })
225
225
if atom .Code == firstc .DataAtom || atom .Span == firstc .DataAtom {
226
- class := lute . domAttrValue (firstc , "class" )
226
+ class := util . DomAttrValue (firstc , "class" )
227
227
if ! strings .Contains (class , "language-" ) {
228
- class = lute . domAttrValue (n , "class" )
228
+ class = util . DomAttrValue (n , "class" )
229
229
}
230
230
if strings .Contains (class , "language-" ) {
231
231
language := class [strings .Index (class , "language-" )+ len ("language-" ):]
@@ -256,20 +256,20 @@ func (lute *Lute) genASTByDOM(n *html.Node, tree *parse.Tree) {
256
256
}
257
257
258
258
buf := & bytes.Buffer {}
259
- buf .WriteString (lute . domText (n ))
259
+ buf .WriteString (util . DomText (n ))
260
260
content := & ast.Node {Type : ast .NodeCodeBlockCode , Tokens : buf .Bytes ()}
261
261
node .AppendChild (content )
262
262
node .AppendChild (& ast.Node {Type : ast .NodeCodeBlockFenceCloseMarker , Tokens : util .StrToBytes ("```" ), CodeBlockFenceLen : 3 })
263
263
tree .Context .Tip .AppendChild (node )
264
264
} else {
265
265
node .Type = ast .NodeHTMLBlock
266
- node .Tokens = lute . domHTML (n )
266
+ node .Tokens = util . DomHTML (n )
267
267
tree .Context .Tip .AppendChild (node )
268
268
}
269
269
}
270
270
return
271
271
case atom .Em , atom .I :
272
- text := lute . domText (n )
272
+ text := util . DomText (n )
273
273
if "" == strings .TrimSpace (text ) {
274
274
break
275
275
}
@@ -281,7 +281,7 @@ func (lute *Lute) genASTByDOM(n *html.Node, tree *parse.Tree) {
281
281
tree .Context .Tip = node
282
282
defer tree .Context .ParentTip ()
283
283
case atom .Strong , atom .B :
284
- text := lute . domText (n )
284
+ text := util . DomText (n )
285
285
if "" == strings .TrimSpace (text ) {
286
286
break
287
287
}
@@ -297,7 +297,7 @@ func (lute *Lute) genASTByDOM(n *html.Node, tree *parse.Tree) {
297
297
return
298
298
}
299
299
300
- code := lute . domHTML (n )
300
+ code := util . DomHTML (n )
301
301
if bytes .Contains (code , []byte (">" )) {
302
302
code = code [bytes .Index (code , []byte (">" ))+ 1 :]
303
303
}
@@ -329,7 +329,7 @@ func (lute *Lute) genASTByDOM(n *html.Node, tree *parse.Tree) {
329
329
defer tree .Context .ParentTip ()
330
330
case atom .A :
331
331
node .Type = ast .NodeLink
332
- text := lute . domText (n )
332
+ text := util . DomText (n )
333
333
if "" == text && nil != n .Parent && (atom .H1 == n .Parent .DataAtom || atom .H2 == n .Parent .DataAtom || atom .H3 == n .Parent .DataAtom || atom .H4 == n .Parent .DataAtom || atom .H5 == n .Parent .DataAtom || atom .H6 == n .Parent .DataAtom ) {
334
334
// 丢弃标题中文本为空的链接,这样的链接可能是锚点 https://github.com/Vanessa219/vditor/issues/359
335
335
return
@@ -344,11 +344,11 @@ func (lute *Lute) genASTByDOM(n *html.Node, tree *parse.Tree) {
344
344
tree .Context .Tip = node
345
345
defer tree .Context .ParentTip ()
346
346
case atom .Img :
347
- imgClass := lute . domAttrValue (n , "class" )
348
- imgAlt := lute . domAttrValue (n , "alt" )
347
+ imgClass := util . DomAttrValue (n , "class" )
348
+ imgAlt := util . DomAttrValue (n , "alt" )
349
349
if "emoji" == imgClass {
350
350
node .Type = ast .NodeEmoji
351
- emojiImg := & ast.Node {Type : ast .NodeEmojiImg , Tokens : tree .EmojiImgTokens (imgAlt , lute . domAttrValue (n , "src" ))}
351
+ emojiImg := & ast.Node {Type : ast .NodeEmojiImg , Tokens : tree .EmojiImgTokens (imgAlt , util . DomAttrValue (n , "src" ))}
352
352
emojiImg .AppendChild (& ast.Node {Type : ast .NodeEmojiAlias , Tokens : util .StrToBytes (":" + imgAlt + ":" )})
353
353
node .AppendChild (emojiImg )
354
354
} else {
@@ -360,13 +360,13 @@ func (lute *Lute) genASTByDOM(n *html.Node, tree *parse.Tree) {
360
360
}
361
361
node .AppendChild (& ast.Node {Type : ast .NodeCloseBracket })
362
362
node .AppendChild (& ast.Node {Type : ast .NodeOpenParen })
363
- src := lute . domAttrValue (n , "src" )
363
+ src := util . DomAttrValue (n , "src" )
364
364
if strings .HasPrefix (src , "data:image" ) {
365
365
// 处理可能存在的预加载情况
366
- src = lute . domAttrValue (n , "data-src" )
366
+ src = util . DomAttrValue (n , "data-src" )
367
367
}
368
368
node .AppendChild (& ast.Node {Type : ast .NodeLinkDest , Tokens : util .StrToBytes (src )})
369
- linkTitle := lute . domAttrValue (n , "title" )
369
+ linkTitle := util . DomAttrValue (n , "title" )
370
370
if "" != linkTitle {
371
371
node .AppendChild (& ast.Node {Type : ast .NodeLinkSpace })
372
372
node .AppendChild (& ast.Node {Type : ast .NodeLinkTitle , Tokens : []byte (linkTitle )})
@@ -425,7 +425,7 @@ func (lute *Lute) genASTByDOM(n *html.Node, tree *parse.Tree) {
425
425
var tableAligns []int
426
426
if nil != n .FirstChild && nil != n .FirstChild .FirstChild && nil != n .FirstChild .FirstChild .FirstChild {
427
427
for th := n .FirstChild .FirstChild .FirstChild ; nil != th ; th = th .NextSibling {
428
- align := lute . domAttrValue (th , "align" )
428
+ align := util . DomAttrValue (th , "align" )
429
429
switch align {
430
430
case "left" :
431
431
tableAligns = append (tableAligns , 1 )
@@ -466,7 +466,7 @@ func (lute *Lute) genASTByDOM(n *html.Node, tree *parse.Tree) {
466
466
defer tree .Context .ParentTip ()
467
467
case atom .Th , atom .Td :
468
468
node .Type = ast .NodeTableCell
469
- align := lute . domAttrValue (n , "align" )
469
+ align := util . DomAttrValue (n , "align" )
470
470
var tableAlign int
471
471
switch align {
472
472
case "left" :
@@ -490,28 +490,28 @@ func (lute *Lute) genASTByDOM(n *html.Node, tree *parse.Tree) {
490
490
}
491
491
case atom .Font :
492
492
node .Type = ast .NodeText
493
- node .Tokens = []byte (lute . domText (n ))
493
+ node .Tokens = []byte (util . DomText (n ))
494
494
node .Tokens = bytes .ReplaceAll (node .Tokens , []byte ("\n " ), nil )
495
495
tree .Context .Tip .AppendChild (node )
496
496
return
497
497
case atom .Details :
498
498
node .Type = ast .NodeHTMLBlock
499
- node .Tokens = lute . domHTML (n )
499
+ node .Tokens = util . DomHTML (n )
500
500
node .Tokens = bytes .SplitAfter (node .Tokens , []byte ("</summary>" ))[0 ]
501
501
tree .Context .Tip .AppendChild (node )
502
502
case atom .Summary :
503
503
return
504
504
case atom .Iframe , atom .Audio , atom .Video :
505
505
node .Type = ast .NodeHTMLBlock
506
- node .Tokens = lute . domHTML (n )
506
+ node .Tokens = util . DomHTML (n )
507
507
tree .Context .Tip .AppendChild (node )
508
508
return
509
509
case atom .Noscript :
510
510
return
511
511
case atom .Figcaption :
512
512
node .Type = ast .NodeParagraph
513
513
node .AppendChild (& ast.Node {Type : ast .NodeHardBreak })
514
- node .AppendChild (& ast.Node {Type : ast .NodeText , Tokens : util .StrToBytes (lute . domText (n ))})
514
+ node .AppendChild (& ast.Node {Type : ast .NodeText , Tokens : util .StrToBytes (util . DomText (n ))})
515
515
tree .Context .Tip .AppendChild (node )
516
516
return
517
517
case atom .Figure :
@@ -538,8 +538,8 @@ func (lute *Lute) genASTByDOM(n *html.Node, tree *parse.Tree) {
538
538
case atom .A :
539
539
node .AppendChild (& ast.Node {Type : ast .NodeCloseBracket })
540
540
node .AppendChild (& ast.Node {Type : ast .NodeOpenParen })
541
- node .AppendChild (& ast.Node {Type : ast .NodeLinkDest , Tokens : util .StrToBytes (lute . domAttrValue (n , "href" ))})
542
- linkTitle := lute . domAttrValue (n , "title" )
541
+ node .AppendChild (& ast.Node {Type : ast .NodeLinkDest , Tokens : util .StrToBytes (util . DomAttrValue (n , "href" ))})
542
+ linkTitle := util . DomAttrValue (n , "title" )
543
543
if "" != linkTitle {
544
544
node .AppendChild (& ast.Node {Type : ast .NodeLinkSpace })
545
545
node .AppendChild (& ast.Node {Type : ast .NodeLinkTitle , Tokens : util .StrToBytes (linkTitle )})
@@ -566,15 +566,15 @@ func (lute *Lute) genASTByDOM(n *html.Node, tree *parse.Tree) {
566
566
567
567
func appendSpace (n * html.Node , tree * parse.Tree , lute * Lute ) {
568
568
if nil != n .NextSibling {
569
- if nextText := lute . domText (n .NextSibling ); "" != nextText {
569
+ if nextText := util . DomText (n .NextSibling ); "" != nextText {
570
570
if runes := []rune (nextText ); ! unicode .IsSpace (runes [0 ]) {
571
571
if unicode .IsPunct (runes [0 ]) || unicode .IsSymbol (runes [0 ]) {
572
572
tree .Context .Tip .InsertBefore (& ast.Node {Type : ast .NodeText , Tokens : []byte (" " )})
573
573
tree .Context .Tip .InsertAfter (& ast.Node {Type : ast .NodeText , Tokens : []byte (" " )})
574
574
return
575
575
}
576
576
577
- if curText := lute . domText (n ); "" != curText {
577
+ if curText := util . DomText (n ); "" != curText {
578
578
runes = []rune (curText )
579
579
if lastC := runes [len (runes )- 1 ]; unicode .IsPunct (lastC ) || unicode .IsSymbol (lastC ) {
580
580
tree .Context .Tip .InsertBefore (& ast.Node {Type : ast .NodeText , Tokens : []byte (" " )})
0 commit comments