Skip to content

Commit 62de5bd

Browse files
authored
IIRR-34: Fix newlines around codeblocks for Redcarpet renderer (#94)
* IIRR-34: Fix newlines around codeblocks for Redcarpet renderer * IIRR-34: Fix bug with regexp that fixes newlines eating last character
1 parent 0954ba3 commit 62de5bd

3 files changed

Lines changed: 26 additions & 0 deletions

File tree

app/assets/stylesheets/application.scss

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,10 @@ td:last-child {
125125
width: 120px;
126126
}
127127

128+
td pre {
129+
margin-bottom: 1rem;
130+
}
131+
128132
/* Banner styles - using styleguide colors where possible */
129133
.banner {
130134
position: fixed;

app/helpers/application_helper.rb

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,14 @@ module ApplicationHelper
22
def markdown(text)
33
renderer = Redcarpet::Render::HTML.new(escape_html: true)
44
markdown = Redcarpet::Markdown.new(renderer, fenced_code_blocks: true)
5+
6+
# Redcarpet needs the codeblocks to have 2 newlines before them
7+
# fixing this at render because it's not really required by Discord
8+
# it's just a quirk of Redcarpet
9+
if text.match?(/[^\n]\n```(.*)\n```/m)
10+
text = text.gsub(/([^\n])\n```(.*?)\n```/m, "\\1\n\n```\\2\n```")
11+
end
12+
513
markdown.render(text.to_s).html_safe
614
end
715
end

test/helpers/markdown_helper_test.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,18 @@ class ACodeBlock
2424

2525
assert_includes as_html, "<pre><code>class ACodeBlock"
2626
end
27+
28+
test "fixes newlines around code blocks" do
29+
puzzle_content = <<~CONTENT
30+
Some content with
31+
```
32+
class ACodeBlock
33+
end
34+
```
35+
in it
36+
CONTENT
37+
as_html = markdown(puzzle_content)
38+
39+
assert_includes as_html, "with</p>\n\n<pre><code>class ACodeBlock"
40+
end
2741
end

0 commit comments

Comments
 (0)