LibMarkdown: Add start numbers for ordered lists

5. hey -> <ol start="5"><li>hey</li></ol>
This commit is contained in:
Peter Elliott 2021-09-29 23:19:56 -06:00 committed by Ali Mohammad Pur
parent a76a23e33b
commit 285038ebcf
2 changed files with 16 additions and 3 deletions

View file

@ -16,7 +16,12 @@ String List::render_to_html(bool) const
StringBuilder builder;
const char* tag = m_is_ordered ? "ol" : "ul";
builder.appendff("<{}>\n", tag);
builder.appendff("<{}", tag);
if (m_start_number != 1)
builder.appendff(" start=\"{}\"", m_start_number);
builder.append(">\n");
for (auto& item : m_items) {
builder.append("<li>");
@ -59,6 +64,7 @@ OwnPtr<List> List::parse(LineIterator& lines)
bool is_tight = true;
bool has_trailing_blank_lines = false;
size_t start_number = 1;
while (!lines.is_end()) {
@ -85,6 +91,11 @@ OwnPtr<List> List::parse(LineIterator& lines)
continue;
if (ch == '.' || ch == ')')
if (i + 1 < line.length() && line[i + 1] == ' ') {
auto maybe_start_number = line.substring_view(offset, i - offset).to_uint<size_t>();
if (!maybe_start_number.has_value())
break;
if (first)
start_number = maybe_start_number.value();
appears_ordered = true;
offset = i + 1;
}
@ -124,7 +135,7 @@ OwnPtr<List> List::parse(LineIterator& lines)
first = false;
}
return make<List>(move(items), is_ordered, is_tight);
return make<List>(move(items), is_ordered, is_tight, start_number);
}
}

View file

@ -15,10 +15,11 @@ namespace Markdown {
class List final : public Block {
public:
List(Vector<OwnPtr<ContainerBlock>> items, bool is_ordered, bool is_tight)
List(Vector<OwnPtr<ContainerBlock>> items, bool is_ordered, bool is_tight, size_t start_number)
: m_items(move(items))
, m_is_ordered(is_ordered)
, m_is_tight(is_tight)
, m_start_number(start_number)
{
}
virtual ~List() override { }
@ -32,6 +33,7 @@ private:
Vector<OwnPtr<ContainerBlock>> m_items;
bool m_is_ordered { false };
bool m_is_tight { false };
size_t m_start_number { 1 };
};
}