Implement named wikilinks

This commit is contained in:
Nat 2023-08-29 09:50:31 -07:00
parent eeceee815e
commit 584593af70
Signed by: nat
GPG Key ID: B53AB05285D710D6
1 changed files with 20 additions and 5 deletions

View File

@ -214,21 +214,36 @@ fn main() {
let link_re = Regex::new(r"\[\[(?<link>[^\[\]]+)\]\]").unwrap();
for page in page_map.values() {
let original_content = content_map.get(&page.id).unwrap();
for cap in link_re.captures_iter(original_content.as_str()) {
let name = String::from(cap.name("link").unwrap().as_str().trim().to_lowercase());
let link_content = cap.name("link").unwrap().as_str();
let page_id = match link_content.split_once("|") {
// It's a named wikilink
Some((fst, _snd)) => fst,
let incoming_pages = incoming_map.get_mut(&name).unwrap();
// It's a regular wikilink
None => link_content,
}.trim().to_lowercase();
let incoming_pages = incoming_map.get_mut(&page_id).unwrap();
incoming_pages.push(page);
}
content_map.insert(page.id.clone(), link_re.replace_all(original_content.as_str(), |caps: &Captures| {
let permalink = page_map.get(&caps.name("link").unwrap().as_str().trim().to_lowercase())
let link_content = caps.name("link").unwrap().as_str();
let (page_id, link_text) = match link_content.split_once("|") {
// It's a named wikilink
Some((fst, snd)) => (fst.trim().to_lowercase(), snd),
// It's a regular wikilink
None => (link_content.trim().to_lowercase(), link_content)
};
let permalink = page_map.get(&page_id)
.unwrap()
.permalink.as_str();
format!("<a href='{}'>{}</a>", permalink, &caps["link"])
format!("<a href='{}'>{}</a>", permalink, link_text)
}).to_string());
}