Skip to content

Commit

Permalink
fixed multiple footnote link bug
Browse files Browse the repository at this point in the history
  • Loading branch information
Foxicution committed Feb 7, 2025
1 parent 23f4d84 commit 25295b1
Showing 1 changed file with 64 additions and 2 deletions.
66 changes: 64 additions & 2 deletions src/vault/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -765,10 +765,13 @@ impl Reference {
})
});

static FOOTNOTE_LINK_RE: Lazy<Regex> =
Lazy::new(|| Regex::new(r"[^\[](?<full>\[(?<index>\^[^\[\] ]+)\])[^\:]").unwrap());
static FOOTNOTE_LINK_RE: Lazy<Regex> = Lazy::new(|| {Regex::new(r"(?<start>\[?)(?<full>\[(?<index>\^[^\[\] ]+)\])(?<end>:?)").unwrap()});
let footnote_references = FOOTNOTE_LINK_RE
.captures_iter(text)
.filter(|capture| matches!(
(capture.name("start"), capture.name("end")),
(Some(start), Some(end)) if !start.as_str().starts_with('[') && !end.as_str().ends_with(':'))
)
.flat_map(
|capture| match (capture.name("full"), capture.name("index")) {
(Some(full), Some(index)) => Some((full, index)),
Expand Down Expand Up @@ -1969,6 +1972,65 @@ mod vault_tests {
assert_eq!(parsed, expected)
}

#[test]
fn multi_footnote_link_parsing() {
let text = "This is a footnote[^1][^2][^3]
[^1]: This is not
[^2]: This is not
[^3]: This is not";
let parsed = Reference::new(text, "test.md").collect_vec();
let expected = vec![
Footnote(ReferenceData {
reference_text: "^1".into(),
range: tower_lsp::lsp_types::Range {
start: tower_lsp::lsp_types::Position {
line: 0,
character: 18,
},
end: tower_lsp::lsp_types::Position {
line: 0,
character: 22,
},
}
.into(),
..ReferenceData::default()
}),
Footnote(ReferenceData {
reference_text: "^2".into(),
range: tower_lsp::lsp_types::Range {
start: tower_lsp::lsp_types::Position {
line: 0,
character: 22,
},
end: tower_lsp::lsp_types::Position {
line: 0,
character: 26,
},
}
.into(),
..ReferenceData::default()
}),
Footnote(ReferenceData {
reference_text: "^3".into(),
range: tower_lsp::lsp_types::Range {
start: tower_lsp::lsp_types::Position {
line: 0,
character: 26,
},
end: tower_lsp::lsp_types::Position {
line: 0,
character: 30,
},
}
.into(),
..ReferenceData::default()
}),
];

assert_eq!(parsed, expected)
}

#[test]
fn link_parsing_with_png() {
let text = "This is a png [[link.png]] [[link|display.png]]";
Expand Down

0 comments on commit 25295b1

Please sign in to comment.