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
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
This commit is contained in:
11
src/lib.rs
11
src/lib.rs
@@ -589,7 +589,7 @@ impl<'a> Linker<'a> {
|
|||||||
pub fn new(historian: &Historian) -> Linker {
|
pub fn new(historian: &Historian) -> Linker {
|
||||||
Linker {
|
Linker {
|
||||||
historian,
|
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 {
|
pub fn resolve_links_for_edit(&self, page: &Page, edit: &Edit) -> String {
|
||||||
let mut content = edit.content.to_owned();
|
let mut content = edit.content.to_owned();
|
||||||
for (link_full, [link]) in self.link_regex.captures_iter(&content.to_owned()).map(|c| c.extract()) {
|
for capture in self.link_regex.captures_iter(&content.to_owned()) {
|
||||||
if let Some(resolved_link) = self.resolve_link(link) {
|
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();
|
let mut absolute_link_path = self.historian.source_root.to_owned();
|
||||||
absolute_link_path.push(&resolved_link);
|
absolute_link_path.push(&resolved_link);
|
||||||
let relative_link_path = diff_paths(&absolute_link_path, &page.path.parent().unwrap()).unwrap();
|
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()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user