mirror of
https://github.com/yuin/goldmark
synced 2025-03-04 23:04:52 +00:00
#248 - 10
This commit is contained in:
parent
661ccb7c9e
commit
b2c88c80f6
2 changed files with 30 additions and 13 deletions
|
|
@ -639,3 +639,13 @@ b</li>
|
||||||
</li>
|
</li>
|
||||||
</ul>
|
</ul>
|
||||||
//= = = = = = = = = = = = = = = = = = = = = = = =//
|
//= = = = = = = = = = = = = = = = = = = = = = = =//
|
||||||
|
|
||||||
|
|
||||||
|
50: Spaces before a visible hard linebreak should be preserved
|
||||||
|
//- - - - - - - - -//
|
||||||
|
a \
|
||||||
|
b
|
||||||
|
//- - - - - - - - -//
|
||||||
|
<p>a <br />
|
||||||
|
b</p>
|
||||||
|
//= = = = = = = = = = = = = = = = = = = = = = = =//
|
||||||
|
|
|
||||||
|
|
@ -1115,6 +1115,12 @@ func (p *parser) walkBlock(block ast.Node, cb func(node ast.Node)) {
|
||||||
cb(block)
|
cb(block)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const (
|
||||||
|
lineBreakHard uint8 = 1 << iota
|
||||||
|
lineBreakSoft
|
||||||
|
lineBreakVisible
|
||||||
|
)
|
||||||
|
|
||||||
func (p *parser) parseBlock(block text.BlockReader, parent ast.Node, pc Context) {
|
func (p *parser) parseBlock(block text.BlockReader, parent ast.Node, pc Context) {
|
||||||
if parent.IsRaw() {
|
if parent.IsRaw() {
|
||||||
return
|
return
|
||||||
|
|
@ -1129,27 +1135,25 @@ func (p *parser) parseBlock(block text.BlockReader, parent ast.Node, pc Context)
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
lineLength := len(line)
|
lineLength := len(line)
|
||||||
softLinebreak := false
|
var lineBreakFlags uint8 = 0
|
||||||
hardlineBreak := false
|
|
||||||
hasNewLine := line[lineLength-1] == '\n'
|
hasNewLine := line[lineLength-1] == '\n'
|
||||||
if ((lineLength >= 3 && line[lineLength-2] == '\\' && line[lineLength-3] != '\\') || (lineLength == 2 && line[lineLength-2] == '\\')) && hasNewLine { // ends with \\n
|
if ((lineLength >= 3 && line[lineLength-2] == '\\' && line[lineLength-3] != '\\') || (lineLength == 2 && line[lineLength-2] == '\\')) && hasNewLine { // ends with \\n
|
||||||
lineLength -= 2
|
lineLength -= 2
|
||||||
hardlineBreak = true
|
lineBreakFlags |= lineBreakHard | lineBreakVisible
|
||||||
|
|
||||||
} else if ((lineLength >= 4 && line[lineLength-3] == '\\' && line[lineLength-2] == '\r' && line[lineLength-4] != '\\') || (lineLength == 3 && line[lineLength-3] == '\\' && line[lineLength-2] == '\r')) && hasNewLine { // ends with \\r\n
|
} else if ((lineLength >= 4 && line[lineLength-3] == '\\' && line[lineLength-2] == '\r' && line[lineLength-4] != '\\') || (lineLength == 3 && line[lineLength-3] == '\\' && line[lineLength-2] == '\r')) && hasNewLine { // ends with \\r\n
|
||||||
lineLength -= 3
|
lineLength -= 3
|
||||||
hardlineBreak = true
|
lineBreakFlags |= lineBreakHard | lineBreakVisible
|
||||||
} else if lineLength >= 3 && line[lineLength-3] == ' ' && line[lineLength-2] == ' ' && hasNewLine { // ends with [space][space]\n
|
} else if lineLength >= 3 && line[lineLength-3] == ' ' && line[lineLength-2] == ' ' && hasNewLine { // ends with [space][space]\n
|
||||||
lineLength -= 3
|
lineLength -= 3
|
||||||
hardlineBreak = true
|
lineBreakFlags |= lineBreakHard
|
||||||
} else if lineLength >= 4 && line[lineLength-4] == ' ' && line[lineLength-3] == ' ' && line[lineLength-2] == '\r' && hasNewLine { // ends with [space][space]\r\n
|
} else if lineLength >= 4 && line[lineLength-4] == ' ' && line[lineLength-3] == ' ' && line[lineLength-2] == '\r' && hasNewLine { // ends with [space][space]\r\n
|
||||||
lineLength -= 4
|
lineLength -= 4
|
||||||
hardlineBreak = true
|
lineBreakFlags |= lineBreakHard
|
||||||
} else if hasNewLine {
|
} else if hasNewLine {
|
||||||
// If the line ends with a newline character, but it is not a hardlineBreak, then it is a softLinebreak
|
// If the line ends with a newline character, but it is not a hardlineBreak, then it is a softLinebreak
|
||||||
// If the line ends with a hardlineBreak, then it cannot end with a softLinebreak
|
// If the line ends with a hardlineBreak, then it cannot end with a softLinebreak
|
||||||
// See https://spec.commonmark.org/0.30/#soft-line-breaks
|
// See https://spec.commonmark.org/0.30/#soft-line-breaks
|
||||||
softLinebreak = true
|
lineBreakFlags |= lineBreakSoft
|
||||||
}
|
}
|
||||||
|
|
||||||
l, startPosition := block.Position()
|
l, startPosition := block.Position()
|
||||||
|
|
@ -1213,11 +1217,14 @@ func (p *parser) parseBlock(block text.BlockReader, parent ast.Node, pc Context)
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
diff := startPosition.Between(currentPosition)
|
diff := startPosition.Between(currentPosition)
|
||||||
stop := diff.Stop
|
var text *ast.Text
|
||||||
rest := diff.WithStop(stop)
|
if lineBreakFlags&(lineBreakHard|lineBreakVisible) == lineBreakHard|lineBreakVisible {
|
||||||
text := ast.NewTextSegment(rest.TrimRightSpace(source))
|
text = ast.NewTextSegment(diff)
|
||||||
text.SetSoftLineBreak(softLinebreak)
|
} else {
|
||||||
text.SetHardLineBreak(hardlineBreak)
|
text = ast.NewTextSegment(diff.TrimRightSpace(source))
|
||||||
|
}
|
||||||
|
text.SetSoftLineBreak(lineBreakFlags&lineBreakSoft != 0)
|
||||||
|
text.SetHardLineBreak(lineBreakFlags&lineBreakHard != 0)
|
||||||
parent.AppendChild(parent, text)
|
parent.AppendChild(parent, text)
|
||||||
block.AdvanceLine()
|
block.AdvanceLine()
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue