fix(anatomy): R5.4 guard rules & (legacy references), image references, nested padded wiki links; linear URL checks
This commit is contained in:
1 parent
6d4fdd65b2
commit
dcef2eb79c
4 files changed
+103
-15
No files matched your search
@@ -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),
|
||||
|
||||
Reference in new issue
Block a user