Add ability for wikilinks to have alternate labels. With this historian should be at feature parity with the legacy megalink script.
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful

This commit is contained in:
2025-07-03 22:46:49 -05:00
parent b1da5d5798
commit 4de2b399f5

View File

@@ -589,7 +589,7 @@ impl<'a> Linker<'a> {
pub fn new(historian: &Historian) -> Linker {
Linker {
historian,
link_regex: Regex::new(r"\[\[([\w\s\|]+)\]\]").unwrap()
link_regex: Regex::new(r"\[\[(?<link>[\w\s]+)(?:\|(?<label>[\w\s]+))?\]\]").unwrap()
}
}
@@ -642,12 +642,15 @@ impl<'a> Linker<'a> {
pub fn resolve_links_for_edit(&self, page: &Page, edit: &Edit) -> String {
let mut content = edit.content.to_owned();
for (link_full, [link]) in self.link_regex.captures_iter(&content.to_owned()).map(|c| c.extract()) {
if let Some(resolved_link) = self.resolve_link(link) {
for capture in self.link_regex.captures_iter(&content.to_owned()) {
let link = capture.name("link").unwrap();
if let Some(resolved_link) = self.resolve_link(link.as_str()) {
let link_full = capture.get(0).unwrap().as_str();
let label = capture.name("label").unwrap_or(link).as_str();
let mut absolute_link_path = self.historian.source_root.to_owned();
absolute_link_path.push(&resolved_link);
let relative_link_path = diff_paths(&absolute_link_path, &page.path.parent().unwrap()).unwrap();
content = content.replace(link_full, &format!("[{}](<{}>)", link, relative_link_path.display()));
content = content.replace(link_full, &format!("[{}](<{}>)", label, relative_link_path.display()));
}
}