1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 12:17:44 +00:00

LibMarkdown: Better support for code fences

- Support tildes.
- Support different lengths of fence.
- Begrudgingly keep support for serenities one markdown extension.
This commit is contained in:
Peter Elliott 2022-04-25 17:48:13 -06:00 committed by Andreas Kling
parent 4d3454d700
commit e084e1ced6

View file

@ -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);