Skip to content

Commit

Permalink
make newline optional at the end of codeblocks
Browse files Browse the repository at this point in the history
  • Loading branch information
lilacstella committed Jul 1, 2024
1 parent 12f00f7 commit 4a720a6
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/rundoc/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ module Rundoc
class Parser
DEFAULT_KEYWORD = ":::"
INDENT_BLOCK = '(?<before_indent>(^\s*$\n|\A)(^(?:[ ]{4}|\t))(?<indent_contents>.*)(?<after_indent>[^\s].*$\n?(?:(?:^\s*$\n?)*^(?:[ ]{4}|\t).*[^\s].*$\n?)*))'
GITHUB_BLOCK = '^(?<fence>(?<fence_char>~|`){3,})\s*?(?<lang>\w+)?\s*?\n(?<contents>.*?)^\g<fence>\g<fence_char>*\s*?\n'
GITHUB_BLOCK = '^(?<fence>(?<fence_char>~|`){3,})\s*?(?<lang>\w+)?\s*?\n(?<contents>.*?)^\g<fence>\g<fence_char>*\s*?\n?'
CODEBLOCK_REGEX = /(#{GITHUB_BLOCK})/m
COMMAND_REGEX = ->(keyword) {
/^#{keyword}(?<tag>(\s|=|-|>)?(=|-|>)?)\s*(?<command>(\S)+)\s+(?<statement>.*)$/
Expand Down
22 changes: 22 additions & 0 deletions test/rundoc/regex_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -190,4 +190,26 @@ def test_complex_regex

assert_equal contents.strip, match.to_s.strip
end

def test_codeblock_optional_newline_regex
code_block_with_newline = <<~MD
hi
```
:::>> $ echo "hello"
```
MD
code_block_without_newline = code_block_with_newline.strip

expected = <<~MD.strip
```
:::>> $ echo "hello"
```
MD
regex = Rundoc::Parser::CODEBLOCK_REGEX
match = code_block_with_newline.match(regex)
assert_equal expected, match.to_s.strip

match = code_block_without_newline.match(regex)
assert_equal expected, match.to_s.strip
end
end

0 comments on commit 4a720a6

Please sign in to comment.