Skip to content

Commit d8b123c

Browse files
committed
refactor: simplify codes
1 parent db34c99 commit d8b123c

7 files changed

Lines changed: 11 additions & 26 deletions

File tree

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ goldmark is compliant with CommonMark 0.31.2.
1212

1313
- [goldmark playground](https://yuin.github.io/goldmark/playground/) : Try goldmark online. This playground is built with WASM(5-10MB).
1414

15+
There is also a Rust version of goldmark: [rushdown](https://github.com/yuin/rushdown)
16+
1517
Motivation
1618
----------------------
1719
I needed a Markdown parser for Go that satisfies the following requirements:

extension/ast/tasklist.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package ast
22

33
import (
44
"fmt"
5+
56
gast "github.com/yuin/goldmark/ast"
67
)
78

parser/fcode_block.go

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,7 @@ func (b *fencedCodeBlockParser) Trigger() []byte {
3434

3535
func (b *fencedCodeBlockParser) Open(parent ast.Node, reader text.Reader, pc Context) (ast.Node, State) {
3636
line, segment := reader.PeekLine()
37-
pos := pc.BlockOffset()
38-
if pos < 0 || (line[pos] != '`' && line[pos] != '~') {
39-
return nil, NoChildren
40-
}
37+
pos := pc.BlockIndent()
4138
findent := pos
4239
fenceChar := line[pos]
4340
i := pos
@@ -79,11 +76,7 @@ func (b *fencedCodeBlockParser) Continue(node ast.Node, reader text.Reader, pc C
7976
}
8077
length := i - pos
8178
if length >= fdata.length && util.IsBlank(line[i:]) {
82-
newline := 1
83-
if line[len(line)-1] != '\n' {
84-
newline = 0
85-
}
86-
reader.Advance(segment.Stop - segment.Start - newline + segment.Padding)
79+
reader.AdvanceToEOL()
8780
return Close
8881
}
8982
}
@@ -99,7 +92,7 @@ func (b *fencedCodeBlockParser) Continue(node ast.Node, reader text.Reader, pc C
9992
}
10093
seg.ForceNewline = true // EOF as newline
10194
node.Lines().Append(seg)
102-
reader.AdvanceAndSetPadding(segment.Stop-segment.Start-pos-1, padding)
95+
reader.AdvanceToEOL()
10396
return Continue | NoChildren
10497
}
10598

parser/html_block.go

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -114,9 +114,6 @@ func (b *htmlBlockParser) Open(parent ast.Node, reader text.Reader, pc Context)
114114
var node *ast.HTMLBlock
115115
line, segment := reader.PeekLine()
116116
last := pc.LastOpenedBlock().Node
117-
if pos := pc.BlockOffset(); pos < 0 || line[pos] != '<' {
118-
return nil, NoChildren
119-
}
120117

121118
if m := htmlBlockType1OpenRegexp.FindSubmatchIndex(line); m != nil {
122119
node = ast.NewHTMLBlock(ast.HTMLBlockType1)

parser/list.go

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -80,14 +80,6 @@ func parseListItem(line []byte) ([6]int, listItemType) {
8080
return ret, typ
8181
}
8282

83-
func matchesListItem(source []byte, strict bool) ([6]int, listItemType) {
84-
m, typ := parseListItem(source)
85-
if typ != notList && (!strict || strict && m[1] < 4) {
86-
return m, typ
87-
}
88-
return m, notList
89-
}
90-
9183
func calcListOffset(source []byte, match [6]int) int {
9284
var offset int
9385
if match[4] < 0 || util.IsBlank(source[match[4]:]) { // list item starts with a blank line
@@ -132,7 +124,7 @@ func (b *listParser) Open(parent ast.Node, reader text.Reader, pc Context) (ast.
132124
return nil, NoChildren
133125
}
134126
line, _ := reader.PeekLine()
135-
match, typ := matchesListItem(line, true)
127+
match, typ := parseListItem(line)
136128
if typ == notList {
137129
return nil, NoChildren
138130
}
@@ -198,7 +190,7 @@ func (b *listParser) Continue(node ast.Node, reader text.Reader, pc Context) Sta
198190

199191
if indent < offset || lastIsEmpty {
200192
if indent < 4 {
201-
match, typ := matchesListItem(line, false) // may have a leading spaces more than 3
193+
match, typ := parseListItem(line)
202194
if typ != notList && match[1]-offset < 4 {
203195
marker := line[match[3]-1]
204196
if !list.CanContinue(marker, typ == orderedList) {

parser/list_item.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ func (b *listItemParser) Open(parent ast.Node, reader text.Reader, pc Context) (
2828
}
2929
offset := lastOffset(list)
3030
line, _ := reader.PeekLine()
31-
match, typ := matchesListItem(line, false)
31+
match, typ := parseListItem(line)
3232
if typ == notList {
3333
return nil, NoChildren
3434
}
@@ -61,7 +61,7 @@ func (b *listItemParser) Continue(node ast.Node, reader text.Reader, pc Context)
6161
isEmpty := node.ChildCount() == 0 && pc.Get(emptyListItemWithBlankLines) != nil
6262
indent, _ := util.IndentWidth(line, reader.LineOffset())
6363
if (isEmpty || indent < offset) && indent < 4 {
64-
_, typ := matchesListItem(line, true)
64+
_, typ := parseListItem(line)
6565
// new list item found
6666
if typ != notList {
6767
pc.Set(skipListParserKey, listItemFlagValue)

renderer/html/html.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -497,7 +497,7 @@ func (r *Renderer) renderThematicBreak(
497497
}
498498

499499
// LinkAttributeFilter defines attribute names which link elements can have.
500-
var LinkAttributeFilter = GlobalAttributeFilter.ExtendString(`download,hreflang,media,ping,referrerpolicy,rel,shape,target`) // nolint:lll
500+
var LinkAttributeFilter = GlobalAttributeFilter.ExtendString(`download,href,lang,media,ping,referrerpolicy,rel,shape,target`) // nolint:lll
501501

502502
func (r *Renderer) renderAutoLink(
503503
w util.BufWriter, source []byte, node ast.Node, entering bool) (ast.WalkStatus, error) {

0 commit comments

Comments
 (0)