1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 18:47:34 +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:
Peter Elliott 2021-09-28 01:12:00 -06:00 committed by Ali Mohammad Pur
parent 5bb87c630c
commit 0a21c2bace
15 changed files with 75 additions and 28 deletions

View file

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