Skip to content

Commit

Permalink
markdown: support soft-line breaks (#92)
Browse files Browse the repository at this point in the history
  • Loading branch information
unknwon authored Feb 15, 2023
1 parent ff80d07 commit d290608
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 2 deletions.
5 changes: 5 additions & 0 deletions markdown/extender.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,11 @@ func (r *nodeRenderer) RegisterFuncs(reg renderer.NodeRendererFuncRegisterer) {

n := node.(*ast.Text)
text := n.Text(source)
if len(text) == 0 {
// Simply write a line break if there is no text in the node, otherwise, soft break lines are sticking.
_ = w.WriteByte('\n')
return ast.WalkContinue, nil
}

// Rewrites `{#foo}` directives in text to `<a id="foo"></a>` anchors.
matches := anchorDirectivePattern.FindAllIndex(text, -1)
Expand Down
1 change: 0 additions & 1 deletion markdown/markdown.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ func New(opts Options) goldmark.Markdown {
parser.WithAttribute(),
),
goldmark.WithRendererOptions(
goldmarkhtml.WithHardWraps(),
goldmarkhtml.WithXHTML(),
goldmarkhtml.WithUnsafe(),
),
Expand Down
4 changes: 3 additions & 1 deletion markdown/markdown_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,9 @@ func TestRenderer(t *testing.T) {
if err != nil {
t.Fatal(err)
}
want := `<h1 id="b"><a name="b" class="anchor" href="#b" rel="nofollow" aria-hidden="true" title="#b"></a>a</h1>` + "\n"
want := `<h1 id="b"><a name="b" class="anchor" href="#b" rel="nofollow" aria-hidden="true" title="#b"></a>a
</h1>
`
if string(doc.HTML) != want {
t.Errorf("got %q, want %q", string(doc.HTML), want)
}
Expand Down

0 comments on commit d290608

Please sign in to comment.