1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-25 13:17:35 +00:00

LibMarkdown: Flush the pending paragraphs when encountering empty lines

Fixes #3854.
This commit is contained in:
AnotherTest 2020-10-26 05:22:07 +03:30 committed by Andreas Kling
parent 63a94deb43
commit af05671843

View file

@ -83,12 +83,20 @@ OwnPtr<Document> Document::parse(const StringView& str)
auto& blocks = document->m_blocks; auto& blocks = document->m_blocks;
NonnullOwnPtrVector<Paragraph::Line> paragraph_lines; NonnullOwnPtrVector<Paragraph::Line> paragraph_lines;
auto flush_paragraph = [&] {
if (paragraph_lines.is_empty())
return;
auto paragraph = make<Paragraph>(move(paragraph_lines));
document->m_blocks.append(move(paragraph));
paragraph_lines.clear();
};
while (true) { while (true) {
if (lines.is_end()) if (lines.is_end())
break; break;
if ((*lines).is_empty()) { if ((*lines).is_empty()) {
++lines; ++lines;
flush_paragraph();
continue; continue;
} }
@ -98,10 +106,8 @@ OwnPtr<Document> Document::parse(const StringView& str)
if (any) { if (any) {
if (!paragraph_lines.is_empty()) { if (!paragraph_lines.is_empty()) {
auto last_block = document->m_blocks.take_last(); auto last_block = document->m_blocks.take_last();
auto paragraph = make<Paragraph>(move(paragraph_lines)); flush_paragraph();
document->m_blocks.append(move(paragraph));
document->m_blocks.append(move(last_block)); document->m_blocks.append(move(last_block));
paragraph_lines.clear();
} }
continue; continue;
} }
@ -113,10 +119,8 @@ OwnPtr<Document> Document::parse(const StringView& str)
paragraph_lines.append(line.release_nonnull()); paragraph_lines.append(line.release_nonnull());
} }
if (!paragraph_lines.is_empty()) { if (!paragraph_lines.is_empty())
auto paragraph = make<Paragraph>(move(paragraph_lines)); flush_paragraph();
document->m_blocks.append(move(paragraph));
}
return document; return document;
} }