Skip to content

Commit 2282042

Browse files
DrakeRichardsFeel-ix-343
authored andcommitted
fix: correctly parse markdown links without including trailing parentheses
Fixes #260
1 parent 7e97774 commit 2282042

File tree

1 file changed

+36
-1
lines changed

1 file changed

+36
-1
lines changed

src/vault/mod.rs

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -744,7 +744,7 @@ impl Reference {
744744
});
745745

746746
static MD_LINK_RE: Lazy<Regex> = Lazy::new(|| {
747-
Regex::new(r"\[(?<display>[^\[\]\.]*)\]\(<?(?<filepath>(\.?\/)?[^\[\]\|\.\#<>]+)?(?<ending>\.[^\# <>]+)?(\#(?<infileref>[^\[\]\.\|<>]+))?>?\)")
747+
Regex::new(r"\[(?<display>[^\[\]\.]*?)\]\(<?(?<filepath>(\.?\/)?[^\[\]\|\.\#<>]+?)?(?<ending>\.[^\# <>]+?)?(\#(?<infileref>[^\[\]\.\|<>]+?))?>?\)")
748748
.expect("MD Link Not Constructing")
749749
}); // [display](relativePath)
750750

@@ -1956,6 +1956,41 @@ mod vault_tests {
19561956
assert_eq!(parsed, expected)
19571957
}
19581958

1959+
#[test]
1960+
fn md_link_with_trailing_parentheses_parsing() {
1961+
// [Issue 260](https://github.com/Feel-ix-343/markdown-oxide/issues/260) covers an issue with parentheses on a new line after a mdlink being parsed as another link.
1962+
1963+
let text = r#"
1964+
Buggy cross [link](path/to/link#^index1):
1965+
1966+
(this causes bug)
1967+
"#;
1968+
1969+
let parsed = Reference::new(text, "test.md").collect_vec();
1970+
1971+
let expected = vec![Reference::MDIndexedBlockLink(
1972+
ReferenceData {
1973+
reference_text: "path/to/link#^index1".into(),
1974+
display_text: Some("link".into()),
1975+
range: Range {
1976+
start: Position {
1977+
line: 1,
1978+
character: 24,
1979+
},
1980+
end: Position {
1981+
line: 1,
1982+
character: 52,
1983+
},
1984+
}
1985+
.into(),
1986+
},
1987+
"path/to/link".into(),
1988+
"index1".into(),
1989+
)];
1990+
1991+
assert_eq!(parsed, expected);
1992+
}
1993+
19591994
#[test]
19601995
fn footnote_link_parsing() {
19611996
let text = "This is a footnote[^1]

0 commit comments

Comments
 (0)