fix(anatomy): R5.4 guard rules & (legacy references), image references, nested padded wiki links; linear URL checks

This commit is contained in:
bchanot committed 2026-10-23 13:02:21 +02:00
1 parent 6d4fdd65b2
commit dcef2eb79c
4 files changed
+103 -15

No files matched your search

+70 -11
View File
@@ -27,15 +27,33 @@ fn refuse(line: &str) -> PipelineError {
PipelineError::UnsupportedMarkdown(line.to_owned())
}
/// Inline constructs Prettier re-prints: a link, image or reference (`](`, `][`), a wiki link
/// with inner spaces, an HTML tag or an autolink (`<` before a letter, `/`, `!` or `?`), a
/// character reference (`&name;`, `&#n;`).
/// HTML's legacy character references, the names a browser and Prettier decode without a `;`
/// (and as a prefix of a longer word): an `&` before one of them is never admitted.
const LEGACY_ENTITIES: [&str; 106] = [
"AElig", "AMP", "Aacute", "Acirc", "Agrave", "Aring", "Atilde", "Auml", "COPY", "Ccedil",
"ETH", "Eacute", "Ecirc", "Egrave", "Euml", "GT", "Iacute", "Icirc", "Igrave", "Iuml", "LT",
"Ntilde", "Oacute", "Ocirc", "Ograve", "Oslash", "Otilde", "Ouml", "QUOT", "REG", "THORN",
"Uacute", "Ucirc", "Ugrave", "Uuml", "Yacute", "aacute", "acirc", "acute", "aelig", "agrave",
"amp", "aring", "atilde", "auml", "brvbar", "ccedil", "cedil", "cent", "copy", "curren", "deg",
"divide", "eacute", "ecirc", "egrave", "eth", "euml", "frac12", "frac14", "frac34", "gt",
"iacute", "icirc", "iexcl", "igrave", "iquest", "iuml", "laquo", "lt", "macr", "micro",
"middot", "nbsp", "not", "ntilde", "oacute", "ocirc", "ograve", "ordf", "ordm", "oslash",
"otilde", "ouml", "para", "plusmn", "pound", "quot", "raquo", "reg", "sect", "shy", "sup1",
"sup2", "sup3", "szlig", "thorn", "times", "uacute", "ucirc", "ugrave", "uml", "uuml",
"yacute", "yen", "yuml",
];
/// Inline constructs Prettier re-prints: a link, image or reference (`](`, `][`), an image
/// reference (`![`), a wiki link padded inside (`[[` before a space, a space before `]]`), an
/// HTML tag or an autolink (`<` before a letter, `/`, `!` or `?`), a character reference
/// (`&name;`, `&#n;`); a bare `&` is ruled in `admitted_char`.
fn rewritten_construct(plain: &str) -> bool {
let padded_wiki = plain.split("[[").skip(1).any(|after| {
let inside = after.split("]]").next().unwrap_or_default();
after.contains("]]")
&& (inside.starts_with(char::is_whitespace) || inside.ends_with(char::is_whitespace))
});
// A `[[` padded inside with a `]]` after it, or a `]]` padded inside with a `[[` before it.
let padded_wiki = plain.match_indices("[[").any(|(at, _)| {
plain[at + 2..].starts_with(char::is_whitespace) && plain[at + 2..].contains("]]")
}) || plain
.match_indices("]]")
.any(|(at, _)| plain[..at].ends_with(char::is_whitespace) && plain[..at].contains("[["));
let tag = plain
.split('<')
.skip(1)
@@ -50,7 +68,12 @@ fn rewritten_construct(plain: &str) -> bool {
.chars()
.all(|c| c.is_ascii_alphanumeric())
});
plain.contains("](") || plain.contains("][") || padded_wiki || tag || reference
plain.contains("](")
|| plain.contains("][")
|| plain.contains("![")
|| padded_wiki
|| tag
|| reference
}
/// The `**word**` spans removed (their word kept); `None` when a `*` stays outside one.
@@ -78,17 +101,53 @@ fn outside_strong(plain: &str) -> Option<String> {
(!out.contains('*')).then_some(out)
}
/// Whether `chars[from..]` starts with `text` (no allocation).
fn starts_with(chars: &[char], from: usize, text: &str) -> bool {
(from..)
.zip(text.chars())
.all(|(index, c)| chars.get(index) == Some(&c))
}
/// Whether position `i` lies in a bare URL token: the run of non-space characters around it
/// starts with a URL literal.
fn in_url_token(chars: &[char], i: usize) -> bool {
let start = chars[..i]
.iter()
.rposition(|c| c.is_whitespace())
.map_or(0, |p| p + 1);
(start..i).any(|k| URL_STARTS.iter().any(|url| starts_with(chars, k, url)))
}
/// An `&` Prettier writes as is: before a space, or a URL's query separator (`&name=`, the name
/// not starting with a legacy reference). Anything else may start a character reference.
fn admitted_ampersand(chars: &[char], i: usize) -> bool {
match chars.get(i + 1) {
Some(' ') => true,
Some(c) if c.is_ascii_lowercase() && in_url_token(chars, i) => {
let name: String = chars[i + 1..]
.iter()
.take_while(|c| c.is_ascii_alphanumeric())
.collect();
chars.get(i + 1 + name.chars().count()) == Some(&'=')
&& !LEGACY_ENTITIES
.iter()
.any(|legacy| name.starts_with(legacy))
}
_ => false,
}
}
fn admitted_char(chars: &[char], i: usize) -> bool {
let at = |k: usize| chars.get(k).copied();
let previous = i.checked_sub(1).and_then(at);
match chars[i] {
c if c.is_alphanumeric() => true,
' ' => at(i + 1) != Some(' '),
'&' => admitted_ampersand(chars, i),
'_' => {
let rest: String = chars[i + 1..].iter().collect();
previous.is_some_and(|c| c.is_ascii_alphanumeric())
&& at(i + 1).is_some_and(|c| c.is_ascii_alphanumeric())
&& !URL_STARTS.iter().any(|start| rest.starts_with(start))
&& !URL_STARTS.iter().any(|url| starts_with(chars, i + 1, url))
}
'\\' => matches!(at(i + 1), Some('"' | '|')) && previous != Some('\\'),
c => PUNCTUATION.contains(c),
+5 -2
View File
@@ -74,7 +74,10 @@ fn equine_safe(probe: &str) -> bool {
.split('<')
.skip(1)
.any(|after| after.starts_with(|c: char| c.is_ascii_alphabetic() || "/!?".contains(c)));
let construct = tag || ["](", "][", "[[ ", "&"].iter().any(|m| probe.contains(m));
let construct = tag
|| ["](", "][", "![", "[[ ", " ]]", "&"]
.iter()
.any(|m| probe.contains(m));
!construct
&& !probe.contains(" ")
&& !probe.starts_with(' ')
@@ -134,6 +137,6 @@ fn every_probe_is_formatted_as_the_typescript_or_refused() {
"silent differences for {} probes: {silent:?}",
silent.len()
);
assert_eq!(same + refused, 762);
assert_eq!(same + refused, 782);
println!("{same} identical, {refused} refused");
}
@@ -1 +1 @@
{"baseline":{"schema_version":2,"species_id":"equine","name":{"fr":"Mini","en":"Mini"},"description":{},"scope_disclaimer":{},"attribution":{"authors":[],"license":null,"notes":null},"vertebral_config":{"regions":[{"code":"C","name":{"fr":"Cervicales","en":"Cervical"},"count":7},{"code":"T","name":{"fr":"Thoraciques","en":"Thoracic"},"count":18}]},"references":[],"categories":[{"id":"joints","name":{"fr":"Articulations","en":"Joints"},"color":null},{"id":"bones","name":{"fr":"Os","en":"Bones"},"color":null}],"views":[{"id":"main","kind":"main","name":{"fr":"Vue","en":"View"},"svg_asset_id":"3f6c1b52-8a4e-4d17-9b0c-5e2a7d91c4f8","view_box":null},{"id":"head","kind":"detail","parent_view_id":"main","name":{"fr":"Tête","en":"Head"},"svg_asset_id":null,"view_box":{"min_x":0,"min_y":0,"width":10,"height":10}}],"annotations":[{"id":"hip","kind":"point","view_id":"main","category_id":"joints","laterality":"bilateral","geometry":{"type":"circle","cx":1,"cy":2,"r":3},"name":{"fr":"Hanche","en":"Hip"},"finding_zone_id":"hip","vertebral_region_code":null},{"id":"neck","kind":"zone","view_id":"main","category_id":"bones","laterality":"unpaired","geometry":null,"name":{"fr":"Cou","en":"Neck"},"finding_zone_id":"neck","vertebral_region_code":"C"},{"id":"head-trigger","kind":"detail_trigger","view_id":"main","category_id":"bones","laterality":"unpaired","geometry":null,"name":null,"opens_view_id":"head"}],"finding_zones":[{"id":"hip","name":{"fr":"Hanche","en":"Hip"},"categories":[{"id":"hip-dysfunctions","kind":"dysfunction","name":{"fr":"Dysfonctions","en":"Dysfunctions"},"children":[{"id":"mobility","name":{"fr":"mobility fr","en":"mobility en"},"children":[{"id":"flexion","name":{"fr":"flexion fr","en":"flexion en"}},{"id":"extension","name":{"fr":"extension fr","en":"extension en"}}]},{"id":"pain","name":{"fr":"pain fr","en":"pain en"}}]},{"id":"anamnesis","kind":"anamnesis","name":{"fr":"Anamnèse","en":"Anamnesis"},"children":[{"id":"trauma","name":{"fr":"trauma fr","en":"trauma en"}}]}]},{"id":"neck","name":{"fr":"Cou","en":"Neck"},"categories":[{"id":"neck-dysfunctions","kind":"dysfunction","name":{"fr":"Dysfonctions","en":"Dysfunctions"},"children":[{"id":"stiffness","name":{"fr":"stiffness fr","en":"stiffness en"}},{"id":"vet","name":{"fr":"vet fr","en":"vet en"},"requires_vet_referral":true}]}]}]},"references":[{"id":"atlas","type":"book","citation":"Atlas of anatomy","url":null,"doi":null},{"id":"paper","type":"journal","citation":"A paper","url":null,"doi":"10.1/x"}],"legacy_id_map":{"views":{"head":"head"},"annotations":{"legacy-hip":"hip","legacy-neck":"neck"},"categories":{},"finding_nodes":{},"assets":{}},"cases":[{"probe":"*","changes_file":{"changes":[{"id":"CHG-001","level":"C","kind":"correction","title":{"fr":"Titre *","en":"Title"},"rationale":"Raison * fin.","reference_ids":[],"question":"Question * ?","ops":[{"op":"add","target":"document","field":"references","value":{"id":"probe-ref","type":"web","citation":"Citation *","url":null,"doi":null}},{"op":"cite","target":"finding:hip/hip-dysfunctions/mobility","reference_ids":["probe-ref"]},{"op":"set","target":"finding:hip/hip-dysfunctions/mobility","field":"name.en","value":"Label *"},{"op":"set","target":"finding:hip/hip-dysfunctions/mobility/flexion","field":"name.fr","value":"*"}]}]},"changelog_raw_sha256":"55b992587e03e05c2b98e81e7c644aa852713084183a89cd2651b61f0e922cbb","changelog_sha256":"1e21c6e4509109bba34c869cf8df77f838e69cfdc85df203a71917b1cd25cca7","review_raw_sha256":"3f289d6d2f18fef8f99ea375703603f2b0f1076063f7440a7554ed081b1f63aa","review_sha256":"2fef9ccf08ff9e38ff59e7ca7b6fa4966e8f4ddea9188c666276adb0c59f41db"},{"probe":"*x","changes_file":{"changes":[{"id":"CHG-001","level":"C","kind":"correction","title":{"fr":"Titre *x","en":"Title"},"rationale":"Raison *x fin.","reference_ids":[],"question":"Question *x ?","ops":[{"op":"add","target":"document","field":"references","value":{"id":"probe-ref","type":"web","citation":"Citation *x","url":null,"doi":null}},{"op":"cite","target":"finding:hip/hip-dysfunctions/mobility","reference_ids":["probe-ref"]},{"op":"set","target":"finding:hip/hip-dysfunctions/mobility","field":"name.en","value":"Label *x"},{"op":"set","target":"finding:hip/hip-dysfunctions/mobility/flexion","field":"name.fr","value":"*x"}]}]},"changelog_raw_sha256":"bcb9602df19f1a01322e79fb099fd4031f40c3863ecf904daf6e107ceec9f8a6","changelog_sha256":"23d0c545ce56bc949d88d6ad5d99104aba582f851fc8f8689901cc57d9400399","review_raw_sha256":"cff4115989dd4c411d94893c5b1a19333169b53fd0a6c4dd668ba771f7e97332","review_sha256":"382d3968e4d03960e4cca33109a06b6af0db8cc53b8aec01cf940004ab91755e"},{"probe":"x*","changes_file":{"changes":[{"id":"CHG-001","level":"C","kind":"correction","title":{"fr":"Titre x*","en":"Title"},"rationale":"Raison x* fin.","reference_ids":[],"question":"Question x* ?","ops":[{"op":"add","target":"document","field":"references","value":{"id":"probe-ref","type":"web","citation":"Citation x*","url":null,"doi":null}}Line truncated
{"baseline":{"schema_version":2,"species_id":"equine","name":{"fr":"Mini","en":"Mini"},"description":{},"scope_disclaimer":{},"attribution":{"authors":[],"license":null,"notes":null},"vertebral_config":{"regions":[{"code":"C","name":{"fr":"Cervicales","en":"Cervical"},"count":7},{"code":"T","name":{"fr":"Thoraciques","en":"Thoracic"},"count":18}]},"references":[],"categories":[{"id":"joints","name":{"fr":"Articulations","en":"Joints"},"color":null},{"id":"bones","name":{"fr":"Os","en":"Bones"},"color":null}],"views":[{"id":"main","kind":"main","name":{"fr":"Vue","en":"View"},"svg_asset_id":"3f6c1b52-8a4e-4d17-9b0c-5e2a7d91c4f8","view_box":null},{"id":"head","kind":"detail","parent_view_id":"main","name":{"fr":"Tête","en":"Head"},"svg_asset_id":null,"view_box":{"min_x":0,"min_y":0,"width":10,"height":10}}],"annotations":[{"id":"hip","kind":"point","view_id":"main","category_id":"joints","laterality":"bilateral","geometry":{"type":"circle","cx":1,"cy":2,"r":3},"name":{"fr":"Hanche","en":"Hip"},"finding_zone_id":"hip","vertebral_region_code":null},{"id":"neck","kind":"zone","view_id":"main","category_id":"bones","laterality":"unpaired","geometry":null,"name":{"fr":"Cou","en":"Neck"},"finding_zone_id":"neck","vertebral_region_code":"C"},{"id":"head-trigger","kind":"detail_trigger","view_id":"main","category_id":"bones","laterality":"unpaired","geometry":null,"name":null,"opens_view_id":"head"}],"finding_zones":[{"id":"hip","name":{"fr":"Hanche","en":"Hip"},"categories":[{"id":"hip-dysfunctions","kind":"dysfunction","name":{"fr":"Dysfonctions","en":"Dysfunctions"},"children":[{"id":"mobility","name":{"fr":"mobility fr","en":"mobility en"},"children":[{"id":"flexion","name":{"fr":"flexion fr","en":"flexion en"}},{"id":"extension","name":{"fr":"extension fr","en":"extension en"}}]},{"id":"pain","name":{"fr":"pain fr","en":"pain en"}}]},{"id":"anamnesis","kind":"anamnesis","name":{"fr":"Anamnèse","en":"Anamnesis"},"children":[{"id":"trauma","name":{"fr":"trauma fr","en":"trauma en"}}]}]},{"id":"neck","name":{"fr":"Cou","en":"Neck"},"categories":[{"id":"neck-dysfunctions","kind":"dysfunction","name":{"fr":"Dysfonctions","en":"Dysfunctions"},"children":[{"id":"stiffness","name":{"fr":"stiffness fr","en":"stiffness en"}},{"id":"vet","name":{"fr":"vet fr","en":"vet en"},"requires_vet_referral":true}]}]}]},"references":[{"id":"atlas","type":"book","citation":"Atlas of anatomy","url":null,"doi":null},{"id":"paper","type":"journal","citation":"A paper","url":null,"doi":"10.1/x"}],"legacy_id_map":{"views":{"head":"head"},"annotations":{"legacy-hip":"hip","legacy-neck":"neck"},"categories":{},"finding_nodes":{},"assets":{}},"cases":[{"probe":"*","changes_file":{"changes":[{"id":"CHG-001","level":"C","kind":"correction","title":{"fr":"Titre *","en":"Title"},"rationale":"Raison * fin.","reference_ids":[],"question":"Question * ?","ops":[{"op":"add","target":"document","field":"references","value":{"id":"probe-ref","type":"web","citation":"Citation *","url":null,"doi":null}},{"op":"cite","target":"finding:hip/hip-dysfunctions/mobility","reference_ids":["probe-ref"]},{"op":"set","target":"finding:hip/hip-dysfunctions/mobility","field":"name.en","value":"Label *"},{"op":"set","target":"finding:hip/hip-dysfunctions/mobility/flexion","field":"name.fr","value":"*"}]}]},"changelog_raw_sha256":"55b992587e03e05c2b98e81e7c644aa852713084183a89cd2651b61f0e922cbb","changelog_sha256":"1e21c6e4509109bba34c869cf8df77f838e69cfdc85df203a71917b1cd25cca7","review_raw_sha256":"3f289d6d2f18fef8f99ea375703603f2b0f1076063f7440a7554ed081b1f63aa","review_sha256":"2fef9ccf08ff9e38ff59e7ca7b6fa4966e8f4ddea9188c666276adb0c59f41db"},{"probe":"*x","changes_file":{"changes":[{"id":"CHG-001","level":"C","kind":"correction","title":{"fr":"Titre *x","en":"Title"},"rationale":"Raison *x fin.","reference_ids":[],"question":"Question *x ?","ops":[{"op":"add","target":"document","field":"references","value":{"id":"probe-ref","type":"web","citation":"Citation *x","url":null,"doi":null}},{"op":"cite","target":"finding:hip/hip-dysfunctions/mobility","reference_ids":["probe-ref"]},{"op":"set","target":"finding:hip/hip-dysfunctions/mobility","field":"name.en","value":"Label *x"},{"op":"set","target":"finding:hip/hip-dysfunctions/mobility/flexion","field":"name.fr","value":"*x"}]}]},"changelog_raw_sha256":"bcb9602df19f1a01322e79fb099fd4031f40c3863ecf904daf6e107ceec9f8a6","changelog_sha256":"23d0c545ce56bc949d88d6ad5d99104aba582f851fc8f8689901cc57d9400399","review_raw_sha256":"cff4115989dd4c411d94893c5b1a19333169b53fd0a6c4dd668ba771f7e97332","review_sha256":"382d3968e4d03960e4cca33109a06b6af0db8cc53b8aec01cf940004ab91755e"},{"probe":"x*","changes_file":{"changes":[{"id":"CHG-001","level":"C","kind":"correction","title":{"fr":"Titre x*","en":"Title"},"rationale":"Raison x* fin.","reference_ids":[],"question":"Question x* ?","ops":[{"op":"add","target":"document","field":"references","value":{"id":"probe-ref","type":"web","citation":"Citation x*","url":null,"doi":null}}Line truncated
@@ -113,6 +113,26 @@ const PATTERNS = [
'中文',
'مرحبا',
'e\u0301',
'a &#42 b',
'&#x2A',
'&#95',
'a&#95b',
'&lt x &',
'&#65 x &',
'![a\\"b]',
'![a|b]',
'![&amp]',
'![x]',
'[[ a [[b]]',
'[[a]] [[ b [[c]]',
'a & b',
'A&B',
'x &',
'https://x.org/?a=1&pid=2',
'https://x.org/?a=1&lt=2',
'https://x.org/?a&not=2',
'https://x.org/?a=1&amp=2',
'https://x.org/?a=1&copyx=2',
'&amp;',
'# titre',
'1. item',
@@ -127,7 +147,13 @@ const PATTERNS = [
/** A seeded generator (a 32-bit LCG): the random probes are the same on every run. */
function randomProbes(count: number): string[] {
const alphabet = [
...'ab_*\\`[]()<>&#!~ :/.|"é中',
...'ab_*\\`[]()<>&#!~ :/.|"é中=;',
'&#',
'&amp',
'&lt',
'![',
'[[',
']]',
'https://',
'www.',
'**',