Fix a bug in link re-writing

In a regex, `.` means `[0-9a-zA-Z]`, not a period character. This means
that this regex worked, unless a link anchor contained `md` inside of
it. This fixes the regex to match a literal period.

Reported by @ehuss in
https://github.com/rust-lang-nursery/mdBook/pull/866#issuecomment-454595806
This commit is contained in:
Steve Klabnik 2019-01-16 15:44:51 -05:00
parent 2ddbb37f49
commit 23ac06e2eb
1 changed files with 7 additions and 1 deletions

View File

@ -69,7 +69,7 @@ pub fn id_from_content(content: &str) -> String {
fn adjust_links<'a>(event: Event<'a>, with_base: &str) -> Event<'a> { fn adjust_links<'a>(event: Event<'a>, with_base: &str) -> Event<'a> {
lazy_static! { lazy_static! {
static ref HTTP_LINK: Regex = Regex::new("^https?://").unwrap(); static ref HTTP_LINK: Regex = Regex::new("^https?://").unwrap();
static ref MD_LINK: Regex = Regex::new("(?P<link>.*).md(?P<anchor>#.*)?").unwrap(); static ref MD_LINK: Regex = Regex::new(r"(?P<link>.*)\.md(?P<anchor>#.*)?").unwrap();
} }
match event { match event {
@ -230,6 +230,12 @@ mod tests {
render_markdown("[example_anchor](example.md#anchor)", false), render_markdown("[example_anchor](example.md#anchor)", false),
"<p><a href=\"example.html#anchor\">example_anchor</a></p>\n" "<p><a href=\"example.html#anchor\">example_anchor</a></p>\n"
); );
// this anchor contains 'md' inside of it
assert_eq!(
render_markdown("[phantom data](foo.html#phantomdata)", false),
"<p><a href=\"foo.html#phantomdata\">phantom data</a></p>\n"
);
} }
#[test] #[test]