Repository navigation
Expand file tree
/
Copy pathinline.go
More file actions
641 lines (528 loc) · 15.2 KB
/
Copy pathinline.go
File metadata and controls
641 lines (528 loc) · 15.2 KB
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
package ast
import (
"fmt"
"strings"
"github.com/yuin/goldmark/v2/text"
"github.com/yuin/goldmark/v2/util"
)
// A BaseInline struct implements the Node interface partially.
type BaseInline struct {
BaseNode
}
func (b *BaseInline) inlineNode() {}
type fourByteWriter struct {
buf [4]byte
written int
}
func (w *fourByteWriter) Write(p []byte) (n int, err error) {
written := 0
for _, b := range p {
if w.written >= 4 {
return written, fmt.Errorf("buffer overflow")
}
w.buf[w.written] = b
w.written++
written++
}
return written, nil
}
// FirstRune implements [InlineNode].FirstRune.
func (b *BaseInline) FirstRune(source []byte) (rune, bool) {
for cur := b.self; cur != nil; cur = cur.FirstChild() {
if t, ok := cur.(*Text); ok {
var fbw fourByteWriter
_, _ = t.Value.WriteTo(&fbw, source)
if fbw.written == 0 {
return 0, false
}
return util.ToRune(fbw.buf[:], 0), true
}
}
return 0, false
}
// LastRune implements [InlineNode].LastRune.
func (b *BaseInline) LastRune(source []byte) (rune, bool) {
for cur := b.self; cur != nil; cur = cur.LastChild() {
if t, ok := cur.(*Text); ok {
v := t.Value.Value(source)
if len(v) == 0 {
return 0, false
}
return util.ToRune(util.StringToReadOnlyBytes(v), len(v)-1), true
}
}
return 0, false
}
var _ InlineNode = (*Text)(nil)
// A Text struct represents a textual content of the Markdown text.
type Text struct {
node
// Value is the text value. It is either a source position or an owned string.
Value text.SingleLineValue
flags uint8
}
const (
textSoftLineBreak = 1 << iota
textHardLineBreak
)
func textFlagsString(flags uint8) string {
buf := []string{}
if flags&textSoftLineBreak != 0 {
buf = append(buf, "SoftLineBreak")
}
if flags&textHardLineBreak != 0 {
buf = append(buf, "HardLineBreak")
}
return strings.Join(buf, ", ")
}
func (n *Text) inlineNode() {}
// Pos implements Node.Pos.
func (n *Text) Pos() int {
if n.Value.IsOwned() {
return -1
}
return n.Value.Index().Start
}
// SetPos implements Node.SetPos.
func (n *Text) SetPos(_ int) {}
// FirstRune implements [InlineNode].FirstRune.
func (n *Text) FirstRune(source []byte) (rune, bool) {
var fbw fourByteWriter
_, _ = n.Value.WriteTo(&fbw, source)
if fbw.written == 0 {
return 0, false
}
return util.ToRune(fbw.buf[:], 0), true
}
// LastRune implements [InlineNode].LastRune.
func (n *Text) LastRune(source []byte) (rune, bool) {
v := n.Value.Value(source)
if len(v) == 0 {
return 0, false
}
return util.ToRune(util.StringToReadOnlyBytes(v), len(v)-1), true
}
// SoftLineBreak returns true if this node ends with a new line,
// otherwise false.
func (n *Text) SoftLineBreak() bool {
return n.flags&textSoftLineBreak != 0
}
// SetSoftLineBreak sets whether this node ends with a new line.
func (n *Text) SetSoftLineBreak(v bool) {
if v {
n.flags |= textSoftLineBreak
} else {
n.flags = n.flags &^ textSoftLineBreak
}
}
// HardLineBreak returns true if this node ends with a hard line break.
// See https://spec.commonmark.org/0.30/#hard-line-breaks for details.
func (n *Text) HardLineBreak() bool {
return n.flags&textHardLineBreak != 0
}
// SetHardLineBreak sets whether this node ends with a hard line break.
func (n *Text) SetHardLineBreak(v bool) {
if v {
n.flags |= textHardLineBreak
} else {
n.flags = n.flags &^ textHardLineBreak
}
}
// Dump implements Node.Dump.
func (n *Text) Dump(_ []byte) *NodeDump {
m := map[string]any{
"Value": n.Value,
}
fs := textFlagsString(n.flags)
if len(fs) != 0 {
m["Flags"] = fs
}
return NewNodeDump(n, m)
}
// KindText is a NodeKind of the Text node.
var KindText = NewNodeKind("Text")
// Kind implements Node.Kind.
func (n *Text) Kind() NodeKind {
return KindText
}
// NewText returns a new Text node.
func NewText(value text.SingleLineValue) *Text {
n := &Text{
Value: value,
}
return n
}
var _ InlineNode = (*CodeSpan)(nil)
// A CodeSpan struct represents a code span of Markdown text.
type CodeSpan struct {
node
// Value holds the content of this code span.
// The content is sourced from the raw Markdown text and may span multiple
// source lines.
//
// This value is expected to be a single line; CommonMark requires that code span content is
// normalized to a single line by replacing newlines with spaces.
//
// Code spans that span multiple lines and return multiple indices from [text.Value].Indices()
// should return normalized single-line data from the Bytes, Str, and Value methods of [text.Value].
Value text.Value
}
func (n *CodeSpan) inlineNode() {}
// IsBlank returns true if this node consists of spaces, otherwise false.
func (n *CodeSpan) IsBlank(source []byte) bool {
return util.IsBlank(n.Value.Bytes(source))
}
// Pos implements Node.Pos.
func (n *CodeSpan) Pos() int {
if n.Value.IsOwned() {
return -1
}
return n.Value.Index().Start
}
// SetPos implements Node.SetPos.
func (n *CodeSpan) SetPos(_ int) {}
// Dump implements Node.Dump.
func (n *CodeSpan) Dump(_ []byte) *NodeDump {
return NewNodeDump(n, map[string]any{
"Value": n.Value,
})
}
// FirstRune implements [InlineNode].FirstRune.
func (n *CodeSpan) FirstRune(source []byte) (rune, bool) {
var fbw fourByteWriter
_, _ = n.Value.WriteTo(&fbw, source)
if fbw.written == 0 {
return 0, false
}
return util.ToRune(fbw.buf[:], 0), true
}
// LastRune implements [InlineNode].LastRune.
func (n *CodeSpan) LastRune(source []byte) (rune, bool) {
v := n.Value.Value(source)
if len(v) == 0 {
return 0, false
}
return util.ToRune(util.StringToReadOnlyBytes(v), len(v)-1), true
}
// KindCodeSpan is a NodeKind of the CodeSpan node.
var KindCodeSpan = NewNodeKind("CodeSpan")
// Kind implements Node.Kind.
func (n *CodeSpan) Kind() NodeKind {
return KindCodeSpan
}
// NewCodeSpan returns a new CodeSpan node with the given value.
//
// Given value is expected to be a single line; CommonMark requires that code span content is
// normalized to a single line by replacing newlines with spaces.
//
// You should use CodeBlock for code that you really want to treat as multiple lines.
//
// Code spans that span multiple lines and return multiple indices from [text.Value].Indices()
// should return normalized single-line data from the Bytes, Str, and Value methods of [text.Value].
func NewCodeSpan(value text.Value) *CodeSpan {
n := &CodeSpan{Value: value}
return n
}
var _ InlineNode = (*Emphasis)(nil)
// An Emphasis struct represents an emphasis of Markdown text (e.g. *text* or _text_).
type Emphasis struct {
BaseInline
}
// Dump implements Node.Dump.
func (n *Emphasis) Dump(_ []byte) *NodeDump {
return NewNodeDump(n, nil)
}
// KindEmphasis is a NodeKind of the Emphasis node.
var KindEmphasis = NewNodeKind("Emphasis")
// Kind implements Node.Kind.
func (n *Emphasis) Kind() NodeKind {
return KindEmphasis
}
// NewEmphasis returns a new Emphasis node.
func NewEmphasis() *Emphasis {
n := &Emphasis{}
n.Init(n)
return n
}
var _ InlineNode = (*Strong)(nil)
// A Strong struct represents strong importance of Markdown text (e.g. **text** or __text__).
type Strong struct {
BaseInline
}
// Dump implements Node.Dump.
func (n *Strong) Dump(_ []byte) *NodeDump {
return NewNodeDump(n, nil)
}
// KindStrong is a NodeKind of the Strong node.
var KindStrong = NewNodeKind("Strong")
// Kind implements Node.Kind.
func (n *Strong) Kind() NodeKind {
return KindStrong
}
// NewStrong returns a new Strong node.
func NewStrong() *Strong {
n := &Strong{}
n.Init(n)
return n
}
// LinkOption is an option for Link and Image nodes.
type LinkOption interface {
setLinkOption(*Link)
}
// ImageOption is an option for Image nodes.
type ImageOption interface {
setImageOption(*Image)
}
type linkTitle struct {
value text.MultiLineValue
}
func (o *linkTitle) setLinkOption(n *Link) {
n.Title = o.value
}
func (o *linkTitle) setImageOption(n *Image) {
n.Title = o.value
}
// WithLinkTitle returns a LinkOption that sets the title of a link or image.
func WithLinkTitle(title text.MultiLineValue) interface {
LinkOption
ImageOption
LinkReferenceDefinitionOption
} {
return &linkTitle{value: title}
}
type linkReference struct {
kind ReferenceLinkKind
value text.MultiLineValue
}
func (o *linkReference) setLinkOption(n *Link) {
n.Reference = &ReferenceLink{ReferenceLinkKind: o.kind, Value: o.value}
}
func (o *linkReference) setImageOption(n *Image) {
n.Reference = &ReferenceLink{ReferenceLinkKind: o.kind, Value: o.value}
}
// WithLinkReference returns a LinkOption that sets the reference of a link or image.
func WithLinkReference(kind ReferenceLinkKind, value text.MultiLineValue) interface {
LinkOption
ImageOption
} {
return &linkReference{kind: kind, value: value}
}
type autoLinkText struct {
value text.SingleLineValue
}
func (o *autoLinkText) setAutoLinkOption(n *AutoLink) {
n.Text = o.value
}
// WithAutoLinkText returns an AutoLinkOption that sets the original source text of an autolink.
func WithAutoLinkText(text text.SingleLineValue) AutoLinkOption {
return &autoLinkText{value: text}
}
// ReferenceLinkKind represents the kind of a reference link.
type ReferenceLinkKind int
const (
// ReferenceLinkKindFull indicates that a reference link has a full reference like [foo][bar].
ReferenceLinkKindFull ReferenceLinkKind = iota + 1
// ReferenceLinkKindCollapsed indicates that a reference link has a collapsed reference like [foo][].
ReferenceLinkKindCollapsed
// ReferenceLinkKindShortcut indicates that a reference link has a shortcut reference like [foo].
ReferenceLinkKindShortcut
)
// String returns a string representation of this reference link kind.
func (t ReferenceLinkKind) String() string {
switch t {
case ReferenceLinkKindFull:
return "Full"
case ReferenceLinkKindCollapsed:
return "Collapsed"
case ReferenceLinkKindShortcut:
return "Shortcut"
default:
return fmt.Sprintf("Unknown(%d)", t)
}
}
// ReferenceLink struct represents a reference link of the Markdown text.
type ReferenceLink struct {
// ReferenceLinkKind is the kind of this reference link.
ReferenceLinkKind ReferenceLinkKind
// Value is a value of this reference link.
Value text.MultiLineValue
}
// NewReferenceLink returns a new ReferenceLink with the given kind and value.
func NewReferenceLink(kind ReferenceLinkKind, value text.MultiLineValue) *ReferenceLink {
return &ReferenceLink{
ReferenceLinkKind: kind,
Value: value,
}
}
var _ InlineNode = (*Link)(nil)
// A Link struct represents a link of the Markdown text.
type Link struct {
BaseInline
// Destination is a destination(URL) of this link.
Destination text.SingleLineValue
// Title is a title of this link.
Title text.MultiLineValue
// Reference is a reference of this link. This field is used for reference links.
// If this link is not a reference link, this field is nil.
Reference *ReferenceLink
}
// Dump implements Node.Dump.
func (n *Link) Dump(_ []byte) *NodeDump {
m := map[string]any{
"Destination": n.Destination,
}
if !n.Title.IsEmpty() {
m["Title"] = n.Title
}
if n.Reference != nil {
r := map[string]any{}
r["Kind"] = n.Reference.ReferenceLinkKind
r["Value"] = n.Reference.Value
m["Reference"] = r
}
return NewNodeDump(n, m)
}
// KindLink is a NodeKind of the Link node.
var KindLink = NewNodeKind("Link")
// Kind implements Node.Kind.
func (n *Link) Kind() NodeKind {
return KindLink
}
// NewLink returns a new Link node with the given destination and options.
func NewLink(destination text.SingleLineValue, opts ...LinkOption) *Link {
n := &Link{}
n.Init(n)
n.Destination = destination
for _, opt := range opts {
opt.setLinkOption(n)
}
return n
}
var _ InlineNode = (*Image)(nil)
// An Image struct represents an image of the Markdown text.
type Image struct {
BaseInline
// Destination is a destination(URL) of this link.
Destination text.SingleLineValue
// Title is a title of this link.
Title text.MultiLineValue
// Reference is a reference of this link. This field is used for reference links.
// If this link is not a reference link, this field is nil.
Reference *ReferenceLink
}
// Dump implements Node.Dump.
func (n *Image) Dump(_ []byte) *NodeDump {
m := map[string]any{
"Destination": n.Destination,
}
if !n.Title.IsEmpty() {
m["Title"] = n.Title
}
if n.Reference != nil {
r := map[string]any{}
r["Kind"] = n.Reference.ReferenceLinkKind
r["Value"] = n.Reference.Value
m["Reference"] = r
}
return NewNodeDump(n, m)
}
// KindImage is a NodeKind of the Image node.
var KindImage = NewNodeKind("Image")
// Kind implements Node.Kind.
func (n *Image) Kind() NodeKind {
return KindImage
}
// NewImage returns a new Image node with the given destination and options.
func NewImage(destination text.SingleLineValue, opts ...ImageOption) *Image {
n := &Image{}
n.Init(n)
n.Destination = destination
for _, opt := range opts {
opt.setImageOption(n)
}
return n
}
var _ InlineNode = (*AutoLink)(nil)
// An AutoLink struct represents an autolink of the Markdown text.
type AutoLink struct {
BaseInline
// Destination is the URL used for the href attribute.
// For email autolinks, it includes the "mailto:" prefix.
Destination text.SingleLineValue
// Label is the display text shown inside the link element.
Label text.SingleLineValue
// Text is the original text as parsed from source, including any
// surrounding syntax characters (e.g. "<" and ">" for CommonMark autolinks).
Text text.SingleLineValue
}
// AutoLinkOption is an option for AutoLink nodes.
type AutoLinkOption interface {
setAutoLinkOption(*AutoLink)
}
// Dump implements Node.Dump.
func (n *AutoLink) Dump(_ []byte) *NodeDump {
return NewNodeDump(n, map[string]any{
"Destination": n.Destination,
"Label": n.Label,
"Text": n.Text,
})
}
// KindAutoLink is a NodeKind of the AutoLink node.
var KindAutoLink = NewNodeKind("AutoLink")
// Kind implements Node.Kind.
func (n *AutoLink) Kind() NodeKind {
return KindAutoLink
}
// NewAutoLink returns a new AutoLink node with the given destination, label, and options.
func NewAutoLink(destination, label text.SingleLineValue, opts ...AutoLinkOption) *AutoLink {
n := &AutoLink{}
n.Init(n)
n.Destination = destination
n.Label = label
for _, opt := range opts {
opt.setAutoLinkOption(n)
}
return n
}
var _ InlineNode = (*RawHTML)(nil)
// A RawHTML struct represents an inline raw HTML of the Markdown text.
type RawHTML struct {
node
// Value holds the raw HTML content.
Value text.MultiLineValue
}
func (n *RawHTML) inlineNode() {}
// Pos implements Node.Pos.
func (n *RawHTML) Pos() int {
if n.Value.IsOwned() {
return -1
}
return n.Value.Index().Start
}
// SetPos implements Node.SetPos.
func (n *RawHTML) SetPos(_ int) {}
// Dump implements Node.Dump.
func (n *RawHTML) Dump(_ []byte) *NodeDump {
return NewNodeDump(n, map[string]any{
"Value": n.Value,
})
}
// FirstRune implements [InlineNode].FirstRune.
func (n *RawHTML) FirstRune(_ []byte) (rune, bool) {
return rune('<'), true
}
// LastRune implements [InlineNode].LastRune.
func (n *RawHTML) LastRune(_ []byte) (rune, bool) {
return rune('>'), true
}
// KindRawHTML is a NodeKind of the RawHTML node.
var KindRawHTML = NewNodeKind("RawHTML")
// Kind implements Node.Kind.
func (n *RawHTML) Kind() NodeKind {
return KindRawHTML
}
// NewRawHTML returns a new RawHTML node with the given value.
func NewRawHTML(value text.MultiLineValue) *RawHTML {
n := &RawHTML{Value: value}
return n
}