mirror of
https://github.com/yuin/goldmark
synced 2025-03-04 23:04:52 +00:00
Fixes #36
This commit is contained in:
parent
4536e57938
commit
6c55ba55a1
3 changed files with 40 additions and 22 deletions
|
|
@ -96,6 +96,9 @@ func (b *atxHeadingParser) Open(parent ast.Node, reader text.Reader, pc Context)
|
||||||
return nil, NoChildren
|
return nil, NoChildren
|
||||||
}
|
}
|
||||||
start := i + l
|
start := i + l
|
||||||
|
if start >= len(line) {
|
||||||
|
start = len(line) - 1
|
||||||
|
}
|
||||||
origstart := start
|
origstart := start
|
||||||
stop := len(line) - util.TrimRightSpaceLength(line)
|
stop := len(line) - util.TrimRightSpaceLength(line)
|
||||||
|
|
||||||
|
|
@ -128,7 +131,7 @@ func (b *atxHeadingParser) Open(parent ast.Node, reader text.Reader, pc Context)
|
||||||
for _, attr := range attrs {
|
for _, attr := range attrs {
|
||||||
node.SetAttribute(attr.Name, attr.Value)
|
node.SetAttribute(attr.Name, attr.Value)
|
||||||
}
|
}
|
||||||
node.Lines().Append(text.NewSegment(segment.Start+start+1, segment.Start+closureOpen))
|
node.Lines().Append(text.NewSegment(segment.Start+start+1-segment.Padding, segment.Start+closureOpen-segment.Padding))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
@ -136,7 +139,7 @@ func (b *atxHeadingParser) Open(parent ast.Node, reader text.Reader, pc Context)
|
||||||
start = origstart
|
start = origstart
|
||||||
stop := len(line) - util.TrimRightSpaceLength(line)
|
stop := len(line) - util.TrimRightSpaceLength(line)
|
||||||
if stop <= start { // empty headings like '##[space]'
|
if stop <= start { // empty headings like '##[space]'
|
||||||
stop = start + 1
|
stop = start
|
||||||
} else {
|
} else {
|
||||||
i = stop - 1
|
i = stop - 1
|
||||||
for ; line[i] == '#' && i >= start; i-- {
|
for ; line[i] == '#' && i >= start; i-- {
|
||||||
|
|
@ -149,7 +152,7 @@ func (b *atxHeadingParser) Open(parent ast.Node, reader text.Reader, pc Context)
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(util.TrimRight(line[start:stop], []byte{'#'})) != 0 { // empty heading like '### ###'
|
if len(util.TrimRight(line[start:stop], []byte{'#'})) != 0 { // empty heading like '### ###'
|
||||||
node.Lines().Append(text.NewSegment(segment.Start+start, segment.Start+stop))
|
node.Lines().Append(text.NewSegment(segment.Start+start-segment.Padding, segment.Start+stop-segment.Padding))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return node, NoChildren
|
return node, NoChildren
|
||||||
|
|
|
||||||
|
|
@ -824,7 +824,7 @@ func (p *parser) Parse(reader text.Reader, opts ...ParseOption) ast.Node {
|
||||||
for _, at := range p.astTransformers {
|
for _, at := range p.astTransformers {
|
||||||
at.Transform(root, reader, pc)
|
at.Transform(root, reader, pc)
|
||||||
}
|
}
|
||||||
//root.Dump(reader.Source(), 0)
|
// root.Dump(reader.Source(), 0)
|
||||||
return root
|
return root
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -1086,13 +1086,28 @@ func (p *parser) parseBlock(block text.BlockReader, parent ast.Node, pc Context)
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
lineLength := len(line)
|
lineLength := len(line)
|
||||||
|
hardlineBreak := false
|
||||||
|
softLinebreak := line[lineLength-1] == '\n'
|
||||||
|
if lineLength > 2 && line[lineLength-2] == '\\' && softLinebreak { // ends with \\n
|
||||||
|
lineLength -= 2
|
||||||
|
hardlineBreak = true
|
||||||
|
|
||||||
|
} else if lineLength > 3 && line[lineLength-3] == '\\' && line[lineLength-2] == '\r' && softLinebreak { // ends with \\r\n
|
||||||
|
lineLength -= 3
|
||||||
|
hardlineBreak = true
|
||||||
|
} else if lineLength > 3 && line[lineLength-3] == ' ' && line[lineLength-2] == ' ' && softLinebreak { // ends with [space][space]\n
|
||||||
|
lineLength -= 3
|
||||||
|
hardlineBreak = true
|
||||||
|
} else if lineLength > 4 && line[lineLength-4] == ' ' && line[lineLength-3] == ' ' && line[lineLength-2] == '\r' && softLinebreak { // ends with [space][space]\r\n
|
||||||
|
lineLength -= 4
|
||||||
|
hardlineBreak = true
|
||||||
|
}
|
||||||
|
|
||||||
l, startPosition := block.Position()
|
l, startPosition := block.Position()
|
||||||
n := 0
|
n := 0
|
||||||
softLinebreak := false
|
|
||||||
for i := 0; i < lineLength; i++ {
|
for i := 0; i < lineLength; i++ {
|
||||||
c := line[i]
|
c := line[i]
|
||||||
if c == '\n' {
|
if c == '\n' {
|
||||||
softLinebreak = true
|
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
isSpace := util.IsSpace(c)
|
isSpace := util.IsSpace(c)
|
||||||
|
|
@ -1150,20 +1165,6 @@ func (p *parser) parseBlock(block text.BlockReader, parent ast.Node, pc Context)
|
||||||
}
|
}
|
||||||
diff := startPosition.Between(currentPosition)
|
diff := startPosition.Between(currentPosition)
|
||||||
stop := diff.Stop
|
stop := diff.Stop
|
||||||
hardlineBreak := false
|
|
||||||
if lineLength > 2 && line[lineLength-2] == '\\' && softLinebreak { // ends with \\n
|
|
||||||
stop--
|
|
||||||
hardlineBreak = true
|
|
||||||
|
|
||||||
} else if lineLength > 3 && line[lineLength-3] == '\\' && line[lineLength-2] == '\r' && softLinebreak { // ends with \\r\n
|
|
||||||
stop -= 2
|
|
||||||
hardlineBreak = true
|
|
||||||
} else if lineLength > 3 && line[lineLength-3] == ' ' && line[lineLength-2] == ' ' && softLinebreak { // ends with [space][space]\n
|
|
||||||
stop--
|
|
||||||
hardlineBreak = true
|
|
||||||
} else if lineLength > 4 && line[lineLength-4] == ' ' && line[lineLength-3] == ' ' && line[lineLength-2] == '\r' && softLinebreak { // ends with [space][space]\r\n
|
|
||||||
hardlineBreak = true
|
|
||||||
}
|
|
||||||
rest := diff.WithStop(stop)
|
rest := diff.WithStop(stop)
|
||||||
text := ast.NewTextSegment(rest.TrimRightSpace(source))
|
text := ast.NewTextSegment(rest.TrimRightSpace(source))
|
||||||
text.SetSoftLineBreak(softLinebreak)
|
text.SetSoftLineBreak(softLinebreak)
|
||||||
|
|
|
||||||
18
util/util.go
18
util/util.go
|
|
@ -91,7 +91,7 @@ func VisualizeSpaces(bs []byte) []byte {
|
||||||
bs = bytes.Replace(bs, []byte(" "), []byte("[SPACE]"), -1)
|
bs = bytes.Replace(bs, []byte(" "), []byte("[SPACE]"), -1)
|
||||||
bs = bytes.Replace(bs, []byte("\t"), []byte("[TAB]"), -1)
|
bs = bytes.Replace(bs, []byte("\t"), []byte("[TAB]"), -1)
|
||||||
bs = bytes.Replace(bs, []byte("\n"), []byte("[NEWLINE]\n"), -1)
|
bs = bytes.Replace(bs, []byte("\n"), []byte("[NEWLINE]\n"), -1)
|
||||||
bs = bytes.Replace(bs, []byte("\r"), []byte("[CR]\n"), -1)
|
bs = bytes.Replace(bs, []byte("\r"), []byte("[CR]"), -1)
|
||||||
return bs
|
return bs
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -620,8 +620,22 @@ func URLEscape(v []byte, resolveReference bool) []byte {
|
||||||
n = i
|
n = i
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
if int(u8len) >= len(v) {
|
||||||
|
u8len = int8(len(v) - 1)
|
||||||
|
}
|
||||||
|
if u8len == 0 {
|
||||||
|
i++
|
||||||
|
n = i
|
||||||
|
continue
|
||||||
|
}
|
||||||
cob.Write(v[n:i])
|
cob.Write(v[n:i])
|
||||||
cob.Write(StringToReadOnlyBytes(url.QueryEscape(string(v[i : i+int(u8len)]))))
|
stop := i + int(u8len)
|
||||||
|
if stop > len(v) {
|
||||||
|
i += 1
|
||||||
|
n = i
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
cob.Write(StringToReadOnlyBytes(url.QueryEscape(string(v[i:stop]))))
|
||||||
i += int(u8len)
|
i += int(u8len)
|
||||||
n = i
|
n = i
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue