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

LibMarkdown: Render sequences of spaces properly in the terminal

This commit is contained in:
Peter Elliott 2021-09-10 00:50:42 -06:00 committed by Andreas Kling
parent 9c0563cc10
commit 565b561522
2 changed files with 18 additions and 4 deletions

View file

@ -79,13 +79,19 @@ void Text::TextNode::render_to_html(StringBuilder& builder) const
void Text::TextNode::render_for_terminal(StringBuilder& builder) const void Text::TextNode::render_for_terminal(StringBuilder& builder) const
{ {
String text_copy = text; if (collapsible && (text == "\n" || text.is_whitespace())) {
text_copy.replace("\n", " "); builder.append(" ");
builder.append(text_copy); } else {
builder.append(text);
}
} }
size_t Text::TextNode::terminal_length() const size_t Text::TextNode::terminal_length() const
{ {
if (collapsible && text.is_whitespace()) {
return 1;
}
return text.length(); return text.length();
} }
@ -445,7 +451,7 @@ NonnullOwnPtr<Text::Node> Text::parse_code(Vector<Token>::ConstIterator& tokens)
} }
is_all_whitespace = is_all_whitespace && iterator->data.is_whitespace(); is_all_whitespace = is_all_whitespace && iterator->data.is_whitespace();
code->children.append(make<TextNode>((*iterator == "\n") ? " " : iterator->data)); code->children.append(make<TextNode>((*iterator == "\n") ? " " : iterator->data, false));
} }
return make<TextNode>(opening.data); return make<TextNode>(opening.data);

View file

@ -65,9 +65,17 @@ public:
class TextNode : public Node { class TextNode : public Node {
public: public:
String text; String text;
bool collapsible;
TextNode(StringView const& text) TextNode(StringView const& text)
: text(text) : text(text)
, collapsible(true)
{
}
TextNode(StringView const& text, bool collapsible)
: text(text)
, collapsible(collapsible)
{ {
} }