mirror of
https://github.com/RGBCube/serenity
synced 2025-05-31 20:28:11 +00:00
LibMarkdown: Implement "tightness" for lists
From the commonmark spec: A list is loose if any of its constituent list items are separated by blank lines, or if any of its constituent list items directly contain two block-level elements with a blank line between them. Otherwise a list is tight. (The difference in HTML output is that paragraphs in a loose list are wrapped in <p> tags, while paragraphs in a tight list are not.)
This commit is contained in:
parent
5bb87c630c
commit
0a21c2bace
15 changed files with 75 additions and 28 deletions
|
@ -14,15 +14,26 @@
|
|||
|
||||
namespace Markdown {
|
||||
|
||||
String ContainerBlock::render_to_html() const
|
||||
String ContainerBlock::render_to_html(bool tight) const
|
||||
{
|
||||
StringBuilder builder;
|
||||
|
||||
for (auto& block : m_blocks) {
|
||||
auto s = block.render_to_html();
|
||||
for (size_t i = 0; i + 1 < m_blocks.size(); ++i) {
|
||||
auto s = m_blocks[i].render_to_html(tight);
|
||||
builder.append(s);
|
||||
}
|
||||
|
||||
// I don't like this edge case.
|
||||
if (m_blocks.size() != 0) {
|
||||
auto& block = m_blocks[m_blocks.size() - 1];
|
||||
auto s = block.render_to_html(tight);
|
||||
if (tight && dynamic_cast<Paragraph const*>(&block)) {
|
||||
builder.append(s.substring_view(0, s.length() - 1));
|
||||
} else {
|
||||
builder.append(s);
|
||||
}
|
||||
}
|
||||
|
||||
return builder.build();
|
||||
}
|
||||
|
||||
|
@ -62,15 +73,21 @@ OwnPtr<ContainerBlock> ContainerBlock::parse(LineIterator& lines)
|
|||
paragraph_text.clear();
|
||||
};
|
||||
|
||||
bool has_blank_lines = false;
|
||||
bool has_trailing_blank_lines = false;
|
||||
|
||||
while (true) {
|
||||
if (lines.is_end())
|
||||
break;
|
||||
|
||||
if ((*lines).is_empty()) {
|
||||
has_trailing_blank_lines = true;
|
||||
++lines;
|
||||
|
||||
flush_paragraph();
|
||||
continue;
|
||||
} else {
|
||||
has_blank_lines = has_blank_lines || has_trailing_blank_lines;
|
||||
}
|
||||
|
||||
bool any = try_parse_block<Table>(lines, blocks) || try_parse_block<List>(lines, blocks) || try_parse_block<CodeBlock>(lines, blocks)
|
||||
|
@ -92,7 +109,7 @@ OwnPtr<ContainerBlock> ContainerBlock::parse(LineIterator& lines)
|
|||
|
||||
flush_paragraph();
|
||||
|
||||
return make<ContainerBlock>(move(blocks));
|
||||
return make<ContainerBlock>(move(blocks), has_blank_lines, has_trailing_blank_lines);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue