diff --git a/_test/extra.txt b/_test/extra.txt
index 1701fd5..197a3f5 100644
--- a/_test/extra.txt
+++ b/_test/extra.txt
@@ -395,3 +395,17 @@ hello\x00world
//- - - - - - - - -//
hello\ufffdworld
//= = = = = = = = = = = = = = = = = = = = = = = =//
+
+27: Newlines in code spans must be preserved as a space
+ OPTIONS: {"enableEscape": true}
+//- - - - - - - - -//
+`\n`
+
+`x\n`
+
+`\nx`
+//- - - - - - - - -//
+
+x
+ x
+//= = = = = = = = = = = = = = = = = = = = = = = =//
diff --git a/parser/code_span.go b/parser/code_span.go
index 1365236..a74b09b 100644
--- a/parser/code_span.go
+++ b/parser/code_span.go
@@ -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 {
@@ -52,9 +51,7 @@ 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:
@@ -62,11 +59,11 @@ end:
// 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 {
@@ -81,3 +78,7 @@ end:
}
return node
}
+
+func isSpaceOrNewline(c byte) bool {
+ return c == ' ' || c == '\n'
+}
diff --git a/renderer/html/html.go b/renderer/html/html.go
index d0243cb..90221b0 100644
--- a/renderer/html/html.go
+++ b/renderer/html/html.go
@@ -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)
}