mirror of
https://github.com/RGBCube/serenity
synced 2025-07-26 07:17:35 +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:
parent
4d3454d700
commit
e084e1ced6
1 changed files with 16 additions and 10 deletions
|
@ -75,9 +75,8 @@ RecursionDecision CodeBlock::walk(Visitor& visitor) const
|
||||||
return RecursionDecision::Continue;
|
return RecursionDecision::Continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
static Regex<ECMA262> style_spec_re("\\s*([\\*_]*)\\s*([^\\*_\\s]*).*");
|
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 constexpr auto tick_tick_tick = "```";
|
|
||||||
|
|
||||||
static Optional<int> line_block_prefix(StringView const& line)
|
static Optional<int> line_block_prefix(StringView const& line)
|
||||||
{
|
{
|
||||||
|
@ -111,7 +110,7 @@ OwnPtr<CodeBlock> CodeBlock::parse(LineIterator& lines)
|
||||||
return {};
|
return {};
|
||||||
|
|
||||||
StringView line = *lines;
|
StringView line = *lines;
|
||||||
if (line.starts_with(tick_tick_tick))
|
if (open_fence_re.match(line).success)
|
||||||
return parse_backticks(lines);
|
return parse_backticks(lines);
|
||||||
|
|
||||||
if (line_block_prefix(line).has_value())
|
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,
|
// The code block will be made bold,
|
||||||
// and if possible syntax-highlighted
|
// and if possible syntax-highlighted
|
||||||
// as appropriate for a shell script.
|
// 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 matches = open_fence_re.match(line).capture_group_matches[0];
|
||||||
auto style = matches.capture_group_matches[0][0].view.string_view();
|
auto fence = matches[0].view.string_view();
|
||||||
auto language = matches.capture_group_matches[0][1].view.string_view();
|
auto style = matches[2].view.string_view();
|
||||||
|
auto language = matches[3].view.string_view();
|
||||||
|
|
||||||
++lines;
|
++lines;
|
||||||
|
|
||||||
|
@ -150,8 +150,14 @@ OwnPtr<CodeBlock> CodeBlock::parse_backticks(LineIterator& lines)
|
||||||
break;
|
break;
|
||||||
line = *lines;
|
line = *lines;
|
||||||
++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)
|
if (!first)
|
||||||
builder.append('\n');
|
builder.append('\n');
|
||||||
builder.append(line);
|
builder.append(line);
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue