From 23ac06e2ebdc7ce566442edc8ca45cc194008905 Mon Sep 17 00:00:00 2001 From: Steve Klabnik Date: Wed, 16 Jan 2019 15:44:51 -0500 Subject: [PATCH] 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 --- src/utils/mod.rs | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/utils/mod.rs b/src/utils/mod.rs index aacdb1c7..df997d5e 100644 --- a/src/utils/mod.rs +++ b/src/utils/mod.rs @@ -69,7 +69,7 @@ pub fn id_from_content(content: &str) -> String { fn adjust_links<'a>(event: Event<'a>, with_base: &str) -> Event<'a> { lazy_static! { static ref HTTP_LINK: Regex = Regex::new("^https?://").unwrap(); - static ref MD_LINK: Regex = Regex::new("(?P.*).md(?P#.*)?").unwrap(); + static ref MD_LINK: Regex = Regex::new(r"(?P.*)\.md(?P#.*)?").unwrap(); } match event { @@ -230,6 +230,12 @@ mod tests { render_markdown("[example_anchor](example.md#anchor)", false), "

example_anchor

\n" ); + + // this anchor contains 'md' inside of it + assert_eq!( + render_markdown("[phantom data](foo.html#phantomdata)", false), + "

phantom data

\n" + ); } #[test]