-
Notifications
You must be signed in to change notification settings - Fork 971
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add heuristic checking for HTML anchors (#1716)
* Add heuristic checking for HTML anchors Previously only anchors specified or generated in markdown could be linked to, without complaint from the link checker. We now use a simple heuristic check for `name` or `id` attributes. Duplicate code has been refactored and all XML anchor checks updated to use regex rather than substring match. * Fix regexp and refactor
- Loading branch information
Showing
9 changed files
with
65 additions
and
24 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,6 @@ | ||
pub mod de; | ||
pub mod fs; | ||
pub mod links; | ||
pub mod minify; | ||
pub mod net; | ||
pub mod site; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
use regex::Regex; | ||
|
||
|
||
pub fn has_anchor_id(content: &str, anchor: &str) -> bool { | ||
let checks = anchor_id_checks(anchor); | ||
checks.is_match(content) | ||
} | ||
|
||
fn anchor_id_checks(anchor:&str) -> Regex { | ||
Regex::new( | ||
&format!(r#" (?i)(id|name) *= *("|')*{}("|'| |>)+"#, anchor) | ||
).unwrap() | ||
} | ||
|
||
|
||
#[cfg(test)] | ||
mod tests{ | ||
use super::anchor_id_checks; | ||
|
||
fn check(anchor:&str, content:&str) -> bool { | ||
anchor_id_checks(anchor).is_match(content) | ||
} | ||
|
||
#[test] | ||
fn matchers () { | ||
let m = |content| {check("fred", content)}; | ||
|
||
// Canonical match/non match | ||
assert!(m(r#"<a name="fred">"#)); | ||
assert!(m(r#"<a id="fred">"#)); | ||
assert!(!m(r#"<a name="george">"#)); | ||
|
||
// Whitespace variants | ||
assert!(m(r#"<a id ="fred">"#)); | ||
assert!(m(r#"<a id = "fred">"#)); | ||
assert!(m(r#"<a id="fred" >"#)); | ||
assert!(m(r#"<a id="fred" >"#)); | ||
|
||
// Quote variants | ||
assert!(m(r#"<a id='fred'>"#)); | ||
assert!(m(r#"<a id=fred>"#)); | ||
|
||
// Case variants | ||
assert!(m(r#"<a ID="fred">"#)); | ||
assert!(m(r#"<a iD="fred">"#)); | ||
} | ||
} |