refactor(tui): switch markdown renderer
This commit is contained in:
1 parent
07212aa130
commit
22d5da9f0d
3 files changed
+353
-69
No files matched your search
Generated
+98
-18
@@ -936,6 +936,12 @@ dependencies = [
|
|||||||
"syn 2.0.117",
|
"syn 2.0.117",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "diff"
|
||||||
|
version = "0.1.13"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "56254986775e3233ffa9c4d7d3faaf6d36a2c09d30b20687e9f88bc8bafc16c8"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "digest"
|
name = "digest"
|
||||||
version = "0.10.7"
|
version = "0.10.7"
|
||||||
@@ -1382,6 +1388,15 @@ dependencies = [
|
|||||||
"version_check",
|
"version_check",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "getopts"
|
||||||
|
version = "0.2.24"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "cfe4fbac503b8d1f88e6676011885f34b7174f46e59956bba534ba83abded4df"
|
||||||
|
dependencies = [
|
||||||
|
"unicode-width",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "getrandom"
|
name = "getrandom"
|
||||||
version = "0.2.17"
|
version = "0.2.17"
|
||||||
@@ -2017,6 +2032,15 @@ dependencies = [
|
|||||||
"either",
|
"either",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "itertools"
|
||||||
|
version = "0.15.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "8b4baf93f58d4425749ca49a51c50ebab072c5df6994d08fed93541c331481dc"
|
||||||
|
dependencies = [
|
||||||
|
"either",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "itoa"
|
name = "itoa"
|
||||||
version = "1.0.18"
|
version = "1.0.18"
|
||||||
@@ -2991,6 +3015,16 @@ version = "0.1.1"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "925383efa346730478fb4838dbe9137d2a47675ad789c546d150a6e1dd4ab31c"
|
checksum = "925383efa346730478fb4838dbe9137d2a47675ad789c546d150a6e1dd4ab31c"
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "pretty_assertions"
|
||||||
|
version = "1.4.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "3ae130e2f271fbc2ac3a40fb1d07180839cdbbe443c7a27e1e3c13c5cac0116d"
|
||||||
|
dependencies = [
|
||||||
|
"diff",
|
||||||
|
"yansi",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "prettyplease"
|
name = "prettyplease"
|
||||||
version = "0.2.37"
|
version = "0.2.37"
|
||||||
@@ -3048,10 +3082,18 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
|||||||
checksum = "e9f068eba8e7071c5f9511831b44f32c740d5adf574e990f946ddb53db2f314e"
|
checksum = "e9f068eba8e7071c5f9511831b44f32c740d5adf574e990f946ddb53db2f314e"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"bitflags 2.13.1",
|
"bitflags 2.13.1",
|
||||||
|
"getopts",
|
||||||
"memchr",
|
"memchr",
|
||||||
|
"pulldown-cmark-escape",
|
||||||
"unicase",
|
"unicase",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "pulldown-cmark-escape"
|
||||||
|
version = "0.11.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "007d8adb5ddab6f8e3f491ac63566a7d5002cc7ed73901f72057943fa71ae1ae"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "pxfm"
|
name = "pxfm"
|
||||||
version = "0.1.29"
|
version = "0.1.29"
|
||||||
@@ -3261,7 +3303,7 @@ dependencies = [
|
|||||||
"compact_str",
|
"compact_str",
|
||||||
"critical-section",
|
"critical-section",
|
||||||
"hashbrown 0.17.0",
|
"hashbrown 0.17.0",
|
||||||
"itertools",
|
"itertools 0.14.0",
|
||||||
"kasuari",
|
"kasuari",
|
||||||
"lru",
|
"lru",
|
||||||
"palette",
|
"palette",
|
||||||
@@ -3378,7 +3420,7 @@ dependencies = [
|
|||||||
"hashbrown 0.17.0",
|
"hashbrown 0.17.0",
|
||||||
"indoc",
|
"indoc",
|
||||||
"instability",
|
"instability",
|
||||||
"itertools",
|
"itertools 0.14.0",
|
||||||
"line-clipping",
|
"line-clipping",
|
||||||
"ratatui-core",
|
"ratatui-core",
|
||||||
"serde",
|
"serde",
|
||||||
@@ -3606,14 +3648,13 @@ dependencies = [
|
|||||||
"regex",
|
"regex",
|
||||||
"reqwest",
|
"reqwest",
|
||||||
"resvg",
|
"resvg",
|
||||||
"rstest",
|
"rstest 0.23.0",
|
||||||
"serde",
|
"serde",
|
||||||
"serde_json",
|
"serde_json",
|
||||||
"sha1 0.10.7",
|
"sha1 0.10.7",
|
||||||
"sha2",
|
"sha2",
|
||||||
"tachyonfx",
|
"tachyonfx",
|
||||||
"tempfile",
|
"tempfile",
|
||||||
"the-other-tui-markdown",
|
|
||||||
"thiserror 2.0.18",
|
"thiserror 2.0.18",
|
||||||
"throbber-widgets-tui",
|
"throbber-widgets-tui",
|
||||||
"tokio",
|
"tokio",
|
||||||
@@ -3622,6 +3663,7 @@ dependencies = [
|
|||||||
"tracing-appender",
|
"tracing-appender",
|
||||||
"tracing-subscriber",
|
"tracing-subscriber",
|
||||||
"tui-logger",
|
"tui-logger",
|
||||||
|
"tui-markdown",
|
||||||
"tui-popup",
|
"tui-popup",
|
||||||
"tui-prompts",
|
"tui-prompts",
|
||||||
"tui-scrollview",
|
"tui-scrollview",
|
||||||
@@ -3669,10 +3711,21 @@ checksum = "0a2c585be59b6b5dd66a9d2084aa1d8bd52fbdb806eafdeffb52791147862035"
|
|||||||
dependencies = [
|
dependencies = [
|
||||||
"futures",
|
"futures",
|
||||||
"futures-timer",
|
"futures-timer",
|
||||||
"rstest_macros",
|
"rstest_macros 0.23.0",
|
||||||
"rustc_version",
|
"rustc_version",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "rstest"
|
||||||
|
version = "0.26.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "f5a3193c063baaa2a95a33f03035c8a72b83d97a54916055ba22d35ed3839d49"
|
||||||
|
dependencies = [
|
||||||
|
"futures-timer",
|
||||||
|
"futures-util",
|
||||||
|
"rstest_macros 0.26.1",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "rstest_macros"
|
name = "rstest_macros"
|
||||||
version = "0.23.0"
|
version = "0.23.0"
|
||||||
@@ -3691,6 +3744,24 @@ dependencies = [
|
|||||||
"unicode-ident",
|
"unicode-ident",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "rstest_macros"
|
||||||
|
version = "0.26.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "9c845311f0ff7951c5506121a9ad75aec44d083c31583b2ea5a30bcb0b0abba0"
|
||||||
|
dependencies = [
|
||||||
|
"cfg-if",
|
||||||
|
"glob",
|
||||||
|
"proc-macro-crate",
|
||||||
|
"proc-macro2",
|
||||||
|
"quote",
|
||||||
|
"regex",
|
||||||
|
"relative-path",
|
||||||
|
"rustc_version",
|
||||||
|
"syn 2.0.117",
|
||||||
|
"unicode-ident",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "rust-ini"
|
name = "rust-ini"
|
||||||
version = "0.21.3"
|
version = "0.21.3"
|
||||||
@@ -4340,17 +4411,6 @@ dependencies = [
|
|||||||
"winapi",
|
"winapi",
|
||||||
]
|
]
|
||||||
|
|
||||||
[[package]]
|
|
||||||
name = "the-other-tui-markdown"
|
|
||||||
version = "0.1.0"
|
|
||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
|
||||||
checksum = "3d1e918c0bbc64d92da8a0016a03711ac2ab76042ae5f671523ff5ab45fa16c2"
|
|
||||||
dependencies = [
|
|
||||||
"pulldown-cmark",
|
|
||||||
"ratatui-core",
|
|
||||||
"unicode-width",
|
|
||||||
]
|
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "thiserror"
|
name = "thiserror"
|
||||||
version = "1.0.69"
|
version = "1.0.69"
|
||||||
@@ -4766,6 +4826,20 @@ dependencies = [
|
|||||||
"unicode-segmentation",
|
"unicode-segmentation",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "tui-markdown"
|
||||||
|
version = "0.3.9"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "a0c95afc7b66008823dea2614a800d944e18990690db47517efc66cd01ff9cee"
|
||||||
|
dependencies = [
|
||||||
|
"itertools 0.15.0",
|
||||||
|
"pretty_assertions",
|
||||||
|
"pulldown-cmark",
|
||||||
|
"ratatui-core",
|
||||||
|
"rstest 0.26.1",
|
||||||
|
"tracing",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "tui-popup"
|
name = "tui-popup"
|
||||||
version = "0.7.4"
|
version = "0.7.4"
|
||||||
@@ -4786,7 +4860,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
|
|||||||
checksum = "2237609b6d31db3635072e3c2a0a501ab546ce4bbdc96cf1a62c2e7ce0ac7e0f"
|
checksum = "2237609b6d31db3635072e3c2a0a501ab546ce4bbdc96cf1a62c2e7ce0ac7e0f"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"crossterm 0.29.0",
|
"crossterm 0.29.0",
|
||||||
"itertools",
|
"itertools 0.14.0",
|
||||||
"ratatui-core",
|
"ratatui-core",
|
||||||
"ratatui-macros",
|
"ratatui-macros",
|
||||||
"ratatui-widgets",
|
"ratatui-widgets",
|
||||||
@@ -4892,7 +4966,7 @@ version = "2.0.1"
|
|||||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
checksum = "16b380a1238663e5f8a691f9039c73e1cdae598a30e9855f541d29b08b53e9a5"
|
checksum = "16b380a1238663e5f8a691f9039c73e1cdae598a30e9855f541d29b08b53e9a5"
|
||||||
dependencies = [
|
dependencies = [
|
||||||
"itertools",
|
"itertools 0.14.0",
|
||||||
"unicode-segmentation",
|
"unicode-segmentation",
|
||||||
"unicode-width",
|
"unicode-width",
|
||||||
]
|
]
|
||||||
@@ -5801,6 +5875,12 @@ dependencies = [
|
|||||||
"hashlink",
|
"hashlink",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "yansi"
|
||||||
|
version = "1.0.1"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "cfe53a6657fd280eaa890a3bc59152892ffa3e30101319d168b781ed6529b049"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "yoke"
|
name = "yoke"
|
||||||
version = "0.8.2"
|
version = "0.8.2"
|
||||||
|
|||||||
+1
-1
@@ -34,7 +34,7 @@ log = "0.4"
|
|||||||
ratatui = { version = "0.30.0", features = ["serde", "unstable-rendered-line-info"] }
|
ratatui = { version = "0.30.0", features = ["serde", "unstable-rendered-line-info"] }
|
||||||
ratatui-themekit = { version = "0.6", features = ["serde"] }
|
ratatui-themekit = { version = "0.6", features = ["serde"] }
|
||||||
ratatui-textarea = { version = "0.9.1", features = ["crossterm"] }
|
ratatui-textarea = { version = "0.9.1", features = ["crossterm"] }
|
||||||
the-other-tui-markdown = "0.1.0"
|
tui-markdown = { version = "0.3.7", default-features = false }
|
||||||
pulldown-cmark = { version = "0.13.4", default-features = false }
|
pulldown-cmark = { version = "0.13.4", default-features = false }
|
||||||
html-to-markdown-rs = { version = "3.9.0", features = ["visitor"] }
|
html-to-markdown-rs = { version = "3.9.0", features = ["visitor"] }
|
||||||
resvg = "0.47"
|
resvg = "0.47"
|
||||||
|
|||||||
+254
-50
@@ -7,7 +7,7 @@ use html_to_markdown_rs::{
|
|||||||
visitor::{HtmlVisitor, NodeContext, VisitResult, VisitorHandle},
|
visitor::{HtmlVisitor, NodeContext, VisitResult, VisitorHandle},
|
||||||
};
|
};
|
||||||
use image::DynamicImage;
|
use image::DynamicImage;
|
||||||
use pulldown_cmark::{Event, Options as MarkdownOptions, Parser, Tag, TagEnd};
|
use pulldown_cmark::{CodeBlockKind, Event, Options as MarkdownOptions, Parser, Tag, TagEnd};
|
||||||
use ratatui::{
|
use ratatui::{
|
||||||
Frame,
|
Frame,
|
||||||
layout::{Alignment, Rect, Size},
|
layout::{Alignment, Rect, Size},
|
||||||
@@ -20,7 +20,7 @@ use ratatui_image::{
|
|||||||
picker::ProtocolType,
|
picker::ProtocolType,
|
||||||
sliced::{SignedPosition, SlicedImage, SlicedProtocol},
|
sliced::{SignedPosition, SlicedImage, SlicedProtocol},
|
||||||
};
|
};
|
||||||
use the_other_tui_markdown::{RendererBuilder, Theme as MarkdownTheme, into_text_with_renderer};
|
use tui_markdown::{Options as TuiMarkdownOptions, StyleSheet, from_str_with_options};
|
||||||
use unicode_segmentation::UnicodeSegmentation;
|
use unicode_segmentation::UnicodeSegmentation;
|
||||||
|
|
||||||
use crate::config::settings::ImageProtocol;
|
use crate::config::settings::ImageProtocol;
|
||||||
@@ -570,23 +570,28 @@ fn push_text_block(blocks: &mut Vec<DocumentBlock>, source: &str) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn external_markdown_text(source: &str, width: u16) -> (Text<'static>, Vec<LinkTarget>) {
|
fn external_markdown_text(source: &str, width: u16) -> (Text<'static>, Vec<LinkTarget>) {
|
||||||
let theme = markdown_theme();
|
let styles = MarkdownStyleSheet;
|
||||||
let rule_style = theme.rule;
|
let rule_style = styles.table_border();
|
||||||
let list_marker_style = theme.list_marker;
|
let list_marker_style = Style::default().fg(THEME.as_ref().text_dim());
|
||||||
let table_separator_style = theme.table_separator;
|
let table_separator_style = styles.table_border();
|
||||||
let code_style = theme.code_block;
|
let code_style = styles.code();
|
||||||
let code_language_style = theme.code_block_lang;
|
let code_language_style = Style::default()
|
||||||
|
.fg(THEME.as_ref().text_dim())
|
||||||
|
.bg(THEME.as_ref().surface());
|
||||||
let links = markdown_links(source);
|
let links = markdown_links(source);
|
||||||
let renderer = RendererBuilder::new()
|
let options = TuiMarkdownOptions::new(styles);
|
||||||
.with_theme(theme)
|
let mut text = own_text(from_str_with_options(source, &options));
|
||||||
.with_heading(|_, spans| vec![Line::from(spans)])
|
text.lines = render_code_blocks(
|
||||||
.with_rule(move || vec![Line::styled("─".repeat(usize::from(width)), rule_style)])
|
text.lines,
|
||||||
.with_code_block(move |language, content| {
|
markdown_code_languages(source),
|
||||||
code_block_lines(language, content, width, code_style, code_language_style)
|
width,
|
||||||
})
|
code_style,
|
||||||
.build();
|
code_language_style,
|
||||||
let mut text = into_text_with_renderer(source, &renderer);
|
);
|
||||||
|
text.lines = unbox_tables(text.lines);
|
||||||
for line in &mut text.lines {
|
for line in &mut text.lines {
|
||||||
|
let line_style = Style::default().fg(THEME.as_ref().text()).patch(line.style);
|
||||||
|
line.style = line_style;
|
||||||
line.spans.retain(|span| {
|
line.spans.retain(|span| {
|
||||||
let content = span.content.as_ref();
|
let content = span.content.as_ref();
|
||||||
content != "*"
|
content != "*"
|
||||||
@@ -594,7 +599,9 @@ fn external_markdown_text(source: &str, width: u16) -> (Text<'static>, Vec<LinkT
|
|||||||
&& content.starts_with("(http")
|
&& content.starts_with("(http")
|
||||||
&& content.ends_with(')'))
|
&& content.ends_with(')'))
|
||||||
});
|
});
|
||||||
|
remove_link_destination(line);
|
||||||
for span in &mut line.spans {
|
for span in &mut line.spans {
|
||||||
|
span.style = line_style.patch(span.style);
|
||||||
if span.content.contains("**") {
|
if span.content.contains("**") {
|
||||||
span.content = span.content.replace("**", "").into();
|
span.content = span.content.replace("**", "").into();
|
||||||
}
|
}
|
||||||
@@ -602,12 +609,158 @@ fn external_markdown_text(source: &str, width: u16) -> (Text<'static>, Vec<LinkT
|
|||||||
}
|
}
|
||||||
text.lines = wrap_tables(text.lines, width, table_separator_style)
|
text.lines = wrap_tables(text.lines, width, table_separator_style)
|
||||||
.into_iter()
|
.into_iter()
|
||||||
|
.map(|line| {
|
||||||
|
if line.to_string() == "---" {
|
||||||
|
Line::styled("─".repeat(usize::from(width)), rule_style)
|
||||||
|
} else {
|
||||||
|
line
|
||||||
|
}
|
||||||
|
})
|
||||||
.flat_map(|line| wrap_list_line(line, width, list_marker_style))
|
.flat_map(|line| wrap_list_line(line, width, list_marker_style))
|
||||||
.flat_map(|line| wrap_line(line, width))
|
.flat_map(|line| wrap_line(line, width))
|
||||||
.collect();
|
.collect();
|
||||||
(text, links)
|
(text, links)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn markdown_code_languages(source: &str) -> Vec<String> {
|
||||||
|
Parser::new_ext(source, markdown_options())
|
||||||
|
.filter_map(|event| match event {
|
||||||
|
Event::Start(Tag::CodeBlock(CodeBlockKind::Fenced(language))) => {
|
||||||
|
Some(language.into_string())
|
||||||
|
}
|
||||||
|
Event::Start(Tag::CodeBlock(CodeBlockKind::Indented)) => Some(String::new()),
|
||||||
|
_ => None,
|
||||||
|
})
|
||||||
|
.collect()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn render_code_blocks(
|
||||||
|
lines: Vec<Line<'static>>,
|
||||||
|
languages: Vec<String>,
|
||||||
|
width: u16,
|
||||||
|
code_style: Style,
|
||||||
|
language_style: Style,
|
||||||
|
) -> Vec<Line<'static>> {
|
||||||
|
let mut output = Vec::new();
|
||||||
|
let mut lines = lines.into_iter().peekable();
|
||||||
|
let mut languages = languages.into_iter();
|
||||||
|
while let Some(line) = lines.next() {
|
||||||
|
if line.style != code_style {
|
||||||
|
output.push(line);
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
let mut content = vec![line.to_string()];
|
||||||
|
while lines.peek().is_some_and(|line| line.style == code_style) {
|
||||||
|
content.push(lines.next().expect("peeked code line").to_string());
|
||||||
|
}
|
||||||
|
output.extend(code_block_lines(
|
||||||
|
&languages.next().unwrap_or_default(),
|
||||||
|
&content.join("\n"),
|
||||||
|
width,
|
||||||
|
code_style,
|
||||||
|
language_style,
|
||||||
|
));
|
||||||
|
}
|
||||||
|
output
|
||||||
|
}
|
||||||
|
|
||||||
|
fn unbox_tables(lines: Vec<Line<'static>>) -> Vec<Line<'static>> {
|
||||||
|
lines
|
||||||
|
.into_iter()
|
||||||
|
.filter_map(|mut line| {
|
||||||
|
let text = line.to_string();
|
||||||
|
if is_table_border(&text, '┌', '┬', '┐') || is_table_border(&text, '└', '┴', '┘')
|
||||||
|
{
|
||||||
|
return None;
|
||||||
|
}
|
||||||
|
if is_table_border(&text, '├', '┼', '┤') {
|
||||||
|
strip_outer_chars(&mut line, '├', '┤');
|
||||||
|
} else if text.starts_with('│') && text.ends_with('│') {
|
||||||
|
strip_outer_chars(&mut line, '│', '│');
|
||||||
|
}
|
||||||
|
Some(line)
|
||||||
|
})
|
||||||
|
.collect()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn is_table_border(text: &str, left: char, middle: char, right: char) -> bool {
|
||||||
|
text.starts_with(left)
|
||||||
|
&& text.ends_with(right)
|
||||||
|
&& text.chars().all(|character| {
|
||||||
|
matches!(character, '─')
|
||||||
|
|| character == left
|
||||||
|
|| character == middle
|
||||||
|
|| character == right
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
fn strip_outer_chars(line: &mut Line<'static>, left: char, right: char) {
|
||||||
|
if let Some(span) = line.spans.iter_mut().find(|span| !span.content.is_empty()) {
|
||||||
|
span.content = span.content.trim_start_matches(left).to_owned().into();
|
||||||
|
}
|
||||||
|
if let Some(span) = line
|
||||||
|
.spans
|
||||||
|
.iter_mut()
|
||||||
|
.rev()
|
||||||
|
.find(|span| !span.content.is_empty())
|
||||||
|
{
|
||||||
|
span.content = span.content.trim_end_matches(right).to_owned().into();
|
||||||
|
}
|
||||||
|
line.spans.retain(|span| !span.content.is_empty());
|
||||||
|
}
|
||||||
|
|
||||||
|
fn remove_link_destination(line: &mut Line<'static>) {
|
||||||
|
let spans = std::mem::take(&mut line.spans);
|
||||||
|
let mut output = Vec::with_capacity(spans.len());
|
||||||
|
let mut index = 0;
|
||||||
|
while index < spans.len() {
|
||||||
|
if index + 2 < spans.len()
|
||||||
|
&& spans[index].content.ends_with(" (")
|
||||||
|
&& spans[index + 1]
|
||||||
|
.style
|
||||||
|
.add_modifier
|
||||||
|
.contains(Modifier::UNDERLINED)
|
||||||
|
&& spans[index + 2].content.starts_with(')')
|
||||||
|
{
|
||||||
|
let mut before = spans[index].clone();
|
||||||
|
before.content = before.content.trim_end_matches(" (").to_owned().into();
|
||||||
|
if !before.content.is_empty() {
|
||||||
|
output.push(before);
|
||||||
|
}
|
||||||
|
let mut after = spans[index + 2].clone();
|
||||||
|
after.content = after.content.trim_start_matches(')').to_owned().into();
|
||||||
|
if !after.content.is_empty() {
|
||||||
|
output.push(after);
|
||||||
|
}
|
||||||
|
index += 3;
|
||||||
|
} else {
|
||||||
|
output.push(spans[index].clone());
|
||||||
|
index += 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
line.spans = output;
|
||||||
|
}
|
||||||
|
|
||||||
|
fn own_text(text: Text<'_>) -> Text<'static> {
|
||||||
|
Text {
|
||||||
|
lines: text
|
||||||
|
.lines
|
||||||
|
.into_iter()
|
||||||
|
.map(|line| Line {
|
||||||
|
spans: line
|
||||||
|
.spans
|
||||||
|
.into_iter()
|
||||||
|
.map(|span| Span::styled(span.content.into_owned(), span.style))
|
||||||
|
.collect(),
|
||||||
|
style: line.style,
|
||||||
|
alignment: line.alignment,
|
||||||
|
})
|
||||||
|
.collect(),
|
||||||
|
style: text.style,
|
||||||
|
alignment: text.alignment,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn markdown_links(source: &str) -> Vec<LinkTarget> {
|
fn markdown_links(source: &str) -> Vec<LinkTarget> {
|
||||||
let mut links = Vec::new();
|
let mut links = Vec::new();
|
||||||
let mut current = None::<(String, String)>;
|
let mut current = None::<(String, String)>;
|
||||||
@@ -702,11 +855,24 @@ fn table_cells(
|
|||||||
return None;
|
return None;
|
||||||
}
|
}
|
||||||
for cell in &mut cells {
|
for cell in &mut cells {
|
||||||
|
trim_span_start(cell);
|
||||||
trim_span_end(cell);
|
trim_span_end(cell);
|
||||||
}
|
}
|
||||||
Some(cells)
|
Some(cells)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn trim_span_start(spans: &mut Vec<Span<'static>>) {
|
||||||
|
while let Some(first) = spans.first_mut() {
|
||||||
|
let trimmed = first.content.trim_start_matches(char::is_whitespace);
|
||||||
|
if trimmed.is_empty() {
|
||||||
|
spans.remove(0);
|
||||||
|
} else {
|
||||||
|
first.content = trimmed.to_owned().into();
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
fn trim_span_end(spans: &mut Vec<Span<'static>>) {
|
fn trim_span_end(spans: &mut Vec<Span<'static>>) {
|
||||||
while let Some(last) = spans.last_mut() {
|
while let Some(last) = spans.last_mut() {
|
||||||
let trimmed = last.content.trim_end_matches(char::is_whitespace);
|
let trimmed = last.content.trim_end_matches(char::is_whitespace);
|
||||||
@@ -909,11 +1075,29 @@ fn wrap_list_line(line: Line<'static>, width: u16, marker_style: Style) -> Vec<L
|
|||||||
let Some(marker) = line
|
let Some(marker) = line
|
||||||
.spans
|
.spans
|
||||||
.first()
|
.first()
|
||||||
.filter(|span| span.style == marker_style && is_list_marker(&span.content))
|
.filter(|span| is_list_marker(&span.content))
|
||||||
else {
|
else {
|
||||||
return vec![line];
|
return vec![line];
|
||||||
};
|
};
|
||||||
let marker = format!(" {}", marker.content);
|
let leading_spaces = marker
|
||||||
|
.content
|
||||||
|
.chars()
|
||||||
|
.take_while(|character| character.is_whitespace())
|
||||||
|
.count();
|
||||||
|
let indent = if leading_spaces == 0 {
|
||||||
|
2
|
||||||
|
} else {
|
||||||
|
leading_spaces
|
||||||
|
};
|
||||||
|
let marker = format!(
|
||||||
|
"{}{} ",
|
||||||
|
" ".repeat(indent),
|
||||||
|
if marker.content.trim() == "-" {
|
||||||
|
"•"
|
||||||
|
} else {
|
||||||
|
marker.content.trim()
|
||||||
|
}
|
||||||
|
);
|
||||||
let marker_width = Span::raw(&marker).width();
|
let marker_width = Span::raw(&marker).width();
|
||||||
let content_width = usize::from(width).saturating_sub(marker_width).max(1);
|
let content_width = usize::from(width).saturating_sub(marker_width).max(1);
|
||||||
let wrapped = wrap_styled_spans(&line.spans[1..], content_width);
|
let wrapped = wrap_styled_spans(&line.spans[1..], content_width);
|
||||||
@@ -944,7 +1128,8 @@ fn wrap_list_line(line: Line<'static>, width: u16, marker_style: Style) -> Vec<L
|
|||||||
|
|
||||||
fn is_list_marker(marker: &str) -> bool {
|
fn is_list_marker(marker: &str) -> bool {
|
||||||
let marker = marker.trim();
|
let marker = marker.trim();
|
||||||
marker == "•"
|
marker == "-"
|
||||||
|
|| marker == "•"
|
||||||
|| marker
|
|| marker
|
||||||
.strip_suffix('.')
|
.strip_suffix('.')
|
||||||
.is_some_and(|number| !number.is_empty() && number.chars().all(|c| c.is_ascii_digit()))
|
.is_some_and(|number| !number.is_empty() && number.chars().all(|c| c.is_ascii_digit()))
|
||||||
@@ -1015,36 +1200,55 @@ fn push_styled(spans: &mut Vec<Span<'static>>, content: &str, style: Style) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn markdown_theme() -> MarkdownTheme {
|
#[derive(Clone, Copy)]
|
||||||
let mut theme = MarkdownTheme::default();
|
struct MarkdownStyleSheet;
|
||||||
let heading = Style::default()
|
|
||||||
.fg(THEME.as_ref().accent())
|
impl StyleSheet for MarkdownStyleSheet {
|
||||||
.add_modifier(Modifier::BOLD);
|
fn heading(&self, _level: u8) -> Style {
|
||||||
theme.base = Style::default().fg(THEME.as_ref().text());
|
Style::default()
|
||||||
theme.h1 = heading;
|
.fg(THEME.as_ref().accent())
|
||||||
theme.h2 = heading;
|
.add_modifier(Modifier::BOLD)
|
||||||
theme.h3 = heading;
|
}
|
||||||
theme.h4 = heading;
|
|
||||||
theme.h5 = heading;
|
fn code(&self) -> Style {
|
||||||
theme.h6 = heading;
|
Style::default()
|
||||||
theme.inline_code = Style::default()
|
.fg(THEME.as_ref().text())
|
||||||
.fg(THEME.as_ref().text())
|
.bg(THEME.as_ref().surface())
|
||||||
.bg(THEME.as_ref().surface());
|
}
|
||||||
theme.code_block = theme.inline_code;
|
|
||||||
theme.code_block_lang = Style::default()
|
fn link(&self) -> Style {
|
||||||
.fg(THEME.as_ref().text_dim())
|
Style::default()
|
||||||
.bg(THEME.as_ref().surface());
|
.fg(THEME.as_ref().info())
|
||||||
theme.link = Style::default()
|
.add_modifier(Modifier::UNDERLINED)
|
||||||
.fg(THEME.as_ref().info())
|
}
|
||||||
.add_modifier(Modifier::UNDERLINED);
|
|
||||||
theme.block_quote = Style::default().fg(THEME.as_ref().text_dim());
|
fn blockquote(&self) -> Style {
|
||||||
theme.list_marker = Style::default().fg(THEME.as_ref().text_dim());
|
Style::default().fg(THEME.as_ref().text_dim())
|
||||||
theme.table_header = heading;
|
}
|
||||||
theme.table_cell = Style::default().fg(THEME.as_ref().text());
|
|
||||||
theme.table_separator = Style::default().fg(THEME.as_ref().border());
|
fn heading_marker(&self, _level: u8) -> &str {
|
||||||
theme.rule = theme.table_separator;
|
""
|
||||||
theme.html = Style::default().fg(THEME.as_ref().text_dim());
|
}
|
||||||
theme
|
|
||||||
|
fn code_block_fence(&self) -> &str {
|
||||||
|
""
|
||||||
|
}
|
||||||
|
|
||||||
|
fn html(&self) -> Style {
|
||||||
|
Style::default().fg(THEME.as_ref().text_dim())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn table_header(&self) -> Style {
|
||||||
|
self.heading(1)
|
||||||
|
}
|
||||||
|
|
||||||
|
fn table_cell(&self) -> Style {
|
||||||
|
Style::default().fg(THEME.as_ref().text())
|
||||||
|
}
|
||||||
|
|
||||||
|
fn table_border(&self) -> Style {
|
||||||
|
Style::default().fg(THEME.as_ref().border())
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl TextBlock {
|
impl TextBlock {
|
||||||
|
|||||||
Reference in new issue
Block a user