fix(markdown): harden Modrinth page rendering

This commit is contained in:
objz committed 2026-07-26 18:14:07 +02:00
1 parent 77980dbb4d
commit aaa9224c7c
3 files changed
+147 -218

No files matched your search

Generated
+18 -98
View File
@@ -936,12 +936,6 @@ dependencies = [
"syn 2.0.117",
]
[[package]]
name = "diff"
version = "0.1.13"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "56254986775e3233ffa9c4d7d3faaf6d36a2c09d30b20687e9f88bc8bafc16c8"
[[package]]
name = "digest"
version = "0.10.7"
@@ -1388,15 +1382,6 @@ dependencies = [
"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]]
name = "getrandom"
version = "0.2.17"
@@ -2032,15 +2017,6 @@ dependencies = [
"either",
]
[[package]]
name = "itertools"
version = "0.15.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8b4baf93f58d4425749ca49a51c50ebab072c5df6994d08fed93541c331481dc"
dependencies = [
"either",
]
[[package]]
name = "itoa"
version = "1.0.18"
@@ -3015,16 +2991,6 @@ version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
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]]
name = "prettyplease"
version = "0.2.37"
@@ -3082,18 +3048,10 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e9f068eba8e7071c5f9511831b44f32c740d5adf574e990f946ddb53db2f314e"
dependencies = [
"bitflags 2.13.1",
"getopts",
"memchr",
"pulldown-cmark-escape",
"unicase",
]
[[package]]
name = "pulldown-cmark-escape"
version = "0.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "007d8adb5ddab6f8e3f491ac63566a7d5002cc7ed73901f72057943fa71ae1ae"
[[package]]
name = "pxfm"
version = "0.1.29"
@@ -3303,7 +3261,7 @@ dependencies = [
"compact_str",
"critical-section",
"hashbrown 0.17.0",
"itertools 0.14.0",
"itertools",
"kasuari",
"lru",
"palette",
@@ -3420,7 +3378,7 @@ dependencies = [
"hashbrown 0.17.0",
"indoc",
"instability",
"itertools 0.14.0",
"itertools",
"line-clipping",
"ratatui-core",
"serde",
@@ -3648,13 +3606,14 @@ dependencies = [
"regex",
"reqwest",
"resvg",
"rstest 0.23.0",
"rstest",
"serde",
"serde_json",
"sha1 0.10.7",
"sha2",
"tachyonfx",
"tempfile",
"the-other-tui-markdown",
"thiserror 2.0.18",
"throbber-widgets-tui",
"tokio",
@@ -3663,7 +3622,6 @@ dependencies = [
"tracing-appender",
"tracing-subscriber",
"tui-logger",
"tui-markdown",
"tui-popup",
"tui-prompts",
"tui-scrollview",
@@ -3710,21 +3668,10 @@ checksum = "0a2c585be59b6b5dd66a9d2084aa1d8bd52fbdb806eafdeffb52791147862035"
dependencies = [
"futures",
"futures-timer",
"rstest_macros 0.23.0",
"rstest_macros",
"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]]
name = "rstest_macros"
version = "0.23.0"
@@ -3743,24 +3690,6 @@ dependencies = [
"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]]
name = "rust-ini"
version = "0.21.3"
@@ -4410,6 +4339,17 @@ dependencies = [
"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]]
name = "thiserror"
version = "1.0.69"
@@ -4825,20 +4765,6 @@ dependencies = [
"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]]
name = "tui-popup"
version = "0.7.4"
@@ -4859,7 +4785,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2237609b6d31db3635072e3c2a0a501ab546ce4bbdc96cf1a62c2e7ce0ac7e0f"
dependencies = [
"crossterm 0.29.0",
"itertools 0.14.0",
"itertools",
"ratatui-core",
"ratatui-macros",
"ratatui-widgets",
@@ -4965,7 +4891,7 @@ version = "2.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "16b380a1238663e5f8a691f9039c73e1cdae598a30e9855f541d29b08b53e9a5"
dependencies = [
"itertools 0.14.0",
"itertools",
"unicode-segmentation",
"unicode-width",
]
@@ -5874,12 +5800,6 @@ dependencies = [
"hashlink",
]
[[package]]
name = "yansi"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cfe53a6657fd280eaa890a3bc59152892ffa3e30101319d168b781ed6529b049"
[[package]]
name = "yoke"
version = "0.8.2"
+1 -1
View File
@@ -33,7 +33,7 @@ log = "0.4"
ratatui = { version = "0.30.0", features = ["serde", "unstable-rendered-line-info"] }
ratatui-themekit = { version = "0.6", features = ["serde"] }
ratatui-textarea = { version = "0.9.1", features = ["crossterm"] }
tui-markdown = { version = "0.3.9", default-features = false }
the-other-tui-markdown = "0.1.0"
pulldown-cmark = { version = "0.13.4", default-features = false }
html-to-markdown-rs = "3.9.0"
resvg = "0.47"
+128 -119
View File
@@ -17,6 +17,7 @@ use ratatui_image::{
picker::ProtocolType,
sliced::{SignedPosition, SlicedImage, SlicedProtocol},
};
use the_other_tui_markdown::{RendererBuilder, Theme as MarkdownTheme, into_text_with_renderer};
use crate::config::settings::ImageProtocol;
use crate::config::theme::THEME;
@@ -24,68 +25,6 @@ use crate::instance::content::mods::{
IconCell, fallback_icon, make_icon_pixels_from_image, make_icon_quadrants_from_image,
};
#[derive(Clone)]
struct RmclMarkdownStyle;
impl tui_markdown::StyleSheet for RmclMarkdownStyle {
fn heading(&self, level: u8) -> Style {
let _ = level;
Style::default()
.fg(THEME.as_ref().accent())
.add_modifier(Modifier::BOLD)
}
fn code(&self) -> Style {
Style::default()
.fg(THEME.as_ref().text())
.bg(THEME.as_ref().surface())
}
fn link(&self) -> Style {
Style::default()
.fg(THEME.as_ref().info())
.add_modifier(Modifier::UNDERLINED)
}
fn blockquote(&self) -> Style {
Style::default().fg(THEME.as_ref().text_dim())
}
fn heading_meta(&self) -> Style {
Style::default().fg(THEME.as_ref().text_dim())
}
fn metadata_block(&self) -> Style {
Style::default().fg(THEME.as_ref().text_dim())
}
fn heading_marker(&self, _level: u8) -> &str {
""
}
fn code_block_fence(&self) -> &str {
""
}
fn html(&self) -> Style {
Style::default().fg(THEME.as_ref().text_dim())
}
fn table_header(&self) -> Style {
Style::default()
.fg(THEME.as_ref().accent())
.add_modifier(Modifier::BOLD)
}
fn table_cell(&self) -> Style {
Style::default().fg(THEME.as_ref().text())
}
fn table_border(&self) -> Style {
Style::default().fg(THEME.as_ref().border())
}
}
struct TextBlock {
source: String,
rendered: Option<TextRender>,
@@ -281,7 +220,7 @@ struct NormalizedDocument {
fn normalize_html(source: &str) -> NormalizedDocument {
let mut regions = Vec::new();
let mut html_block_start = None;
let mut paragraph = None;
let mut inline_container = None;
for (event, range) in Parser::new_ext(source, markdown_options()).into_offset_iter() {
match event {
Event::Start(Tag::HtmlBlock) => html_block_start = Some(range.start),
@@ -290,14 +229,16 @@ fn normalize_html(source: &str) -> NormalizedDocument {
regions.push(start..range.end);
}
}
Event::Start(Tag::Paragraph) => paragraph = Some((range.start, false)),
Event::Start(Tag::Paragraph | Tag::Heading { .. }) => {
inline_container = Some((range.start, false));
}
Event::InlineHtml(_) => {
if let Some((_, contains_html)) = paragraph.as_mut() {
if let Some((_, contains_html)) = inline_container.as_mut() {
*contains_html = true;
}
}
Event::End(TagEnd::Paragraph) => {
if let Some((start, true)) = paragraph.take() {
Event::End(TagEnd::Paragraph | TagEnd::Heading(_)) => {
if let Some((start, true)) = inline_container.take() {
regions.push(start..range.end);
}
}
@@ -407,24 +348,22 @@ fn strip_duplicate_title(source: &str, title: &str) -> String {
fn split_images(source: &str, image_sizes: &HashMap<String, ImageSizeHint>) -> Vec<DocumentBlock> {
let mut images = Vec::<FoundImage>::new();
let mut image_stack = Vec::<FoundImage>::new();
let mut link_stack = Vec::<(usize, bool)>::new();
let mut link_stack = Vec::<(usize, usize)>::new();
for (event, range) in Parser::new_ext(source, markdown_options()).into_offset_iter() {
match event {
Event::Start(Tag::Link { .. }) => link_stack.push((range.start, false)),
Event::Start(Tag::Link { .. }) => link_stack.push((range.start, images.len())),
Event::End(TagEnd::Link) => {
if let Some((start, contains_image)) = link_stack.pop()
&& contains_image
&& let Some(image) = images.last_mut()
&& image.range.start >= start
if let Some((start, first_image)) = link_stack.pop()
&& first_image < images.len()
{
image.range = start..range.end;
images[first_image].range.start = start;
if let Some(last) = images.last_mut() {
last.range.end = range.end;
}
}
}
Event::Start(Tag::Image { dest_url, .. }) => {
if let Some((_, contains_image)) = link_stack.last_mut() {
*contains_image = true;
}
image_stack.push(FoundImage {
range: range.start..range.end,
url: dest_url.into_string(),
@@ -452,6 +391,15 @@ fn split_images(source: &str, image_sizes: &HashMap<String, ImageSizeHint>) -> V
let mut image_row = Vec::new();
let mut cursor = 0;
for image in images {
if image.range.start < cursor
|| image.range.end < image.range.start
|| image.range.end > source.len()
|| !source.is_char_boundary(image.range.start)
|| !source.is_char_boundary(image.range.end)
{
tracing::debug!("Ignoring invalid project image source range");
continue;
}
let between = &source[cursor..image.range.start];
let stays_in_row = !image_row.is_empty()
&& between.trim().is_empty()
@@ -510,40 +458,60 @@ fn formatted_text(source: &str, width: u16) -> Text<'static> {
external_markdown_text(source, width)
}
fn external_markdown_text(source: &str, _width: u16) -> Text<'static> {
let options = tui_markdown::Options::new(RmclMarkdownStyle);
let text = tui_markdown::from_str_with_options(source, &options);
let mut lines = Vec::with_capacity(text.lines.len());
for line in text.lines {
let mut spans = Vec::with_capacity(line.spans.len());
let mut index = 0;
while index < line.spans.len() {
if index + 2 < line.spans.len()
&& line.spans[index].content.as_ref() == " ("
&& line.spans[index + 2].content.as_ref() == ")"
&& line.spans[index + 1]
.style
.add_modifier
.contains(Modifier::UNDERLINED)
{
index += 3;
continue;
}
let content = line.spans[index].content.replace('*', "").replace("__", "");
spans.push(Span::styled(content, line.spans[index].style));
index += 1;
}
lines.push(Line {
style: line.style,
alignment: line.alignment,
spans,
fn external_markdown_text(source: &str, width: u16) -> Text<'static> {
let theme = markdown_theme();
let rule_style = theme.rule;
let renderer = RendererBuilder::new()
.with_theme(theme)
.with_heading(|_, spans| vec![Line::from(spans)])
.with_rule(move || vec![Line::styled("─".repeat(usize::from(width)), rule_style)])
.build();
let mut text = into_text_with_renderer(source, &renderer);
for line in &mut text.lines {
line.spans.retain(|span| {
let content = span.content.as_ref();
content != "*"
&& !(span.style.add_modifier.contains(Modifier::UNDERLINED)
&& content.starts_with("(http")
&& content.ends_with(')'))
});
for span in &mut line.spans {
if span.content.contains("**") {
span.content = span.content.replace("**", "").into();
}
}
}
Text {
alignment: text.alignment,
style: text.style,
lines,
}
text
}
fn markdown_theme() -> MarkdownTheme {
let mut theme = MarkdownTheme::default();
let heading = Style::default()
.fg(THEME.as_ref().accent())
.add_modifier(Modifier::BOLD);
theme.base = Style::default().fg(THEME.as_ref().text());
theme.h1 = heading;
theme.h2 = heading;
theme.h3 = heading;
theme.h4 = heading;
theme.h5 = heading;
theme.h6 = heading;
theme.inline_code = Style::default()
.fg(THEME.as_ref().text())
.bg(THEME.as_ref().surface());
theme.code_block = theme.inline_code;
theme.code_block_lang = Style::default().fg(THEME.as_ref().text_dim());
theme.link = Style::default()
.fg(THEME.as_ref().info())
.add_modifier(Modifier::UNDERLINED);
theme.block_quote = Style::default().fg(THEME.as_ref().text_dim());
theme.list_marker = 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());
theme.rule = theme.table_separator;
theme.html = Style::default().fg(THEME.as_ref().text_dim());
theme
}
impl TextBlock {
@@ -1172,7 +1140,7 @@ mod tests {
}
#[test]
fn headings_hide_markers_and_rules_are_delegated_to_the_renderer() {
fn headings_hide_markers_and_rules_use_terminal_lines() {
let text = super::formatted_text("## Known Issues\n\n---", 12);
let rendered = text
.lines
@@ -1185,7 +1153,8 @@ mod tests {
})
.collect::<Vec<_>>();
assert!(rendered.iter().any(|line| line == "Known Issues"));
assert!(rendered.iter().any(|line| line == "---"));
assert!(rendered.iter().any(|line| line == "────────────"));
assert!(!rendered.iter().any(|line| line == "---"));
assert!(!rendered.iter().any(|line| line.starts_with('#')));
}
@@ -1201,12 +1170,11 @@ mod tests {
.any(|span| span.content.contains("Performance"))
})
.unwrap();
assert!(
heading
.style
assert!(heading.spans.iter().any(|span| {
span.style
.add_modifier
.contains(ratatui::style::Modifier::BOLD)
);
}));
}
#[test]
@@ -1246,8 +1214,8 @@ mod tests {
}
#[test]
fn lists_are_rendered_by_tui_markdown() {
let text = super::formatted_text("- first\n- second", 20);
fn lists_use_terminal_bullets() {
let text = super::formatted_text("- first\n - nested\n- second", 20);
let rendered = text
.lines
.iter()
@@ -1258,7 +1226,31 @@ mod tests {
.collect::<String>()
})
.collect::<Vec<_>>();
assert_eq!(rendered, ["- first", "- second"]);
assert_eq!(rendered, ["• first", " • nested", "• second"]);
}
#[test]
fn markdown_headings_with_inline_html_are_converted_as_one_block() {
let document = super::Document::new(
"Project",
r#"### Follow me? <a href="https://example.com"><span style="text-decoration: underline;">example.com</span></a>"#,
);
let source = document
.blocks
.iter()
.find_map(|block| match block {
super::DocumentBlock::Text(text) => Some(text.source.as_str()),
_ => None,
})
.unwrap();
assert!(!source.contains('<'), "{source}");
let rendered = super::formatted_text(source, 80)
.lines
.iter()
.flat_map(|line| &line.spans)
.map(|span| span.content.as_ref())
.collect::<String>();
assert_eq!(rendered, "Follow me? example.com");
}
#[test]
@@ -1355,7 +1347,7 @@ mod tests {
}
#[test]
fn tables_are_rendered_by_tui_markdown() {
fn tables_are_rendered_by_the_external_renderer() {
let text = super::formatted_text(
"| Mod | Status | Note |\n| --- | --- | --- |\n| Player Animator | Supported | This means mods that use it like Better Combat and Emotecraft |",
48,
@@ -1418,4 +1410,21 @@ mod tests {
assert_eq!(scroll, max_scroll);
assert_eq!(terminal.backend().buffer()[(39, 7)].symbol(), "█");
}
#[test]
fn multiple_images_inside_one_link_do_not_overlap() {
let document = super::Document::new(
"Project",
"[![First](https://cdn.example/first.png) ![Second](https://cdn.example/second.png)](https://example.com)",
);
let mut urls = document.image_urls();
urls.sort();
assert_eq!(
urls,
[
"https://cdn.example/first.png",
"https://cdn.example/second.png"
]
);
}
}