From 0a21c2bace9797f835718bf4ea45fb09a5a2297a Mon Sep 17 00:00:00 2001 From: Peter Elliott Date: Tue, 28 Sep 2021 01:12:00 -0600 Subject: 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

tags, while paragraphs in a tight list are not.) --- Userland/Libraries/LibMarkdown/ContainerBlock.cpp | 25 +++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) (limited to 'Userland/Libraries/LibMarkdown/ContainerBlock.cpp') diff --git a/Userland/Libraries/LibMarkdown/ContainerBlock.cpp b/Userland/Libraries/LibMarkdown/ContainerBlock.cpp index 7585d635ee..9585264661 100644 --- a/Userland/Libraries/LibMarkdown/ContainerBlock.cpp +++ b/Userland/Libraries/LibMarkdown/ContainerBlock.cpp @@ -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(&block)) { + builder.append(s.substring_view(0, s.length() - 1)); + } else { + builder.append(s); + } + } + return builder.build(); } @@ -62,15 +73,21 @@ OwnPtr 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(lines, blocks) || try_parse_block(lines, blocks) || try_parse_block(lines, blocks) @@ -92,7 +109,7 @@ OwnPtr ContainerBlock::parse(LineIterator& lines) flush_paragraph(); - return make(move(blocks)); + return make(move(blocks), has_blank_lines, has_trailing_blank_lines); } } -- cgit v1.2.3