From 565b561522d5eaac29d1c8fd326018bad6ea3066 Mon Sep 17 00:00:00 2001 From: Peter Elliott Date: Fri, 10 Sep 2021 00:50:42 -0600 Subject: [PATCH] LibMarkdown: Render sequences of spaces properly in the terminal --- Userland/Libraries/LibMarkdown/Text.cpp | 14 ++++++++++---- Userland/Libraries/LibMarkdown/Text.h | 8 ++++++++ 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/Userland/Libraries/LibMarkdown/Text.cpp b/Userland/Libraries/LibMarkdown/Text.cpp index 71995fc7fb..ddc0901c07 100644 --- a/Userland/Libraries/LibMarkdown/Text.cpp +++ b/Userland/Libraries/LibMarkdown/Text.cpp @@ -79,13 +79,19 @@ void Text::TextNode::render_to_html(StringBuilder& builder) const void Text::TextNode::render_for_terminal(StringBuilder& builder) const { - String text_copy = text; - text_copy.replace("\n", " "); - builder.append(text_copy); + if (collapsible && (text == "\n" || text.is_whitespace())) { + builder.append(" "); + } else { + builder.append(text); + } } size_t Text::TextNode::terminal_length() const { + if (collapsible && text.is_whitespace()) { + return 1; + } + return text.length(); } @@ -445,7 +451,7 @@ NonnullOwnPtr Text::parse_code(Vector::ConstIterator& tokens) } is_all_whitespace = is_all_whitespace && iterator->data.is_whitespace(); - code->children.append(make((*iterator == "\n") ? " " : iterator->data)); + code->children.append(make((*iterator == "\n") ? " " : iterator->data, false)); } return make(opening.data); diff --git a/Userland/Libraries/LibMarkdown/Text.h b/Userland/Libraries/LibMarkdown/Text.h index d7715412cf..f67aa5a149 100644 --- a/Userland/Libraries/LibMarkdown/Text.h +++ b/Userland/Libraries/LibMarkdown/Text.h @@ -65,9 +65,17 @@ public: class TextNode : public Node { public: String text; + bool collapsible; TextNode(StringView const& text) : text(text) + , collapsible(true) + { + } + + TextNode(StringView const& text, bool collapsible) + : text(text) + , collapsible(collapsible) { }