diff options
author | Peter Elliott <pelliott@ualberta.ca> | 2022-04-25 17:48:13 -0600 |
---|---|---|
committer | Andreas Kling <kling@serenityos.org> | 2022-04-26 15:02:26 +0200 |
commit | e084e1ced6029490e319d2cd1c36b3e2aa35b912 (patch) | |
tree | fad62474fa7b08786f5eee124303a252468f82a5 /Userland/Libraries/LibMarkdown | |
parent | 4d3454d7003ccc5adf3dac844f8c7f89845726e3 (diff) | |
download | serenity-e084e1ced6029490e319d2cd1c36b3e2aa35b912.zip |
LibMarkdown: Better support for code fences
- Support tildes.
- Support different lengths of fence.
- Begrudgingly keep support for serenities one markdown extension.
Diffstat (limited to 'Userland/Libraries/LibMarkdown')
-rw-r--r-- | Userland/Libraries/LibMarkdown/CodeBlock.cpp | 26 |
1 files changed, 16 insertions, 10 deletions
diff --git a/Userland/Libraries/LibMarkdown/CodeBlock.cpp b/Userland/Libraries/LibMarkdown/CodeBlock.cpp index 53e8e420f5..a59a15a2aa 100644 --- a/Userland/Libraries/LibMarkdown/CodeBlock.cpp +++ b/Userland/Libraries/LibMarkdown/CodeBlock.cpp @@ -75,9 +75,8 @@ RecursionDecision CodeBlock::walk(Visitor& visitor) const return RecursionDecision::Continue; } -static Regex<ECMA262> style_spec_re("\\s*([\\*_]*)\\s*([^\\*_\\s]*).*"); - -static constexpr auto tick_tick_tick = "```"; +static Regex<ECMA262> open_fence_re("^ {0,3}(([\\`\\~])\\2{2,})\\s*([\\*_]*)\\s*([^\\*_\\s]*).*$"); +static Regex<ECMA262> close_fence_re("^ {0,3}(([\\`\\~])\\2{2,})\\s*$"); static Optional<int> line_block_prefix(StringView const& line) { @@ -111,7 +110,7 @@ OwnPtr<CodeBlock> CodeBlock::parse(LineIterator& lines) return {}; StringView line = *lines; - if (line.starts_with(tick_tick_tick)) + if (open_fence_re.match(line).success) return parse_backticks(lines); if (line_block_prefix(line).has_value()) @@ -135,10 +134,11 @@ OwnPtr<CodeBlock> CodeBlock::parse_backticks(LineIterator& lines) // The code block will be made bold, // and if possible syntax-highlighted // as appropriate for a shell script. - StringView style_spec = line.substring_view(3, line.length() - 3); - auto matches = style_spec_re.match(style_spec); - auto style = matches.capture_group_matches[0][0].view.string_view(); - auto language = matches.capture_group_matches[0][1].view.string_view(); + + auto matches = open_fence_re.match(line).capture_group_matches[0]; + auto fence = matches[0].view.string_view(); + auto style = matches[2].view.string_view(); + auto language = matches[3].view.string_view(); ++lines; @@ -150,8 +150,14 @@ OwnPtr<CodeBlock> CodeBlock::parse_backticks(LineIterator& lines) break; line = *lines; ++lines; - if (line == tick_tick_tick) - break; + + auto close_match = close_fence_re.match(line); + if (close_match.success) { + auto close_fence = close_match.capture_group_matches[0][0].view.string_view(); + if (close_fence[0] == fence[0] && close_fence.length() >= fence.length()) + break; + } + if (!first) builder.append('\n'); builder.append(line); |