mirror of
https://github.com/russross/blackfriday.git
synced 2024-03-22 13:40:34 +08:00
Remove calls to Truncate() from linebreak parser
Replace output truncation with appropriate inline callbacks. lineBreak() is now only responsible for handling HardLineBreak. BackslashLineBreak is handled in escape() and trailing whitespace is considered in maybeLineBreak().
This commit is contained in:
parent
ef087889f4
commit
8e90e8b645
47
inline.go
47
inline.go
|
@ -155,31 +155,28 @@ func codeSpan(p *parser, out *bytes.Buffer, data []byte, offset int) int {
|
|||
}
|
||||
|
||||
// newline preceded by two spaces becomes <br>
|
||||
// newline without two spaces works when EXTENSION_HARD_LINE_BREAK is enabled
|
||||
func maybeLineBreak(p *parser, out *bytes.Buffer, data []byte, offset int) int {
|
||||
origOffset := offset
|
||||
for offset < len(data) && data[offset] == ' ' {
|
||||
offset++
|
||||
}
|
||||
if offset < len(data) && data[offset] == '\n' {
|
||||
if offset-origOffset >= 2 {
|
||||
p.r.LineBreak(out)
|
||||
return offset - origOffset + 1
|
||||
}
|
||||
return offset - origOffset
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
// newline without two spaces works when HardLineBreak is enabled
|
||||
func lineBreak(p *parser, out *bytes.Buffer, data []byte, offset int) int {
|
||||
// remove trailing spaces from out
|
||||
outBytes := out.Bytes()
|
||||
end := len(outBytes)
|
||||
eol := end
|
||||
for eol > 0 && outBytes[eol-1] == ' ' {
|
||||
eol--
|
||||
if p.flags&HardLineBreak != 0 {
|
||||
p.r.LineBreak(out)
|
||||
return 1
|
||||
}
|
||||
out.Truncate(eol)
|
||||
|
||||
precededByTwoSpaces := offset >= 2 && data[offset-2] == ' ' && data[offset-1] == ' '
|
||||
precededByBackslash := offset >= 1 && data[offset-1] == '\\' // see http://spec.commonmark.org/0.18/#example-527
|
||||
precededByBackslash = precededByBackslash && p.flags&BackslashLineBreak != 0
|
||||
|
||||
// should there be a hard line break here?
|
||||
if p.flags&HardLineBreak == 0 && !precededByTwoSpaces && !precededByBackslash {
|
||||
return 0
|
||||
}
|
||||
|
||||
if precededByBackslash && eol > 0 {
|
||||
out.Truncate(eol - 1)
|
||||
}
|
||||
p.r.LineBreak(out)
|
||||
return 1
|
||||
return 0
|
||||
}
|
||||
|
||||
type linkType int
|
||||
|
@ -615,6 +612,10 @@ func escape(p *parser, out *bytes.Buffer, data []byte, offset int) int {
|
|||
data = data[offset:]
|
||||
|
||||
if len(data) > 1 {
|
||||
if p.flags&BackslashLineBreak != 0 && data[1] == '\n' {
|
||||
p.r.LineBreak(out)
|
||||
return 2
|
||||
}
|
||||
if bytes.IndexByte(escapeChars, data[1]) < 0 {
|
||||
return 0
|
||||
}
|
||||
|
|
|
@ -376,6 +376,7 @@ func MarkdownOptions(input []byte, renderer Renderer, opts Options) []byte {
|
|||
p.inlineCallback['&'] = entity
|
||||
p.inlineCallback['!'] = maybeImage
|
||||
p.inlineCallback['^'] = maybeInlineFootnote
|
||||
p.inlineCallback[' '] = maybeLineBreak
|
||||
|
||||
if extensions&Autolink != 0 {
|
||||
p.inlineCallback[':'] = autoLink
|
||||
|
|
Loading…
Reference in New Issue
Block a user