mirror of
https://github.com/LadybirdBrowser/ladybird
synced 2026-05-05 06:32:30 +02: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:
committed by
Andreas Kling
parent
7a4b912ece
commit
5cc984d74c
Notes:
sideshowbarker
2024-07-17 02:24:10 +09:00
Author: https://github.com/kuzux Commit: https://github.com/SerenityOS/serenity/commit/5cc984d74c Pull-request: https://github.com/SerenityOS/serenity/pull/16633 Reviewed-by: https://github.com/awesomekling Reviewed-by: https://github.com/kleinesfilmroellchen ✅
@@ -5,6 +5,7 @@
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include <AK/Forward.h>
|
||||
#include <AK/StringBuilder.h>
|
||||
#include <LibJS/MarkupGenerator.h>
|
||||
#include <LibMarkdown/CodeBlock.h>
|
||||
@@ -53,22 +54,23 @@ DeprecatedString CodeBlock::render_to_html(bool) const
|
||||
return builder.build();
|
||||
}
|
||||
|
||||
DeprecatedString CodeBlock::render_for_terminal(size_t) const
|
||||
Vector<DeprecatedString> CodeBlock::render_lines_for_terminal(size_t) const
|
||||
{
|
||||
StringBuilder builder;
|
||||
Vector<DeprecatedString> lines;
|
||||
|
||||
for (auto const& line : m_code.split('\n')) {
|
||||
// Do not indent too much if we are in the synopsis
|
||||
if (!(m_current_section && m_current_section->render_for_terminal().contains("SYNOPSIS"sv)))
|
||||
builder.append(" "sv);
|
||||
|
||||
builder.append(" "sv);
|
||||
builder.append(line);
|
||||
builder.append("\n"sv);
|
||||
// Do not indent too much if we are in the synopsis
|
||||
auto indentation = " "sv;
|
||||
if (m_current_section != nullptr) {
|
||||
auto current_section_name = m_current_section->render_lines_for_terminal()[0];
|
||||
if (current_section_name.contains("SYNOPSIS"sv))
|
||||
indentation = " "sv;
|
||||
}
|
||||
builder.append("\n"sv);
|
||||
|
||||
return builder.build();
|
||||
for (auto const& line : m_code.split('\n'))
|
||||
lines.append(DeprecatedString::formatted("{}{}", indentation, line));
|
||||
lines.append("");
|
||||
|
||||
return lines;
|
||||
}
|
||||
|
||||
RecursionDecision CodeBlock::walk(Visitor& visitor) const
|
||||
|
||||
Reference in New Issue
Block a user