1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 05:27:45 +00:00

LibMarkdown: Render lines to terminal instead of a single string

With this patch, the blocks in a markdown document render a vector of
lines. These lines get concatenated in Document::render_to_terminal, so
this does not change any external APIs of LibMarkdown.

This change makes it possible to indent individual lines in the rendered
markdown. So, rendering blockquotes in a similar way to code blocks :^)
This commit is contained in:
Arda Cinar 2022-12-23 12:25:00 +03:00 committed by Andreas Kling
parent 7a4b912ece
commit 5cc984d74c
20 changed files with 85 additions and 55 deletions

View file

@ -5,6 +5,7 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <AK/Forward.h>
#include <AK/StringBuilder.h>
#include <LibMarkdown/List.h>
#include <LibMarkdown/Paragraph.h>
@ -37,21 +38,36 @@ DeprecatedString List::render_to_html(bool) const
return builder.build();
}
DeprecatedString List::render_for_terminal(size_t) const
Vector<DeprecatedString> List::render_lines_for_terminal(size_t view_width) const
{
StringBuilder builder;
Vector<DeprecatedString> lines;
int i = 0;
for (auto& item : m_items) {
auto item_lines = item->render_lines_for_terminal(view_width);
auto first_line = item_lines.take_first();
StringBuilder builder;
builder.append(" "sv);
if (m_is_ordered)
builder.appendff("{}.", ++i);
else
builder.append('*');
builder.append(item->render_for_terminal());
auto item_indentation = builder.length();
builder.append(first_line);
lines.append(builder.build());
for (auto& line : item_lines) {
builder.clear();
builder.append(DeprecatedString::repeated(' ', item_indentation));
builder.append(line);
lines.append(builder.build());
}
}
return builder.build();
return lines;
}
RecursionDecision List::walk(Visitor& visitor) const