Skip to content

Commit

Permalink
Keep blocks indented if they are
Browse files Browse the repository at this point in the history
  • Loading branch information
krschacht committed May 15, 2024
1 parent 073ef3b commit 96c585c
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 4 deletions.
2 changes: 1 addition & 1 deletion lib/markdown_renderer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,6 @@ def self.render(markdown, options = {})
end

def self.ensure_blank_line_before_code_block_start(markdown)
markdown.gsub(/(\n*)(```.*?```)/m, "\n\n\\2")
markdown.gsub(/(\n*)( *)(```.*?```)/m, "\n\\2\\3")
end
end
15 changes: 12 additions & 3 deletions test/lib/markdown_renderer_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class MarkdownRendererTest < ActiveSupport::TestCase
@renderer = MarkdownRenderer
end

test 'ensure_blank_line_before_code_block_start adds blank line before code block when zero newlines' do
test "ensure_blank_line_before_code_block_start adds blank line before code block when zero newlines" do
markdown = "Text before code block```ruby\ncode block\n```"
expected = "Text before code block\n\n```ruby\ncode block\n```"
assert_equal expected, MarkdownRenderer.ensure_blank_line_before_code_block_start(markdown)
Expand All @@ -19,7 +19,7 @@ class MarkdownRendererTest < ActiveSupport::TestCase
assert_equal expected, MarkdownRenderer.ensure_blank_line_before_code_block_start(markdown)
end

test 'ensure_blank_line_before_code_block_start adds blank line before code block when one newline' do
test "ensure_blank_line_before_code_block_start adds blank line before code block when one newline" do
markdown = "Text before code block\n```ruby\ncode block\n```"
expected = "Text before code block\n\n```ruby\ncode block\n```"
assert_equal expected, MarkdownRenderer.ensure_blank_line_before_code_block_start(markdown)
Expand All @@ -33,7 +33,7 @@ class MarkdownRendererTest < ActiveSupport::TestCase
assert_equal expected, MarkdownRenderer.ensure_blank_line_before_code_block_start(markdown)
end

test 'ensure_blank_line_before_code_block_start does not add blank line when one is already present' do
test "ensure_blank_line_before_code_block_start does not add blank line when one is already present" do
markdown = "Text before code block\n\n```ruby\ncode block\n```"
expected = "Text before code block\n\n```ruby\ncode block\n```"
assert_equal expected, MarkdownRenderer.ensure_blank_line_before_code_block_start(markdown)
Expand All @@ -47,6 +47,15 @@ class MarkdownRendererTest < ActiveSupport::TestCase
assert_equal expected, MarkdownRenderer.ensure_blank_line_before_code_block_start(markdown)
end

test "ensure_blank_line_before_code_block_start keeps a block indented if it is indented" do
# TODO: Notably, if a block is indented right after a bullet such that it's intended to be included within the bullet,
# redcarpet is not including the block within the bullet: https://github.com/allyourbot/hostedgpt/issues/323

markdown = "Text before code block\n\n ``` ruby\n code block\n ```"
expected = "Text before code block\n\n ``` ruby\n code block\n ```"
assert_equal expected, MarkdownRenderer.ensure_blank_line_before_code_block_start(markdown)
end

test "block_code nicely formatted gets converted" do
markdown = <<~MD
This is sql:
Expand Down

0 comments on commit 96c585c

Please sign in to comment.