Skip to content

Commit 6e08c46

Browse files
committed
Render whitespace for selected markdown elements to improve to-text rendering
1 parent ef61164 commit 6e08c46

File tree

3 files changed

+18
-15
lines changed

3 files changed

+18
-15
lines changed

app/views/components/markdown/base.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,15 @@ def visit(node)
3737
plain(node.string_content)
3838
in :heading
3939
header(node.header_level) { visit_children(node) }
40+
whitespace
4041
in :paragraph
4142
grandparent = node.parent&.parent
4243

4344
if grandparent&.type == :list && grandparent&.list_tight
4445
visit_children(node)
4546
else
4647
p { visit_children(node) }
48+
whitespace
4749
end
4850
in :link
4951
link(node.url, node.title) { visit_children(node) }
@@ -60,6 +62,7 @@ def visit(node)
6062
end
6163
in :item
6264
li { visit_children(node) }
65+
whitespace
6366
in :code
6467
inline_code do |**attributes|
6568
code(**attributes) { plain(node.string_content) }

spec/views/components/markdown/base_spec.rb

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
###### 6
1414
MD
1515

16-
expect(output).to be ==
17-
"<h1>1</h1><h2>2</h2><h3>3</h3><h4>4</h4><h5>5</h5><h6>6</h6>"
16+
expect(output.strip).to be ==
17+
"<h1>1</h1> <h2>2</h2> <h3>3</h3> <h4>4</h4> <h5>5</h5> <h6>6</h6>"
1818
end
1919

2020
it "supports ordered lists" do
@@ -25,7 +25,7 @@
2525
MD
2626

2727
expect(output).to be ==
28-
"<ol><li>One</li><li>Two</li><li>Three</li></ol>"
28+
"<ol><li>One</li> <li>Two</li> <li>Three</li> </ol>"
2929
end
3030

3131
it "supports unordered lists" do
@@ -36,12 +36,12 @@
3636
MD
3737

3838
expect(output).to be ==
39-
"<ul><li>One</li><li>Two</li><li>Three</li></ul>"
39+
"<ul><li>One</li> <li>Two</li> <li>Three</li> </ul>"
4040
end
4141

4242
it "supports inline code" do
4343
output = md "Some `code` here"
44-
expect(output).to be == "<p>Some <code>code</code> here</p>"
44+
expect(output.strip).to be == "<p>Some <code>code</code> here</p>"
4545
end
4646

4747
it "supports block code" do
@@ -59,27 +59,27 @@ def foo
5959

6060
it "supports paragraphs" do
6161
output = md "A\n\nB"
62-
expect(output).to be == "<p>A</p><p>B</p>"
62+
expect(output.strip).to be == "<p>A</p> <p>B</p>"
6363
end
6464

6565
it "supports links" do
6666
output = md "[Hello](world 'title')"
67-
expect(output).to be == %(<p><a href="world" title="title">Hello</a></p>)
67+
expect(output.strip).to be == %(<p><a href="world" title="title">Hello</a></p>)
6868
end
6969

7070
it "supports emphasis" do
7171
output = md "*Hello*"
72-
expect(output).to be == "<p><em>Hello</em></p>"
72+
expect(output.strip).to be == "<p><em>Hello</em></p>"
7373
end
7474

7575
it "supports strong" do
7676
output = md "**Hello**"
77-
expect(output).to be == "<p><strong>Hello</strong></p>"
77+
expect(output.strip).to be == "<p><strong>Hello</strong></p>"
7878
end
7979

8080
it "supports blockquotes" do
8181
output = md "> Hello"
82-
expect(output).to be == "<blockquote><p>Hello</p></blockquote>"
82+
expect(output).to be == "<blockquote><p>Hello</p> </blockquote>"
8383
end
8484

8585
it "supports horizontal rules" do
@@ -89,7 +89,7 @@ def foo
8989

9090
it "supports images" do
9191
output = md "![alt](src 'title')"
92-
expect(output).to be == %(<p><img src="src" alt="alt" title="title"></p>)
92+
expect(output.strip).to be == %(<p><img src="src" alt="alt" title="title"></p>)
9393
end
9494

9595
it "supports softbreaks in content as spaces" do
@@ -100,7 +100,7 @@ def foo
100100
Three
101101
MD
102102

103-
expect(output).to be == "<p>One Two</p><p>Three</p>"
103+
expect(output.strip).to be == "<p>One Two</p> <p>Three</p>"
104104
end
105105

106106
xit "supports blockquote [!NOTE]" do
@@ -109,7 +109,7 @@ def foo
109109
> Hello!
110110
MD
111111

112-
expect(output).to match %r{<blockquote><svg .*><p>Hello!</p></blockquote>}
112+
expect(output.strip).to match %r{<blockquote><svg .*><p>Hello!</p> </blockquote>}
113113
end
114114

115115
def md(content)

spec/views/components/markdown/erb_spec.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@ def render(content, &block)
2727
end
2828

2929
it "ignores inline fenced erb following text" do
30-
expect(render("This is the number <%= 1 + 1 %>")).to eq("<p>This is the number <%= 1 + 1 %></p>")
30+
expect(render("This is the number <%= 1 + 1 %>").strip).to eq("<p>This is the number <%= 1 + 1 %></p>")
3131
end
3232

3333
it "escapes inline fenced erb" do
34-
expect(render("`<%= 1 + 1 %>`")).to eq("<p><code>&lt;%= 1 + 1 %&gt;</code></p>")
34+
expect(render("`<%= 1 + 1 %>`").strip).to eq("<p><code>&lt;%= 1 + 1 %&gt;</code></p>")
3535
end
3636

3737
it "handles multiline fenced erb" do

0 commit comments

Comments
 (0)