Keep hyperref command labels
\hyperref[label]{text}, \hyperlink{name}{text} and \hypertarget{name}{text}
were unsupported and leaked as text. A text-stage pass now drops the link
machinery and keeps the visible label, accepting the optional [label] argument
of \hyperref and the braced target of the other two; internal PDF destinations
are not modelled.
A preview test uses all three forms and checks "the intro", "linked" and
"anchor" with no warnings and no leaked command names.
This commit is contained in:
1 parent
6c6963cac7
commit
5fbcc34ddd
4 files changed
+86
-6
No files matched your search
@@ -9800,6 +9800,59 @@ fn strip_noop_commands(text: &str) -> String {
|
||||
out
|
||||
}
|
||||
|
||||
/// Keep the visible label of `\hyperref[label]{text}`, `\hyperlink{name}{text}`
|
||||
/// and `\hypertarget{name}{text}`; internal PDF targets are not modelled.
|
||||
fn strip_hyperref_commands(text: &str) -> String {
|
||||
let mut output = String::new();
|
||||
let mut rest = text;
|
||||
loop {
|
||||
let mut next: Option<(usize, &str, bool)> = None;
|
||||
for (command, bracketed) in [
|
||||
("\\hyperref", true),
|
||||
("\\hyperlink", false),
|
||||
("\\hypertarget", false),
|
||||
] {
|
||||
if let Some(index) = rest.find(command) {
|
||||
let better = next.is_none_or(|(known, _, _)| index < known);
|
||||
if better {
|
||||
next = Some((index, command, bracketed));
|
||||
}
|
||||
}
|
||||
}
|
||||
let Some((index, command, bracketed)) = next else {
|
||||
output.push_str(rest);
|
||||
break;
|
||||
};
|
||||
output.push_str(&rest[..index]);
|
||||
let mut cursor = rest[index + command.len()..].trim_start();
|
||||
if bracketed {
|
||||
if let Some(stripped) = cursor.strip_prefix('[')
|
||||
&& let Some((_, tail)) = stripped.split_once(']')
|
||||
{
|
||||
cursor = tail.trim_start();
|
||||
} else {
|
||||
output.push_str(command);
|
||||
rest = &rest[index + command.len()..];
|
||||
continue;
|
||||
}
|
||||
} else if let Some((_, used)) = braced_group(cursor) {
|
||||
cursor = cursor[used..].trim_start();
|
||||
} else {
|
||||
output.push_str(command);
|
||||
rest = &rest[index + command.len()..];
|
||||
continue;
|
||||
}
|
||||
let Some((label, used)) = braced_group(cursor) else {
|
||||
output.push_str(command);
|
||||
rest = &rest[index + command.len()..];
|
||||
continue;
|
||||
};
|
||||
output.push_str(label);
|
||||
rest = &cursor[used..];
|
||||
}
|
||||
output
|
||||
}
|
||||
|
||||
/// Rewrite `\parbox[pos]{width}[..][..]{body}` into an `\mbox` so the existing
|
||||
/// box handling keeps the content unbreakable (width wrapping is not modelled).
|
||||
fn expand_parbox(text: &str) -> String {
|
||||
@@ -12492,6 +12545,7 @@ fn build_document_with_labels(
|
||||
}
|
||||
|
||||
text = expand_parbox(&text);
|
||||
text = strip_hyperref_commands(&text);
|
||||
text = expand_orcidlink(&text);
|
||||
text = strip_box_commands(&text);
|
||||
text = expand_math_spaces(&text);
|
||||
@@ -16424,6 +16478,32 @@ A definition.
|
||||
assert!(!text.contains("makeatletter"), "{text}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn hyperref_commands_keep_their_labels() {
|
||||
let source = "\\documentclass{article}\n\\begin{document}\n\nSee \\hyperref[sec:intro]{the intro}, \\hyperlink{target}{linked} and \\hypertarget{id}{anchor}.\n\n\\end{document}\n";
|
||||
let json = preview_json(source).expect("preview");
|
||||
let value: serde_json::Value = serde_json::from_str(&json).expect("JSON");
|
||||
assert_eq!(
|
||||
value["warnings"].as_array().map(Vec::len),
|
||||
Some(0),
|
||||
"{}",
|
||||
value["warnings"]
|
||||
);
|
||||
let svg = value["pages"][0]["svg"].as_str().expect("svg");
|
||||
let text: String = svg
|
||||
.split('>')
|
||||
.filter_map(|piece| piece.split('<').next())
|
||||
.collect::<String>()
|
||||
.split_whitespace()
|
||||
.collect();
|
||||
assert!(text.contains("theintro"), "{text}");
|
||||
assert!(text.contains("linked"), "{text}");
|
||||
assert!(text.contains("anchor"), "{text}");
|
||||
assert!(!text.contains("hyperref"), "{text}");
|
||||
assert!(!text.contains("hyperlink"), "{text}");
|
||||
assert!(!text.contains("hypertarget"), "{text}");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn footnotemark_and_footnotetext_pair_up() {
|
||||
let source = "\\documentclass{article}\n\\begin{document}\n\nText\\footnotemark and more.\\footnotetext{Deferred note.}\n\n\\end{document}\n";
|
||||
|
||||
Reference in new issue
Block a user