diff options
author | Peter Elliott <pelliott@ualberta.ca> | 2021-09-28 01:12:00 -0600 |
---|---|---|
committer | Ali Mohammad Pur <Ali.mpfard@gmail.com> | 2021-10-05 13:27:25 +0330 |
commit | 0a21c2bace9797f835718bf4ea45fb09a5a2297a (patch) | |
tree | a2056944cefc95c1753e08b542e205ffc8fe77d5 /Userland/Libraries/LibMarkdown/List.cpp | |
parent | 5bb87c630c5599309f29977512d7719ff6cae2eb (diff) | |
download | serenity-0a21c2bace9797f835718bf4ea45fb09a5a2297a.zip |
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.)
Diffstat (limited to 'Userland/Libraries/LibMarkdown/List.cpp')
-rw-r--r-- | Userland/Libraries/LibMarkdown/List.cpp | 25 |
1 files changed, 18 insertions, 7 deletions
diff --git a/Userland/Libraries/LibMarkdown/List.cpp b/Userland/Libraries/LibMarkdown/List.cpp index 393ff2899c..a75757b6e9 100644 --- a/Userland/Libraries/LibMarkdown/List.cpp +++ b/Userland/Libraries/LibMarkdown/List.cpp @@ -7,10 +7,11 @@ #include <AK/StringBuilder.h> #include <LibMarkdown/List.h> +#include <LibMarkdown/Paragraph.h> namespace Markdown { -String List::render_to_html() const +String List::render_to_html(bool) const { StringBuilder builder; @@ -18,8 +19,10 @@ String List::render_to_html() const builder.appendff("<{}>\n", tag); for (auto& item : m_items) { - builder.append("<li>\n"); - builder.append(item->render_to_html()); + builder.append("<li>"); + if (!m_is_tight || (item->blocks().size() != 0 && !dynamic_cast<Paragraph const*>(&(item->blocks()[0])))) + builder.append("\n"); + builder.append(item->render_to_html(m_is_tight)); builder.append("</li>\n"); } @@ -53,12 +56,15 @@ OwnPtr<List> List::parse(LineIterator& lines) bool first = true; bool is_ordered = false; + + bool is_tight = true; + bool has_trailing_blank_lines = false; + while (!lines.is_end()) { + size_t offset = 0; const StringView& line = *lines; - if (line.is_empty()) - break; bool appears_unordered = false; if (line.length() > 2) { @@ -98,18 +104,23 @@ OwnPtr<List> List::parse(LineIterator& lines) break; } + is_tight = is_tight && !has_trailing_blank_lines; + size_t saved_indent = lines.indent(); lines.set_indent(saved_indent + offset); lines.ignore_next_prefix(); - items.append(ContainerBlock::parse(lines)); + auto list_item = ContainerBlock::parse(lines); + is_tight = is_tight && !list_item->has_blank_lines(); + has_trailing_blank_lines = has_trailing_blank_lines || list_item->has_trailing_blank_lines(); + items.append(move(list_item)); lines.set_indent(saved_indent); first = false; } - return make<List>(move(items), is_ordered); + return make<List>(move(items), is_ordered, is_tight); } } |