Drop layout-only commands from paragraph text

This commit is contained in:
gloamxun committed 2026-10-26 04:19:18 +08:00
1 parent fb111806e2
commit 55d34dc259
3 files changed
+59 -3

No files matched your search

+56
View File
@@ -6798,6 +6798,51 @@ fn split_color_runs(
out
}
fn strip_noop_commands(text: &str) -> String {
const COMMANDS: [&str; 10] = [
"\\protect",
"\\relax",
"\\ignorespaces",
"\\unskip",
"\\nobreak",
"\\allowbreak",
"\\sloppy",
"\\fussy",
"\\phantomsection",
"\\leavevmode",
];
let mut out = String::new();
let mut index = 0;
while index < text.len() {
let rest = &text[index..];
let mut matched = None;
if rest.starts_with('\\') {
for command in COMMANDS {
if let Some(after) = rest.strip_prefix(command) {
if !after
.chars()
.next()
.is_some_and(|c| c.is_ascii_alphabetic())
{
matched = Some(command.len());
break;
}
}
}
}
if let Some(length) = matched {
index += length;
continue;
}
let Some(character) = rest.chars().next() else {
break;
};
out.push(character);
index += character.len_utf8();
}
out
}
fn strip_box_commands(text: &str) -> String {
let mut out = String::new();
let mut rest = text;
@@ -8994,6 +9039,7 @@ fn build_document_with_labels(
.trim_start()
.to_string();
}
text = strip_noop_commands(&text);
text = strip_box_commands(&text);
text = expand_ensuremath(&text);
if let Some((color, rest)) = color_switch(&text) {
@@ -13060,6 +13106,16 @@ A definition.
assert_eq!(runs[0].3, "Boxed");
}
#[test]
fn noop_commands_are_dropped() {
let preview = preview_json("A \\protect B \\relax C \\ignorespaces D \\phantomsection E.")
.expect("preview");
assert!(preview.contains(">A</text>"));
assert!(preview.contains(">E</text>"));
assert!(!preview.contains("protect"));
assert!(preview.contains("\"warnings\":[]"));
}
#[test]
fn box_commands_keep_their_text() {
let preview =