Skip to content

Commit

Permalink
Fix #245 - 5
Browse files Browse the repository at this point in the history
  • Loading branch information
yuin committed Sep 11, 2021
1 parent 351308f commit 7efc483
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 9 deletions.
14 changes: 14 additions & 0 deletions _test/extra.txt
Original file line number Diff line number Diff line change
Expand Up @@ -395,3 +395,17 @@ hello\x00world
//- - - - - - - - -//
<p>hello\ufffdworld</p>
//= = = = = = = = = = = = = = = = = = = = = = = =//

27: Newlines in code spans must be preserved as a space
OPTIONS: {"enableEscape": true}
//- - - - - - - - -//
`\n`

`x\n`

`\nx`
//- - - - - - - - -//
<p><code> </code></p>
<p><code>x </code></p>
<p><code> x</code></p>
//= = = = = = = = = = = = = = = = = = = = = = = =//
13 changes: 7 additions & 6 deletions parser/code_span.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package parser
import (
"github.com/yuin/goldmark/ast"
"github.com/yuin/goldmark/text"
"github.com/yuin/goldmark/util"
)

type codeSpanParser struct {
Expand Down Expand Up @@ -52,21 +51,19 @@ func (s *codeSpanParser) Parse(parent ast.Node, block text.Reader, pc Context) a
}
}
}
if !util.IsBlank(line) {
node.AppendChild(node, ast.NewRawTextSegment(segment))
}
node.AppendChild(node, ast.NewRawTextSegment(segment))
block.AdvanceLine()
}
end:
if !node.IsBlank(block.Source()) {
// trim first halfspace and last halfspace
segment := node.FirstChild().(*ast.Text).Segment
shouldTrimmed := true
if !(!segment.IsEmpty() && block.Source()[segment.Start] == ' ') {
if !(!segment.IsEmpty() && isSpaceOrNewline(block.Source()[segment.Start])) {
shouldTrimmed = false
}
segment = node.LastChild().(*ast.Text).Segment
if !(!segment.IsEmpty() && block.Source()[segment.Stop-1] == ' ') {
if !(!segment.IsEmpty() && isSpaceOrNewline(block.Source()[segment.Stop-1])) {
shouldTrimmed = false
}
if shouldTrimmed {
Expand All @@ -81,3 +78,7 @@ end:
}
return node
}

func isSpaceOrNewline(c byte) bool {
return c == ' ' || c == '\n'
}
4 changes: 1 addition & 3 deletions renderer/html/html.go
Original file line number Diff line number Diff line change
Expand Up @@ -477,9 +477,7 @@ func (r *Renderer) renderCodeSpan(w util.BufWriter, source []byte, n ast.Node, e
value := segment.Value(source)
if bytes.HasSuffix(value, []byte("\n")) {
r.Writer.RawWrite(w, value[:len(value)-1])
if c != n.LastChild() {
r.Writer.RawWrite(w, []byte(" "))
}
r.Writer.RawWrite(w, []byte(" "))
} else {
r.Writer.RawWrite(w, value)
}
Expand Down

0 comments on commit 7efc483

Please sign in to comment.