//! Portal blocks and normalization for Slint's inline Markdown subset.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum Kind {
Paragraph,
Heading(u8),
ListItem,
Code,
Rule,
Image,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct DescriptionImage {
pub alt: String,
pub url: String,
pub link: String,
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct Block {
pub kind: Kind,
pub depth: usize,
pub text: String,
pub url: String,
pub marker: String,
pub images: Vec<DescriptionImage>,
}
impl Block {
fn new(kind: Kind, depth: usize, text: String) -> Self {
Self {
kind,
depth,
text,
url: String::new(),
marker: String::new(),
images: Vec::new(),
}
}
}
/// Browser destinations are independent of the network allowlist. Never accept shell/file schemes.
pub fn browser_url(url: &str) -> bool {
let lower = url.to_ascii_lowercase();
let Some(rest) = lower
.strip_prefix("https://")
.or_else(|| lower.strip_prefix("http://"))
else {
return false;
};
let host = rest.split(['/', '?', '#']).next().unwrap_or_default();
!host.is_empty()
&& !host.starts_with(':')
&& !host.contains('@')
&& !url
.chars()
.any(|c| c.is_whitespace() || c.is_control() || c == '\\')
}
pub fn image_url(url: &str) -> bool {
url.starts_with("https://assets-mod.factorio.com/") && browser_url(url)
}
fn heading(line: &str) -> Option<(u8, &str)> {
let level = line.bytes().take_while(|c| *c == b'#').count();
(1..=6).contains(&level).then(|| {
(
level.min(3) as u8,
line[level..].trim().trim_end_matches('#').trim_end(),
)
})
}
fn bullet(line: &str) -> Option<(&str, &str)> {
let (marker, text) = line.split_once(char::is_whitespace)?;
if matches!(marker, "*" | "-" | "+")
|| (marker.ends_with('.') && marker[..marker.len() - 1].parse::<u64>().is_ok())
{
Some((marker, text.trim_start()))
} else {
None
}
}
fn rule(line: &str) -> bool {
let s: String = line.chars().filter(|c| !c.is_whitespace()).collect();
s.len() >= 3
&& s.bytes().all(|c| c == s.as_bytes()[0])
&& matches!(s.as_bytes()[0], b'-' | b'*' | b'_')
}
/// Normalize inline constructs that differ from Slint's CommonMark subset.
pub fn inline(text: &str) -> String {
let mut out = String::new();
let mut rest = text;
while !rest.is_empty() {
// Escaped punctuation and complete code spans are opaque, including multi-backtick spans.
if let Some(len) = opaque_len(rest) {
out.push_str(&rest[..len]);
rest = &rest[len..];
continue;
}
if rest.starts_with('<')
&& let Some(end) = rest.find('>')
{
let tag = &rest[1..end];
let lower = tag.to_ascii_lowercase();
let name = lower.trim().trim_end_matches('/').trim();
if browser_url(tag) {
out.push_str(&format!("[{tag}]({tag})"));
} else {
out.push_str(match name {
"br" => " \n",
"b" | "strong" | "/b" | "/strong" => "**",
"i" | "em" | "/i" | "/em" => "*",
"s" | "del" | "strike" | "/s" | "/del" | "/strike" => "~~",
"code" | "/code" => "`",
_ => "",
});
}
rest = &rest[end + 1..];
continue;
}
if (rest.starts_with("https://") || rest.starts_with("http://"))
&& (out.is_empty() || out.ends_with([' ', '\n']))
{
let end = rest.find(char::is_whitespace).unwrap_or(rest.len());
let url = rest[..end].trim_end_matches(['.', ',', ';', '!', '?']);
if browser_url(url) {
out.push_str(&format!("[{url}]({url})"));
rest = &rest[url.len()..];
continue;
}
}
let c = rest.chars().next().unwrap();
out.push(c);
rest = &rest[c.len_utf8()..];
}
balance_breaks(&flatten_links(&lenient_emphasis(&out)))
}
// Python-Markdown accepts whitespace inside paired emphasis. Move boundary spaces outside
// the delimiters for Slint's CommonMark parser, keeping the visible text and opaque spans.
fn lenient_emphasis(text: &str) -> String {
let mut out = String::new();
let mut rest = text;
while !rest.is_empty() {
if let Some(len) = opaque_len(rest) {
out.push_str(&rest[..len]);
rest = &rest[len..];
continue;
}
if rest.starts_with("](")
&& let Some(end) = link_end(&rest[2..])
{
out.push_str(&rest[..end + 3]);
rest = &rest[end + 3..];
continue;
}
let n = emphasis_token(rest);
if n > 0 && !rest.starts_with('~') {
let token = &rest[..n];
let intraword =
rest.starts_with('_') && out.chars().next_back().is_some_and(char::is_alphanumeric);
if !intraword {
let mut offset = n;
while offset < rest.len() {
let tail = &rest[offset..];
if let Some(len) = opaque_len(tail) {
offset += len;
continue;
}
if tail.starts_with("](")
&& let Some(end) = link_end(&tail[2..])
{
offset += end + 3;
continue;
}
let run = emphasis_token(tail);
if run == n && tail.starts_with(token) {
break;
}
offset += if run > 0 {
run
} else {
tail.chars().next().unwrap().len_utf8()
};
}
if offset < rest.len() {
let inner = &rest[n..offset];
let trimmed = inner.trim();
if !trimmed.is_empty() {
out.push_str(&inner[..inner.len() - inner.trim_start().len()]);
out.push_str(token);
out.push_str(&lenient_emphasis(trimmed));
out.push_str(token);
out.push_str(&inner[inner.trim_end().len()..]);
rest = &rest[offset + n..];
continue;
}
}
}
out.push_str(token);
rest = &rest[n..];
continue;
}
let c = rest.chars().next().unwrap();
out.push(c);
rest = &rest[c.len_utf8()..];
}
out
}
/// Keep the heading's outer weight delimiter distinct from inner strong spans, without
/// rewriting underscores inside code or escaped punctuation.
pub fn heading_inline(text: &str) -> String {
let mut normalized = String::new();
let mut rest = text;
while !rest.is_empty() {
let len = opaque_len(rest).unwrap_or_else(|| {
if rest.starts_with("__") {
2
} else {
rest.chars().next().unwrap().len_utf8()
}
});
normalized.push_str(if &rest[..len] == "__" {
"**"
} else {
&rest[..len]
});
rest = &rest[len..];
}
inline(&format!("__{normalized}__"))
}
// Slint attaches a link when ANY inner style ends. Move label emphasis outside each link
// segment, so bold/italic labels keep their entire clickable range.
fn flatten_links(text: &str) -> String {
let mut out = String::new();
let mut rest = text;
while !rest.is_empty() {
if let Some(len) = opaque_len(rest) {
out.push_str(&rest[..len]);
rest = &rest[len..];
continue;
}
if let Some(label) = rest.strip_prefix('[')
&& let Some(end) = label.find("](")
&& let Some(close) = link_end(&label[end + 2..])
{
let url = &label[end + 2..end + 2 + close];
let label = &label[..end];
let mut segment = String::new();
let mut tail = label;
while !tail.is_empty() {
let mut token = emphasis_token(tail);
if tail.starts_with('_')
&& segment
.chars()
.next_back()
.is_some_and(char::is_alphanumeric)
&& tail[token..]
.chars()
.next()
.is_some_and(char::is_alphanumeric)
{
token = 0;
}
if token > 0 || tail.starts_with('\n') {
if !segment.is_empty() {
out.push_str(&format!("[{segment}]({url})"));
segment.clear();
}
let len = token.max(1);
out.push_str(&tail[..len]);
tail = &tail[len..];
} else {
let len =
opaque_len(tail).unwrap_or_else(|| tail.chars().next().unwrap().len_utf8());
segment.push_str(&tail[..len]);
tail = &tail[len..];
}
}
if !segment.is_empty() {
out.push_str(&format!("[{segment}]({url})"));
}
// `label` was narrowed above; offsets are relative to the original bracket content.
rest = &rest[1 + end + 2 + close + 1..];
continue;
}
let c = rest.chars().next().unwrap();
out.push(c);
rest = &rest[c.len_utf8()..];
}
out
}
fn link_end(text: &str) -> Option<usize> {
let mut depth = 0;
for (i, c) in text.char_indices() {
match c {
'(' => depth += 1,
')' if depth == 0 => return Some(i),
')' => depth -= 1,
_ => (),
}
}
None
}
fn opaque_len(text: &str) -> Option<usize> {
if let Some(tail) = text.strip_prefix('\\') {
return tail.chars().next().map(|c| 1 + c.len_utf8());
}
if text.starts_with('`') {
let n = text.bytes().take_while(|b| *b == b'`').count();
let mut offset = n;
while let Some(next) = text[offset..].find('`') {
let start = offset + next;
let length = text[start..].bytes().take_while(|b| *b == b'`').count();
if length == n {
return Some(start + length);
}
offset = start + length;
}
}
None
}
fn emphasis_token(text: &str) -> usize {
let first = text.as_bytes()[0];
if matches!(first, b'*' | b'_') {
text.bytes().take_while(|b| *b == first).count().min(3)
} else if text.starts_with("~~") {
2
} else {
0
}
}
// A Slint hard break starts a new paragraph but retains stale span offsets. Close/reopen
// active emphasis at the break; each paragraph then has its own valid style ranges.
fn balance_breaks(text: &str) -> String {
let mut out = String::new();
let mut stack: Vec<&str> = Vec::new();
let mut rest = text;
while !rest.is_empty() {
if let Some(len) = opaque_len(rest) {
out.push_str(&rest[..len]);
rest = &rest[len..];
continue;
}
if rest.starts_with("](")
&& let Some(end) = link_end(&rest[2..])
{
out.push_str(&rest[..end + 3]);
rest = &rest[end + 3..];
continue;
}
if rest.starts_with('\n') {
let trimmed = out.trim_end_matches(' ').len();
out.truncate(trimmed);
for token in stack.iter().rev() {
out.push_str(token);
}
out.push_str(" \n");
for token in &stack {
out.push_str(token);
}
rest = &rest[1..];
continue;
}
let n = emphasis_token(rest);
if n > 0 {
let token = &rest[..n];
let before = out.chars().next_back();
let after = rest[n..].chars().next();
let intraword = token.starts_with('_')
&& before.is_some_and(char::is_alphanumeric)
&& after.is_some_and(char::is_alphanumeric);
if !intraword
&& stack.last() == Some(&token)
&& before.is_some_and(|c| !c.is_whitespace())
{
stack.pop();
} else if !intraword
&& after.is_some_and(|c| !c.is_whitespace())
&& rest[n..].contains(token)
{
stack.push(token);
}
out.push_str(token);
rest = &rest[n..];
continue;
}
let c = rest.chars().next().unwrap();
// Quotes are optional blocks in our mix; preserve them without triggering Slint's
// unsupported-block error and losing the formatting of the entire paragraph.
if c == '>' && (out.is_empty() || out.ends_with('\n')) {
out.push('\\');
}
out.push(c);
rest = &rest[c.len_utf8()..];
}
out
}
// Whitespace-separated images in a paragraph share a flow row.
fn push_text(out: &mut Vec<Block>, mut block: Block) {
let mut rest = block.text.as_str();
let mut text = String::new();
let mut image_row: Option<usize> = None;
while let Some(start) = rest.find("![") {
text.push_str(&rest[..start]);
let tail = &rest[start + 2..];
let Some(label_end) = tail.find("](") else {
text.push_str(&rest[start..]);
rest = "";
break;
};
let after = &tail[label_end + 2..];
let mut nesting = 0;
let end = after.char_indices().find_map(|(i, c)| {
match c {
'(' => nesting += 1,
')' if nesting > 0 => nesting -= 1,
')' => return Some(i),
_ => (),
}
None
});
let Some(end) = end else {
text.push_str(&rest[start..]);
rest = "";
break;
};
let alt = &tail[..label_end];
let url = after[..end].split_whitespace().next().unwrap_or_default();
let mut remaining = &after[end + 1..];
let mut destination = None;
if text.ends_with('[')
&& let Some(outer) = remaining.strip_prefix("](")
&& let Some(close) = outer.find(')')
{
text.pop();
destination = Some(&outer[..close]);
remaining = &outer[close + 1..];
}
{
if !text.trim().is_empty() {
let mut before = block.clone();
before.text = inline(text.trim());
out.push(before);
image_row = None;
}
let mut img = Block::new(Kind::Image, block.depth, alt.into());
img.url = url.into();
let item = DescriptionImage {
alt: alt.into(),
url: url.into(),
link: destination.unwrap_or_default().into(),
};
if let Some(index) = image_row {
out[index].images.push(item);
} else {
img.images.push(item);
image_row = Some(out.len());
out.push(img);
}
text.clear();
block.kind = Kind::Paragraph;
block.marker.clear();
}
rest = remaining;
}
text.push_str(rest);
if !text.trim().is_empty() {
block.text = inline(text.trim());
out.push(block);
}
}
pub fn parse(markdown: &str) -> Vec<Block> {
let source = markdown
.replace("\r\n", "\n")
.replace('\r', "\n")
.replace('\t', " ");
let lines: Vec<_> = source.lines().collect();
let mut out = Vec::new();
let mut i = 0;
let mut in_list = false;
while i < lines.len() {
let raw = lines[i];
let line = raw.trim();
if line.is_empty() {
i += 1;
continue;
}
let indent = raw.len() - raw.trim_start().len();
if indent == 0 {
in_list = bullet(line).is_some();
}
let depth = (indent / 4).min(32);
if line.starts_with("```") || line.starts_with("~~~") {
let fence = &line[..3];
i += 1;
let mut text = Vec::new();
while i < lines.len() && !lines[i].trim_start().starts_with(fence) {
text.push(lines[i]);
i += 1;
}
i += usize::from(i < lines.len());
out.push(Block::new(Kind::Code, 0, text.join("\n")));
continue;
}
if indent >= 4 && !in_list {
let mut text = Vec::new();
while i < lines.len() && (lines[i].starts_with(" ") || lines[i].is_empty()) {
text.push(lines[i].strip_prefix(" ").unwrap_or(""));
i += 1;
}
out.push(Block::new(Kind::Code, 0, text.join("\n").trim_end().into()));
continue;
}
if let Some((level, text)) = heading(line) {
push_text(&mut out, Block::new(Kind::Heading(level), 0, text.into()));
i += 1;
continue;
}
if rule(line) {
out.push(Block::new(Kind::Rule, 0, String::new()));
i += 1;
continue;
}
if i + 1 < lines.len()
&& lines[i + 1].trim().len() >= 3
&& lines[i + 1].trim().bytes().all(|b| b == b'=' || b == b'-')
{
let level = if lines[i + 1].trim().starts_with('=') {
1
} else {
2
};
push_text(&mut out, Block::new(Kind::Heading(level), 0, line.into()));
i += 2;
continue;
}
let item = bullet(line);
let mut block = if let Some((marker, text)) = item {
in_list = true;
let mut b = Block::new(Kind::ListItem, depth, text.into());
b.marker = if marker.ends_with('.') {
marker.into()
} else {
String::new()
};
b
} else {
Block::new(
Kind::Paragraph,
if in_list { depth } else { 0 },
line.into(),
)
};
let mut hard_break = raw.ends_with(" ");
i += 1;
while i < lines.len() && !lines[i].trim().is_empty() {
let next = lines[i].trim();
// A list may nest inside its initial block. Continuation paragraphs do not interrupt
// on bullet syntax: this is the portal's literal Prerequisites/Backline Recipe quirk.
if heading(next).is_some() || rule(next) || (item.is_some() && bullet(next).is_some()) {
break;
}
block.text.push_str(if hard_break { " \n" } else { " " });
if item.is_none() && bullet(next).is_some() && next.starts_with('*') {
block.text.push('\\');
}
block.text.push_str(next);
hard_break = lines[i].ends_with(" ");
i += 1;
}
push_text(&mut out, block);
}
out
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn inline_emphasis_keeps_the_native_supported_constructs() {
for text in [
"**bold**",
"__bold__",
"*italic*",
"_italic_",
"***both***",
"**outer *inner* end**",
"~~old~~",
"`a*b`",
"``a`*b``",
r"\*literal\*",
"snake_case",
] {
assert_eq!(inline(text), text);
}
}
#[test]
fn padded_portal_emphasis_keeps_spaces_and_opaque_literals() {
assert_eq!(
inline("**Temperature: HotClimates: ** Some text"),
"**Temperature: HotClimates:** Some text"
);
assert_eq!(inline("__ padded __"), " __padded__ ");
assert_eq!(inline("Text * padded *"), "Text *padded* ");
assert_eq!(inline("Text _ padded _"), "Text _padded_ ");
assert_eq!(
inline("a*b snake_case `** padded **`"),
"a*b snake_case `** padded **`"
);
assert_eq!(parse("**soft\nbreak **")[0].text, "**soft break** ");
}
#[test]
fn inline_code_requires_a_closing_run_of_the_same_length() {
for text in ["`a``<b>*c`", "``a```<br>*c``", "``a```<b>*c`<i>end``"] {
assert_eq!(inline(text), text);
}
}
#[test]
fn emphasis_spans_are_closed_and_reopened_across_hard_breaks() {
assert_eq!(inline("**first \nsecond**"), "**first** \n**second**");
assert_eq!(inline("__first<br>second__"), "__first__ \n__second__");
assert_eq!(
inline("***first \nsecond***"),
"***first*** \n***second***"
);
assert_eq!(
inline("**outer *first \nsecond* end**"),
"**outer *first*** \n***second* end**"
);
}
#[test]
fn html_inline_styles_survive_and_code_and_escapes_are_opaque() {
assert_eq!(
inline("<b>bold</b> <strong>strong</strong> <i>italic</i> <em>em</em> <del>old</del>"),
"**bold** **strong** *italic* *em* ~~old~~"
);
assert_eq!(inline("<B>bold</B><BR />done"), "**bold** \ndone");
assert_eq!(inline("<code>a*b</code> `a<b>*c`"), "`a*b` `a<b>*c`");
assert_eq!(inline(r"\`x<b>y</b>"), r"\`x**y**");
}
#[test]
fn emphasis_in_links_lists_and_headings_reaches_the_view() {
assert_eq!(heading_inline("**bold** heading"), "__**bold** heading__");
assert_eq!(heading_inline("__bold__ heading"), "__**bold** heading__");
assert_eq!(heading_inline("`__code__`"), "__`__code__`__");
assert_eq!(
inline("[a **bold** *word*](https://example.com/a_(b))"),
"[a ](https://example.com/a_(b))**[bold](https://example.com/a_(b))**[ ](https://example.com/a_(b))*[word](https://example.com/a_(b))*"
);
let blocks = parse("# A **bold** heading\n\n* __bold__ and *italic*");
assert_eq!(blocks[0].text, "A **bold** heading");
assert_eq!(blocks[1].text, "__bold__ and *italic*");
}
#[test]
fn matches_portal_headings_and_continuation_list_quirk() {
let blocks = parse(
"###CONTENT\n* __Turret Mounts__ \n Recipe: \n * 8 Steel Plates\n * 1 Engine Unit\n\n Prerequisites: \n * Concrete\n * Engine\n\n* __Backline__",
);
assert_eq!(blocks[0].kind, Kind::Heading(3));
assert_eq!(blocks[1].kind, Kind::ListItem);
assert_eq!(blocks[2].depth, 1);
assert_eq!(blocks[3].kind, Kind::ListItem);
assert_eq!(blocks[4].kind, Kind::Paragraph);
assert_eq!(blocks[4].text, "Prerequisites: \n\\* Concrete \\* Engine");
assert_eq!(blocks[5].depth, 0);
}
#[test]
fn parses_blocks_without_truncating_and_keeps_ordered_depth() {
let b = parse(
"# Title\n\n1. **first**\n 2. nested\n\n---\n\n```lua\nlocal x = 1\n```\n\n code\n\nlast",
);
assert_eq!(
b.iter().map(|b| b.kind.clone()).collect::<Vec<_>>(),
vec![
Kind::Heading(1),
Kind::ListItem,
Kind::ListItem,
Kind::Rule,
Kind::Code,
Kind::Code,
Kind::Paragraph
]
);
assert_eq!(b[2].marker, "2.");
assert_eq!(b[2].depth, 1);
assert_eq!(b[4].text, "local x = 1");
assert_eq!(parse(&"paragraph\n\n".repeat(10000)).len(), 10000);
}
#[test]
fn images_preserve_external_urls_and_outer_links() {
let b = parse(
"before  after\n\n",
);
assert_eq!(b.len(), 4);
assert_eq!(b[1].kind, Kind::Image);
assert_eq!(b[1].text, "machine");
assert_eq!(b[3].images[0].url, "https://example.com/x.gif");
assert!(!image_url("https://assets-mod.factorio.com.evil/x"));
assert!(!image_url("https://assets-mod.factorio.com/@bad\\x"));
assert_eq!(
parse("[](https://example.com/donate)")[0]
.images[0]
.link,
"https://example.com/donate"
);
}
#[test]
fn only_http_and_https_may_reach_the_injected_browser_opener() {
for u in [
"https://mods.factorio.com/mod/a",
"http://example.com/path?q=one#two",
"HTTPS://example.com",
] {
assert!(browser_url(u), "{u}");
}
for u in [
"file:///x",
"javascript:alert(1)",
"cmd.exe /c x",
"https://",
"https:///path",
" https://example.com",
"https://example.com/\ncmd",
"https://user@host/x",
"https://example.com\\evil",
] {
assert!(!browser_url(u), "{u}");
}
}
#[test]
fn inline_autolinks_and_html_breaks_do_not_fetch_resources() {
assert_eq!(
inline("See https://example.com/x. `<x>`<br>done"),
"See [https://example.com/x](https://example.com/x). `<x>` \ndone"
);
assert_eq!(
inline("<script>ignore</script> **bold**"),
"ignore **bold**"
);
}
}