-
-
Notifications
You must be signed in to change notification settings - Fork 133
/
h2m.go
1448 lines (1330 loc) · 50 KB
/
h2m.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Lute - 一款结构化的 Markdown 引擎,支持 Go 和 JavaScript
// Copyright (c) 2019-present, b3log.org
//
// Lute is licensed under Mulan PSL v2.
// You can use this software according to the terms and conditions of the Mulan PSL v2.
// You may obtain a copy of Mulan PSL v2 at:
// http://license.coscl.org.cn/MulanPSL2
// THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
// See the Mulan PSL v2 for more details.
package lute
import (
"bytes"
"path"
"strings"
"unicode"
"github.com/88250/lute/ast"
"github.com/88250/lute/editor"
"github.com/88250/lute/html"
"github.com/88250/lute/html/atom"
"github.com/88250/lute/lex"
"github.com/88250/lute/parse"
"github.com/88250/lute/render"
"github.com/88250/lute/util"
)
// HTML2Markdown 将 HTML 转换为 Markdown。
func (lute *Lute) HTML2Markdown(htmlStr string) (markdown string, err error) {
//fmt.Println(htmlStr)
// 将字符串解析为 DOM 树
tree := lute.HTML2Tree(htmlStr)
// 将 AST 进行 Markdown 格式化渲染
var formatted []byte
renderer := render.NewFormatRenderer(tree, lute.RenderOptions)
for nodeType, rendererFunc := range lute.HTML2MdRendererFuncs {
renderer.ExtRendererFuncs[nodeType] = rendererFunc
}
formatted = renderer.Render()
markdown = util.BytesToStr(formatted)
return
}
// HTML2Tree 将 HTML 转换为 AST。
func (lute *Lute) HTML2Tree(dom string) (ret *parse.Tree) {
htmlRoot := lute.parseHTML(dom)
if nil == htmlRoot {
return
}
// 调整 DOM 结构
lute.adjustVditorDOM(htmlRoot)
// 将 HTML 树转换为 Markdown AST
ret = &parse.Tree{Name: "", Root: &ast.Node{Type: ast.NodeDocument}, Context: &parse.Context{ParseOption: lute.ParseOptions}}
ret.Context.Tip = ret.Root
for c := htmlRoot.FirstChild; nil != c; c = c.NextSibling {
lute.genASTByDOM(c, ret)
}
// 调整树结构
ast.Walk(ret.Root, func(n *ast.Node, entering bool) ast.WalkStatus {
if entering {
if ast.NodeList == n.Type {
// ul.ul => ul.li.ul
if nil != n.Parent && ast.NodeList == n.Parent.Type {
previousLi := n.Previous
if nil != previousLi {
n.Unlink()
previousLi.AppendChild(n)
}
}
}
}
return ast.WalkContinue
})
return
}
// genASTByDOM 根据指定的 DOM 节点 n 进行深度优先遍历并逐步生成 Markdown 语法树 tree。
func (lute *Lute) genASTByDOM(n *html.Node, tree *parse.Tree) {
if html.CommentNode == n.Type || atom.Meta == n.DataAtom {
return
}
if "svg" == n.Namespace {
return
}
dataRender := util.DomAttrValue(n, "data-render")
if "1" == dataRender {
return
}
class := util.DomAttrValue(n, "class")
if strings.HasPrefix(class, "line-number") &&
!strings.HasPrefix(class, "line-numbers" /* 简书代码块 https://github.com/siyuan-note/siyuan/issues/4361 */) {
return
}
if strings.Contains(class, "mw-editsection") {
// 忽略 Wikipedia [编辑] Do not clip the `Edit` element next to Wikipedia headings https://github.com/siyuan-note/siyuan/issues/11600
return
}
if strings.Contains(class, "citation-comment") {
// 忽略 Wikipedia 引用中的注释 https://github.com/siyuan-note/siyuan/issues/11640
return
}
if 0 == n.DataAtom && html.ElementNode == n.Type { // 自定义标签
for c := n.FirstChild; c != nil; c = c.NextSibling {
lute.genASTByDOM(c, tree)
}
return
}
node := &ast.Node{Type: ast.NodeText, Tokens: util.StrToBytes(n.Data)}
switch n.DataAtom {
case 0:
class := util.DomAttrValue(n.PrevSibling, "class")
if "fn__space5" == class {
// 链滴剪藏图片时多了长宽显示 https://github.com/siyuan-note/siyuan/issues/10987
return
}
if nil != n.Parent && atom.A == n.Parent.DataAtom {
node.Type = ast.NodeLinkText
}
// 将 \n空格空格* 转换为\n
for strings.Contains(string(node.Tokens), "\n ") {
node.Tokens = bytes.ReplaceAll(node.Tokens, []byte("\n "), []byte("\n "))
}
node.Tokens = bytes.ReplaceAll(node.Tokens, []byte("\n "), []byte("\n"))
node.Tokens = bytes.Trim(node.Tokens, "\t\n")
if lute.parentIs(n, atom.Table) {
if "\n" == n.Data {
if nil == tree.Context.Tip.FirstChild || nil == n.NextSibling {
break
}
tree.Context.Tip.AppendChild(&ast.Node{Type: ast.NodeBr})
break
} else {
if "" == strings.TrimSpace(n.Data) {
node.Tokens = []byte(" ")
tree.Context.Tip.AppendChild(node)
break
}
}
node.Tokens = bytes.TrimSpace(node.Tokens)
node.Tokens = bytes.ReplaceAll(node.Tokens, []byte("\n"), []byte(" "))
}
node.Tokens = bytes.ReplaceAll(node.Tokens, []byte{194, 160}, []byte{' '}) // 将 转换为空格
node.Tokens = bytes.ReplaceAll(node.Tokens, []byte("\n"), []byte{' '}) // 将 \n 转换为空格 https://github.com/siyuan-note/siyuan/issues/6052
if ast.NodeStrong == tree.Context.Tip.Type ||
ast.NodeEmphasis == tree.Context.Tip.Type ||
ast.NodeStrikethrough == tree.Context.Tip.Type ||
ast.NodeMark == tree.Context.Tip.Type ||
ast.NodeSup == tree.Context.Tip.Type ||
ast.NodeSub == tree.Context.Tip.Type {
if bytes.HasPrefix(node.Tokens, []byte(" ")) || 1 > len(bytes.TrimSpace(node.Tokens)) {
if nil != tree.Context.Tip.LastChild && tree.Context.Tip.LastChild.IsMarker() {
node.Tokens = append([]byte(editor.Zwsp), node.Tokens...)
}
}
if bytes.HasSuffix(node.Tokens, []byte(" ")) && nil == n.NextSibling {
node.Tokens = append(node.Tokens, []byte(editor.Zwsp)...)
}
}
if nil != n.Parent && atom.Span == n.Parent.DataAtom && 0 == len(n.Parent.Attr) {
// 按原文解析,不处理转义
} else {
if lute.ParseOptions.ProtyleWYSIWYG {
node.Tokens = lex.EscapeProtyleMarkers(node.Tokens)
} else {
node.Tokens = lex.EscapeCommonMarkers(node.Tokens)
if lute.parentIs(n, atom.Table) {
node.Tokens = bytes.ReplaceAll(node.Tokens, []byte("\\|"), []byte("|"))
node.Tokens = bytes.ReplaceAll(node.Tokens, []byte("|"), []byte("\\|"))
}
}
}
if 1 > len(node.Tokens) {
return
}
if ast.NodeListItem == tree.Context.Tip.Type {
p := &ast.Node{Type: ast.NodeParagraph}
p.AppendChild(node)
tree.Context.Tip.AppendChild(p)
tree.Context.Tip = p
} else {
tree.Context.Tip.AppendChild(node)
}
case atom.P, atom.Div, atom.Section, atom.Dd:
if ast.NodeLink == tree.Context.Tip.Type {
break
}
if lute.parentIs(n, atom.Table) {
if nil != n.PrevSibling && strings.Contains(n.PrevSibling.Data, "\n") {
break
}
if nil != n.NextSibling && strings.Contains(n.NextSibling.Data, "\n") {
break
}
if nil == tree.Context.Tip.FirstChild {
break
}
tree.Context.Tip.AppendChild(&ast.Node{Type: ast.NodeBr})
break
}
if ast.NodeHeading == tree.Context.Tip.Type {
// h 下存在 div/p/section 则忽略分块
break
}
class := util.DomAttrValue(n, "class")
if atom.Div == n.DataAtom || atom.Section == n.DataAtom {
// 解析 GitHub 语法高亮代码块
language := ""
if strings.Contains(class, "-source-") {
language = class[strings.LastIndex(class, "-source-")+len("-source-"):]
} else if strings.Contains(class, "-text-html-basic") {
language = "html"
}
if "" != language {
node.Type = ast.NodeCodeBlock
node.IsFencedCodeBlock = true
node.AppendChild(&ast.Node{Type: ast.NodeCodeBlockFenceOpenMarker, Tokens: util.StrToBytes("```"), CodeBlockFenceLen: 3})
node.AppendChild(&ast.Node{Type: ast.NodeCodeBlockFenceInfoMarker})
buf := &bytes.Buffer{}
node.LastChild.CodeBlockInfo = []byte(language)
buf.WriteString(util.DomText(n))
tokens := buf.Bytes()
tokens = bytes.ReplaceAll(tokens, []byte("\u00A0"), []byte(" "))
tokens = bytes.TrimSuffix(tokens, []byte("\n"+editor.Zwsp))
content := &ast.Node{Type: ast.NodeCodeBlockCode, Tokens: tokens}
node.AppendChild(content)
node.AppendChild(&ast.Node{Type: ast.NodeCodeBlockFenceCloseMarker, Tokens: util.StrToBytes("```"), CodeBlockFenceLen: 3})
tree.Context.Tip.AppendChild(node)
return
}
// The browser extension supports CSDN formula https://github.com/siyuan-note/siyuan/issues/5624
if strings.Contains(class, "MathJax") && nil != n.NextSibling && atom.Script == n.NextSibling.DataAtom && strings.Contains(util.DomAttrValue(n.NextSibling, "type"), "math/tex") {
tex := util.DomText(n.NextSibling)
appendMathBlock(tree, tex)
n.NextSibling.Unlink()
return
}
// The browser extension supports Wikipedia formula clipping https://github.com/siyuan-note/siyuan/issues/11583
if tex := strings.TrimSpace(util.DomAttrValue(n, "data-tex")); "" != tex {
appendMathBlock(tree, tex)
return
}
}
if strings.Contains(strings.ToLower(class), "mathjax") {
return
}
if "" == strings.TrimSpace(util.DomText(n)) {
for { // 这里用 for 是为了简化实现
if util.DomExistChildByType(n, atom.Img, atom.Picture, atom.Annotation, atom.Iframe, atom.Video, atom.Audio, atom.Source, atom.Canvas, atom.Svg, atom.Math) {
break
}
// span 可能是 TextMark 元素,也可能是公式,其他情况则忽略
spans := util.DomChildrenByType(n, atom.Span)
if 0 < len(spans) {
span := spans[0]
if "" != util.DomAttrValue(span, "data-type") || "" != util.DomAttrValue(span, "data-tex") {
break
}
}
return
}
}
node.Type = ast.NodeParagraph
tree.Context.Tip.AppendChild(node)
tree.Context.Tip = node
defer tree.Context.ParentTip()
case atom.H1, atom.H2, atom.H3, atom.H4, atom.H5, atom.H6:
if ast.NodeLink == tree.Context.Tip.Type {
break
}
node.Type = ast.NodeHeading
node.HeadingLevel = int(node.Tokens[1] - byte('0'))
node.AppendChild(&ast.Node{Type: ast.NodeHeadingC8hMarker, Tokens: util.StrToBytes(strings.Repeat("#", node.HeadingLevel))})
tree.Context.Tip.AppendChild(node)
tree.Context.Tip = node
defer tree.Context.ParentTip()
case atom.Hr:
node.Type = ast.NodeThematicBreak
tree.Context.Tip.AppendChild(node)
case atom.Blockquote:
node.Type = ast.NodeBlockquote
node.AppendChild(&ast.Node{Type: ast.NodeBlockquoteMarker, Tokens: util.StrToBytes(">")})
tree.Context.Tip.AppendChild(node)
tree.Context.Tip = node
defer tree.Context.ParentTip()
case atom.Ol, atom.Ul:
node.Type = ast.NodeList
node.ListData = &ast.ListData{}
if atom.Ol == n.DataAtom {
node.ListData.Typ = 1
}
node.ListData.Tight = true
tree.Context.Tip.AppendChild(node)
tree.Context.Tip = node
defer tree.Context.ParentTip()
case atom.Li:
node.Type = ast.NodeListItem
marker := util.DomAttrValue(n, "data-marker")
var bullet byte
if "" == marker {
if nil != n.Parent && atom.Ol == n.Parent.DataAtom {
start := util.DomAttrValue(n.Parent, "start")
if "" == start {
marker = "1."
} else {
marker = start + "."
}
} else {
marker = "*"
bullet = marker[0]
}
} else {
if nil != n.Parent && "1." != marker && atom.Ol == n.Parent.DataAtom && nil != n.Parent.Parent && (atom.Ol == n.Parent.Parent.DataAtom || atom.Ul == n.Parent.Parent.DataAtom) {
// 子有序列表必须从 1 开始
marker = "1."
}
}
node.ListData = &ast.ListData{Marker: []byte(marker), BulletChar: bullet}
tree.Context.Tip.AppendChild(node)
tree.Context.Tip = node
defer tree.Context.ParentTip()
case atom.Pre:
firstc := n.FirstChild
if nil == firstc {
return
}
if atom.Div == firstc.DataAtom && nil != firstc.NextSibling && atom.Code == firstc.NextSibling.DataAtom {
firstc = firstc.NextSibling
n.FirstChild.Unlink()
}
if atom.Em == firstc.DataAtom && nil != firstc.NextSibling && atom.Em == firstc.NextSibling.DataAtom {
// pre.em,em,code 的情况,这两个 em 是“复制代码”和“隐藏代码” https://github.com/siyuan-note/siyuan/issues/13026
code := util.DomChildrenByType(n, atom.Code)
if 0 < len(code) {
firstc = code[0]
if nil != firstc {
unlinks := []*html.Node{}
for prev := firstc.PrevSibling; nil != prev; prev = prev.PrevSibling {
unlinks = append(unlinks, prev)
}
for _, unlink := range unlinks {
unlink.Unlink()
}
}
}
}
if atom.Div == firstc.DataAtom && nil == firstc.NextSibling {
codes := util.DomChildrenByType(n, atom.Code)
if 1 == len(codes) {
code := codes[0]
// pre 下只有一个 div,且 div 下只有一个 code,那么将 pre.div 替换为 pre.code https://github.com/siyuan-note/siyuan/issues/11131
code.Unlink()
n.AppendChild(code)
firstc.Unlink()
firstc = n.FirstChild
}
}
// 改进两种 pre.ol.li 的代码块解析 https://github.com/siyuan-note/siyuan/issues/11296
// 第一种:将 pre.ol.li.p.span, span, ... span 转换为 pre.ol.li.p.code, code, ... code,然后交由第二种处理
span2Code := false
if atom.Ol == firstc.DataAtom && nil == firstc.NextSibling && nil != firstc.FirstChild && atom.Li == firstc.FirstChild.DataAtom &&
nil != firstc.FirstChild.FirstChild && atom.P == firstc.FirstChild.FirstChild.DataAtom &&
nil != firstc.FirstChild.FirstChild.FirstChild && (atom.Span == firstc.FirstChild.FirstChild.FirstChild.DataAtom || html.TextNode == firstc.FirstChild.FirstChild.FirstChild.Type) {
for li := firstc.FirstChild; nil != li; li = li.NextSibling {
code := &html.Node{Data: "code", DataAtom: atom.Code, Type: html.ElementNode}
var spans []*html.Node
if nil == li.FirstChild {
continue
}
for span := li.FirstChild.FirstChild; nil != span; span = span.NextSibling {
spans = append(spans, span)
}
for _, span := range spans {
span.Unlink()
code.AppendChild(span)
}
li.FirstChild.AppendChild(code)
span2Code = true
}
}
// 第二种:将 pre.ol.li.p.code, code, ... code 转换为 pre.code, code, ... code,然后交由后续处理
if atom.Ol == firstc.DataAtom && nil == firstc.NextSibling && nil != firstc.FirstChild && atom.Li == firstc.FirstChild.DataAtom &&
nil != firstc.FirstChild.FirstChild && atom.P == firstc.FirstChild.FirstChild.DataAtom &&
nil != firstc.FirstChild.FirstChild.FirstChild && atom.Code == firstc.FirstChild.FirstChild.FirstChild.DataAtom {
var lis, codes []*html.Node
for li := firstc.FirstChild; nil != li; li = li.NextSibling {
lis = append(lis, li)
if nil == li.FirstChild {
continue
}
codes = append(codes, li.FirstChild.FirstChild)
}
for _, li := range lis {
li.Unlink()
}
for _, code := range codes {
code.Unlink()
n.AppendChild(code)
}
firstc.Unlink()
firstc = n.FirstChild
}
if html.TextNode == firstc.Type || atom.Span == firstc.DataAtom || atom.Code == firstc.DataAtom || atom.Section == firstc.DataAtom || atom.Pre == firstc.DataAtom || atom.A == firstc.DataAtom || atom.P == firstc.DataAtom {
node.Type = ast.NodeCodeBlock
node.IsFencedCodeBlock = true
node.AppendChild(&ast.Node{Type: ast.NodeCodeBlockFenceOpenMarker, Tokens: util.StrToBytes("```"), CodeBlockFenceLen: 3})
node.AppendChild(&ast.Node{Type: ast.NodeCodeBlockFenceInfoMarker})
if atom.Code == firstc.DataAtom || atom.Span == firstc.DataAtom || atom.A == firstc.DataAtom {
class := util.DomAttrValue(firstc, "class")
if nil != n.Parent && nil != n.Parent.Parent {
parentClass := util.DomAttrValue(n.Parent.Parent, "class")
if strings.Contains(parentClass, "language-") {
class = parentClass
}
}
if !strings.Contains(class, "language-") {
class = util.DomAttrValue(n, "class")
}
if strings.Contains(class, "language-") {
language := class[strings.Index(class, "language-")+len("language-"):]
language = strings.Split(language, " ")[0]
if "fallback" != language && "chroma" != language {
node.LastChild.CodeBlockInfo = []byte(language)
}
} else {
if atom.Code == firstc.DataAtom && !span2Code {
class := util.DomAttrValue(firstc, "class")
if !strings.Contains(class, " ") {
node.LastChild.CodeBlockInfo = []byte(class)
}
}
}
if 1 > len(node.LastChild.CodeBlockInfo) {
class := util.DomAttrValue(n, "class")
if !strings.Contains(class, " ") && "fallback" != class && "chroma" != class {
node.LastChild.CodeBlockInfo = []byte(class)
}
}
if 1 > len(node.LastChild.CodeBlockInfo) {
lang := util.DomAttrValue(n, "data-language")
if !strings.Contains(lang, " ") {
node.LastChild.CodeBlockInfo = []byte(lang)
}
}
if bytes.ContainsAny(node.LastChild.CodeBlockInfo, "-_ ") {
node.LastChild.CodeBlockInfo = nil
}
}
if atom.Code == firstc.DataAtom {
if nil != firstc.NextSibling && atom.Code == firstc.NextSibling.DataAtom {
// pre.code code 每个 code 为一行的结构,需要在 code 中间插入换行
for c := firstc.NextSibling; nil != c; c = c.NextSibling {
c.InsertBefore(&html.Node{DataAtom: atom.Br})
}
}
if nil != firstc.FirstChild && atom.Ol == firstc.FirstChild.DataAtom {
// CSDN 代码块:pre.code.ol.li
for li := firstc.FirstChild.FirstChild; nil != li; li = li.NextSibling {
if li != firstc.FirstChild.FirstChild {
li.InsertBefore(&html.Node{DataAtom: atom.Br})
}
}
}
if nil != n.LastChild && atom.Ul == n.LastChild.DataAtom {
// CSDN 代码块:pre.code,ul
n.LastChild.Unlink() // 去掉最后一个代码行号子块 https://github.com/siyuan-note/siyuan/issues/5564
}
}
if atom.Pre == firstc.DataAtom && nil != firstc.FirstChild {
// pre.code code 每个 code 为一行的结构,需要在 code 中间插入换行
for c := firstc.FirstChild.NextSibling; nil != c; c = c.NextSibling {
c.InsertBefore(&html.Node{DataAtom: atom.Br})
}
}
if atom.P == firstc.DataAtom {
// 避免下面 util.DomText 把 p 转换为两个换行
firstc.DataAtom = atom.Div
}
buf := &bytes.Buffer{}
buf.WriteString(util.DomText(n))
tokens := buf.Bytes()
tokens = bytes.ReplaceAll(tokens, []byte("\u00A0"), []byte(" "))
tokens = bytes.TrimSuffix(tokens, []byte("\n"+editor.Zwsp))
content := &ast.Node{Type: ast.NodeCodeBlockCode, Tokens: tokens}
node.AppendChild(content)
node.AppendChild(&ast.Node{Type: ast.NodeCodeBlockFenceCloseMarker, Tokens: util.StrToBytes("```"), CodeBlockFenceLen: 3})
if tree.Context.Tip.ParentIs(ast.NodeTable) {
// 如果表格中只有一行一列,那么丢弃表格直接使用代码块
// Improve HTML parsing code blocks https://github.com/siyuan-note/siyuan/issues/11068
for table := tree.Context.Tip.Parent; nil != table; table = table.Parent {
if ast.NodeTable == table.Type {
if nil != table.FirstChild && table.FirstChild == table.LastChild && ast.NodeTableHead == table.FirstChild.Type &&
table.FirstChild.FirstChild == table.FirstChild.LastChild &&
nil != table.FirstChild.FirstChild.FirstChild && ast.NodeTableCell == table.FirstChild.FirstChild.FirstChild.Type {
table.InsertBefore(node)
table.Unlink()
tree.Context.Tip = node
parent := n.Parent
for i := 0; i < 32; i++ {
if nil == parent {
break
}
class := util.DomAttrValue(parent, "class")
if strings.Contains(class, "language-") {
node.ChildByType(ast.NodeCodeBlockFenceInfoMarker).CodeBlockInfo = []byte(class[strings.Index(class, "language-")+len("language-"):])
break
} else if strings.Contains(class, "highlight ") {
node.ChildByType(ast.NodeCodeBlockFenceInfoMarker).CodeBlockInfo = []byte(class[strings.Index(class, "highlight ")+len("highlight "):])
break
}
parent = parent.Parent
}
return
}
}
}
// 表格中不支持添加块级元素,所以这里只能将其转换为多个行级代码元素
lines := bytes.Split(content.Tokens, []byte("\n"))
for i, line := range lines {
if 0 < len(line) {
code := &ast.Node{Type: ast.NodeCodeSpan}
if bytes.Contains(line, []byte("`")) {
node.CodeMarkerLen = 2
}
code.AppendChild(&ast.Node{Type: ast.NodeCodeSpanOpenMarker, Tokens: []byte("`")})
code.AppendChild(&ast.Node{Type: ast.NodeCodeSpanContent, Tokens: line})
code.AppendChild(&ast.Node{Type: ast.NodeCodeSpanCloseMarker, Tokens: []byte("`")})
tree.Context.Tip.AppendChild(code)
if i < len(lines)-1 {
if tree.Context.ParseOption.ProtyleWYSIWYG {
tree.Context.Tip.AppendChild(&ast.Node{Type: ast.NodeBr})
} else {
tree.Context.Tip.AppendChild(&ast.Node{Type: ast.NodeHardBreak, Tokens: []byte("\n")})
}
}
}
}
} else {
tree.Context.Tip.AppendChild(node)
}
} else {
node.Type = ast.NodeHTMLBlock
node.Tokens = util.DomHTML(n)
tree.Context.Tip.AppendChild(node)
}
return
case atom.Em, atom.I:
text := util.DomText(n)
if "" == strings.TrimSpace(text) {
break
}
if ast.NodeEmphasis == tree.Context.Tip.Type || tree.Context.Tip.ParentIs(ast.NodeEmphasis) {
break
}
if nil != tree.Context.Tip.LastChild && (ast.NodeStrong == tree.Context.Tip.LastChild.Type || ast.NodeEmphasis == tree.Context.Tip.LastChild.Type) {
// 在两个相邻的加粗或者斜体之间插入零宽空格,避免标记符重复
tree.Context.Tip.AppendChild(&ast.Node{Type: ast.NodeText, Tokens: util.StrToBytes(editor.Zwsp)})
}
if !lute.ParseOptions.InlineAsterisk || !lute.ParseOptions.InlineUnderscore {
node.Type = ast.NodeHTMLTag
node.AppendChild(&ast.Node{Type: ast.NodeHTMLTagOpen, Tokens: util.StrToBytes("<em>")})
} else {
node.Type = ast.NodeEmphasis
node.AppendChild(&ast.Node{Type: ast.NodeEmA6kOpenMarker, Tokens: util.StrToBytes("*")})
}
tree.Context.Tip.AppendChild(node)
tree.Context.Tip = node
defer tree.Context.ParentTip()
case atom.Strong, atom.B:
text := util.DomText(n)
if "" == strings.TrimSpace(text) {
break
}
if ast.NodeStrong == tree.Context.Tip.Type || tree.Context.Tip.ParentIs(ast.NodeStrong) {
break
}
if nil != tree.Context.Tip.LastChild && (ast.NodeStrong == tree.Context.Tip.LastChild.Type || ast.NodeEmphasis == tree.Context.Tip.LastChild.Type) {
tree.Context.Tip.AppendChild(&ast.Node{Type: ast.NodeText, Tokens: util.StrToBytes(editor.Zwsp)})
}
if !lute.ParseOptions.InlineAsterisk || !lute.ParseOptions.InlineUnderscore {
node.Type = ast.NodeHTMLTag
node.AppendChild(&ast.Node{Type: ast.NodeHTMLTagOpen, Tokens: util.StrToBytes("<strong>")})
} else {
node.Type = ast.NodeStrong
node.AppendChild(&ast.Node{Type: ast.NodeStrongA6kOpenMarker, Tokens: util.StrToBytes("**")})
}
tree.Context.Tip.AppendChild(node)
tree.Context.Tip = node
defer tree.Context.ParentTip()
case atom.Code:
if nil == n.FirstChild {
return
}
if nil != tree.Context.Tip.LastChild && ast.NodeCodeSpan == tree.Context.Tip.LastChild.Type {
tree.Context.Tip.AppendChild(&ast.Node{Type: ast.NodeText, Tokens: util.StrToBytes(editor.Zwsp)})
}
code := util.DomHTML(n)
if bytes.Contains(code, []byte(">")) {
code = code[bytes.Index(code, []byte(">"))+1:]
}
code = bytes.TrimSuffix(code, []byte("</code>"))
allSpan := true
for c := n.FirstChild; nil != c; c = c.NextSibling {
if html.TextNode == c.Type {
continue
}
if atom.Em == c.DataAtom || atom.Strong == c.DataAtom {
// https://github.com/siyuan-note/siyuan/issues/11682
continue
}
if atom.Span != c.DataAtom {
allSpan = false
break
}
}
if allSpan {
// 如果全部都是 span 子节点,那么直接使用 span 的内容 https://github.com/siyuan-note/siyuan/issues/11281
code = []byte(util.DomText(n))
code = bytes.ReplaceAll(code, []byte("\u00A0"), []byte(" "))
}
content := &ast.Node{Type: ast.NodeCodeSpanContent, Tokens: code}
node.Type = ast.NodeCodeSpan
if bytes.Contains(code, []byte("```")) {
node.CodeMarkerLen = 2
} else if bytes.Contains(code, []byte("``")) {
node.CodeMarkerLen = 1
} else if bytes.Contains(code, []byte("`")) {
node.CodeMarkerLen = 2
}
node.AppendChild(&ast.Node{Type: ast.NodeCodeSpanOpenMarker, Tokens: []byte("`")})
if bytes.Contains(code, []byte("``")) {
content.Tokens = append([]byte(" "), content.Tokens...)
}
node.AppendChild(content)
if bytes.Contains(code, []byte("``")) {
content.Tokens = append(content.Tokens, ' ')
}
node.AppendChild(&ast.Node{Type: ast.NodeCodeSpanCloseMarker, Tokens: []byte("`")})
tree.Context.Tip.AppendChild(node)
tree.Context.Tip = node
defer tree.Context.ParentTip()
return
case atom.Br:
if ast.NodeLink == tree.Context.Tip.Type || ast.NodeHeading == tree.Context.Tip.Type {
break
}
if nil == n.NextSibling {
break
}
if tree.Context.ParseOption.ProtyleWYSIWYG && lute.parentIs(n, atom.Table) {
node.Type = ast.NodeBr
} else {
node.Type = ast.NodeHardBreak
node.Tokens = util.StrToBytes("\n")
}
tree.Context.Tip.AppendChild(node)
tree.Context.Tip = node
defer tree.Context.ParentTip()
case atom.A:
node.Type = ast.NodeLink
text := strings.TrimSpace(util.DomText(n))
if "" == text && nil != n.Parent && lute.parentIs(n, atom.H1, atom.H2, atom.H3, atom.H4, atom.H5, atom.H6, atom.Div, atom.Section) && nil == util.DomChildrenByType(n, atom.Img) {
// 丢弃标题中文本为空的链接,这样的链接是没有锚文本的锚点
// https://github.com/Vanessa219/vditor/issues/359
// https://github.com/siyuan-note/siyuan/issues/11445
return
}
if "" == text && nil == n.FirstChild {
// 剪藏时过滤空的超链接 https://github.com/siyuan-note/siyuan/issues/5686
return
}
if nil != n.FirstChild && atom.Img == n.FirstChild.DataAtom && strings.Contains(util.DomAttrValue(n.FirstChild, "src"), "wikimedia.org") {
// Wikipedia 链接嵌套图片的情况只保留图片
break
}
node.AppendChild(&ast.Node{Type: ast.NodeOpenBracket})
tree.Context.Tip.AppendChild(node)
tree.Context.Tip = node
defer tree.Context.ParentTip()
case atom.Img:
imgClass := util.DomAttrValue(n, "class")
imgAlt := util.DomAttrValue(n, "alt")
if "emoji" == imgClass {
node.Type = ast.NodeEmoji
emojiImg := &ast.Node{Type: ast.NodeEmojiImg, Tokens: tree.EmojiImgTokens(imgAlt, util.DomAttrValue(n, "src"))}
emojiImg.AppendChild(&ast.Node{Type: ast.NodeEmojiAlias, Tokens: util.StrToBytes(":" + imgAlt + ":")})
node.AppendChild(emojiImg)
} else {
node.Type = ast.NodeImage
node.AppendChild(&ast.Node{Type: ast.NodeBang})
node.AppendChild(&ast.Node{Type: ast.NodeOpenBracket})
if "" != imgAlt {
imgAlt = strings.TrimSpace(imgAlt)
imgAlt = strings.ReplaceAll(imgAlt, "动图封面", "动图")
node.AppendChild(&ast.Node{Type: ast.NodeLinkText, Tokens: util.StrToBytes(imgAlt)})
}
node.AppendChild(&ast.Node{Type: ast.NodeCloseBracket})
node.AppendChild(&ast.Node{Type: ast.NodeOpenParen})
src := util.DomAttrValue(n, "src")
if strings.Contains(class, "ztext-gif") && strings.Contains(src, "zhimg.com") {
// 处理知乎动图
src = strings.Replace(src, ".jpg", ".webp", 1)
}
if strings.HasPrefix(src, "data:image") {
// 处理可能存在的预加载情况
if dataSrc := util.DomAttrValue(n, "data-src"); "" != dataSrc {
src = dataSrc
}
}
// 处理使用 data-original 属性的情况 https://github.com/siyuan-note/siyuan/issues/11826
dataOriginal := util.DomAttrValue(n, "data-original")
if "" != dataOriginal {
if "" == src || !strings.HasSuffix(src, ".gif") {
src = dataOriginal
}
}
if "" == src {
// 处理使用 srcset 属性的情况
if srcset := util.DomAttrValue(n, "srcset"); "" != srcset {
if strings.Contains(srcset, ",") {
src = strings.Split(srcset, ",")[len(strings.Split(srcset, ","))-1]
src = strings.TrimSpace(src)
if strings.Contains(src, " ") {
src = strings.TrimSpace(strings.Split(src, " ")[0])
}
} else {
src = strings.TrimSpace(src)
if strings.Contains(src, " ") {
src = strings.TrimSpace(strings.Split(srcset, " ")[0])
}
}
}
}
// Wikipedia 使用图片原图 https://github.com/siyuan-note/siyuan/issues/11640
if strings.Contains(src, "wikipedia/commons/thumb/") {
ext := path.Ext(src)
if strings.Contains(src, ".svg.png") {
ext = ".svg"
}
if idx := strings.Index(src, ext+"/"); 0 < idx {
src = src[:idx+len(ext)]
src = strings.ReplaceAll(src, "/commons/thumb/", "/commons/")
}
}
node.AppendChild(&ast.Node{Type: ast.NodeLinkDest, Tokens: util.StrToBytes(src)})
var linkTitle string
if nil != n.NextSibling && atom.Figcaption == n.NextSibling.DataAtom {
linkTitle = util.DomText(n.NextSibling)
n.NextSibling.Unlink()
}
if "" == linkTitle {
linkTitle = util.DomAttrValue(n, "title")
}
if "" != linkTitle {
node.AppendChild(&ast.Node{Type: ast.NodeLinkSpace})
node.AppendChild(&ast.Node{Type: ast.NodeLinkTitle, Tokens: []byte(linkTitle)})
}
node.AppendChild(&ast.Node{Type: ast.NodeCloseParen})
}
if ast.NodeDocument == tree.Context.Tip.Type {
p := &ast.Node{Type: ast.NodeParagraph}
tree.Context.Tip.AppendChild(p)
tree.Context.Tip = p
defer tree.Context.ParentTip()
}
tree.Context.Tip.AppendChild(node)
tree.Context.Tip = node
defer tree.Context.ParentTip()
case atom.Input:
node.Type = ast.NodeTaskListItemMarker
node.TaskListItemChecked = lute.hasAttr(n, "checked")
tree.Context.Tip.AppendChild(node)
if nil != node.Parent.Parent {
if nil == node.Parent.Parent.ListData {
node.Parent.Parent.ListData = &ast.ListData{Typ: 3}
} else {
node.Parent.Parent.ListData.Typ = 3
}
}
case atom.Del, atom.S, atom.Strike:
if !lute.ParseOptions.GFMStrikethrough {
node.Type = ast.NodeHTMLTag
node.AppendChild(&ast.Node{Type: ast.NodeHTMLTagOpen, Tokens: util.StrToBytes("<s>")})
} else {
node.Type = ast.NodeStrikethrough
node.AppendChild(&ast.Node{Type: ast.NodeStrikethrough2OpenMarker, Tokens: util.StrToBytes("~~")})
}
tree.Context.Tip.AppendChild(node)
tree.Context.Tip = node
defer tree.Context.ParentTip()
case atom.U:
node.Type = ast.NodeHTMLTag
node.AppendChild(&ast.Node{Type: ast.NodeHTMLTagOpen, Tokens: util.StrToBytes("<u>")})
tree.Context.Tip.AppendChild(node)
tree.Context.Tip = node
defer tree.Context.ParentTip()
case atom.Mark:
if !lute.ParseOptions.Mark {
node.Type = ast.NodeHTMLTag
node.AppendChild(&ast.Node{Type: ast.NodeHTMLTagOpen, Tokens: util.StrToBytes("<mark>")})
} else {
node.Type = ast.NodeMark
node.AppendChild(&ast.Node{Type: ast.NodeMark2OpenMarker, Tokens: util.StrToBytes("==")})
}
tree.Context.Tip.AppendChild(node)
tree.Context.Tip = node
defer tree.Context.ParentTip()
case atom.Sup:
if !lute.ParseOptions.Sup {
node.Type = ast.NodeHTMLTag
node.AppendChild(&ast.Node{Type: ast.NodeHTMLTagOpen, Tokens: util.StrToBytes("<sup>")})
} else {
node.Type = ast.NodeSup
node.AppendChild(&ast.Node{Type: ast.NodeSupOpenMarker})
}
tree.Context.Tip.AppendChild(node)
tree.Context.Tip = node
defer tree.Context.ParentTip()
case atom.Sub:
if !lute.ParseOptions.Sub {
node.Type = ast.NodeHTMLTag
node.AppendChild(&ast.Node{Type: ast.NodeHTMLTagOpen, Tokens: util.StrToBytes("<sub>")})
} else {
node.Type = ast.NodeSub
node.AppendChild(&ast.Node{Type: ast.NodeSubOpenMarker})
}
tree.Context.Tip.AppendChild(node)
tree.Context.Tip = node
defer tree.Context.ParentTip()
case atom.Table:
node.Type = ast.NodeTable
var tableAligns []int
if nil != n.FirstChild && nil != n.FirstChild.FirstChild && nil != n.FirstChild.FirstChild.FirstChild {
for th := n.FirstChild.FirstChild.FirstChild; nil != th; th = th.NextSibling {
align := util.DomAttrValue(th, "align")
switch align {
case "left":
tableAligns = append(tableAligns, 1)
case "center":
tableAligns = append(tableAligns, 2)
case "right":
tableAligns = append(tableAligns, 3)
default:
tableAligns = append(tableAligns, 0)
}
}
}
node.TableAligns = tableAligns
tree.Context.Tip.AppendChild(node)
tree.Context.Tip = node
defer tree.Context.ParentTip()
case atom.Thead:
if nil == n.FirstChild {
break
}
tbodys := util.DomChildrenByType(n.Parent, atom.Tbody)
if 0 < len(tbodys) {
tbody := tbodys[0]
// 找到最多的 td 数
var tdCount int
for tr := tbody.FirstChild; nil != tr; tr = tr.NextSibling {
if atom.Tr != tr.DataAtom {
continue
}
var count int
for td := tr.FirstChild; nil != td; td = td.NextSibling {
if atom.Td == td.DataAtom {
count++
}
}
if count > tdCount {
tdCount = count
}
}
// 补全 thead 中 tr 的 th
for tr := n.FirstChild; nil != tr; tr = tr.NextSibling {
if atom.Tr != tr.DataAtom {
continue
}
var count int
for td := tr.FirstChild; nil != td; td = td.NextSibling {
if atom.Th == td.DataAtom {
count++
}
}
if count < tdCount {
for i := count; i < tdCount; i++ {
th := &html.Node{Data: "th", DataAtom: atom.Th, Type: html.ElementNode}
tr.AppendChild(th)
}
}
}
}
node.Type = ast.NodeTableHead
tree.Context.Tip.AppendChild(node)
tree.Context.Tip = node
defer tree.Context.ParentTip()
case atom.Tbody:
case atom.Tr:
if nil == n.FirstChild {
break
}
table := n.Parent.Parent
node.Type = ast.NodeTableRow
if nil == tree.Context.Tip.ChildByType(ast.NodeTableHead) && 1 > len(util.DomChildrenByType(table, atom.Thead)) {
// 补全 thread 节点
thead := &ast.Node{Type: ast.NodeTableHead}
tree.Context.Tip.AppendChild(thead)
tree.Context.Tip = thead
defer tree.Context.ParentTip()
}
tree.Context.Tip.AppendChild(node)
tree.Context.Tip = node
defer tree.Context.ParentTip()
case atom.Th, atom.Td:
node.Type = ast.NodeTableCell