Ensure links are case and trailing/leading whitespace insensitive
This commit is contained in:
parent
f9f1a16e01
commit
eeceee815e
23
src/main.rs
23
src/main.rs
|
@ -23,6 +23,7 @@ struct Frontmatter {
|
||||||
#[derive(Serialize)]
|
#[derive(Serialize)]
|
||||||
struct Page {
|
struct Page {
|
||||||
title: String,
|
title: String,
|
||||||
|
id: String,
|
||||||
permalink: String,
|
permalink: String,
|
||||||
data: Option<serde_yaml::Value>,
|
data: Option<serde_yaml::Value>,
|
||||||
}
|
}
|
||||||
|
@ -188,6 +189,7 @@ fn main() {
|
||||||
};
|
};
|
||||||
|
|
||||||
let page = Page {
|
let page = Page {
|
||||||
|
id: parsed_frontmatter.title.trim().to_lowercase(),
|
||||||
title: parsed_frontmatter.title,
|
title: parsed_frontmatter.title,
|
||||||
permalink: match parsed_frontmatter.permalink {
|
permalink: match parsed_frontmatter.permalink {
|
||||||
Some(p) => p,
|
Some(p) => p,
|
||||||
|
@ -196,33 +198,33 @@ fn main() {
|
||||||
data: parsed_frontmatter.data
|
data: parsed_frontmatter.data
|
||||||
};
|
};
|
||||||
|
|
||||||
if page_map.contains_key(&page.title) {
|
if page_map.contains_key(&page.id) {
|
||||||
println!("calathea: duplicate title '{}'. Page titles must be unique.", page.title);
|
println!("calathea: duplicate title '{}'. Page titles must be unique.", page.title);
|
||||||
process::exit(1);
|
process::exit(1);
|
||||||
}
|
}
|
||||||
|
|
||||||
content_map.insert(page.title.clone(), file_content);
|
content_map.insert(page.id.clone(), file_content);
|
||||||
|
|
||||||
incoming_map.insert(page.title.clone(), Vec::new());
|
incoming_map.insert(page.id.clone(), Vec::new());
|
||||||
|
|
||||||
page_map.insert(page.title.clone(), page);
|
page_map.insert(page.id.clone(), page);
|
||||||
}
|
}
|
||||||
|
|
||||||
// Scan for and render wikilinks
|
// Scan for and render wikilinks
|
||||||
let link_re = Regex::new(r"\[\[(?<link>[^\[\]]+)\]\]").unwrap();
|
let link_re = Regex::new(r"\[\[(?<link>[^\[\]]+)\]\]").unwrap();
|
||||||
for page in page_map.values() {
|
for page in page_map.values() {
|
||||||
let original_content = content_map.get(&page.title).unwrap();
|
let original_content = content_map.get(&page.id).unwrap();
|
||||||
|
|
||||||
for cap in link_re.captures_iter(original_content.as_str()) {
|
for cap in link_re.captures_iter(original_content.as_str()) {
|
||||||
let name = String::from(cap.name("link").unwrap().as_str());
|
let name = String::from(cap.name("link").unwrap().as_str().trim().to_lowercase());
|
||||||
|
|
||||||
let incoming_pages = incoming_map.get_mut(&name).unwrap();
|
let incoming_pages = incoming_map.get_mut(&name).unwrap();
|
||||||
|
|
||||||
incoming_pages.push(page);
|
incoming_pages.push(page);
|
||||||
}
|
}
|
||||||
|
|
||||||
content_map.insert(page.title.clone(), link_re.replace_all(original_content.as_str(), |caps: &Captures| {
|
content_map.insert(page.id.clone(), link_re.replace_all(original_content.as_str(), |caps: &Captures| {
|
||||||
let permalink = page_map.get(&caps["link"])
|
let permalink = page_map.get(&caps.name("link").unwrap().as_str().trim().to_lowercase())
|
||||||
.unwrap()
|
.unwrap()
|
||||||
.permalink.as_str();
|
.permalink.as_str();
|
||||||
|
|
||||||
|
@ -243,13 +245,14 @@ fn main() {
|
||||||
md_options.extension.front_matter_delimiter = Some("---".to_owned());
|
md_options.extension.front_matter_delimiter = Some("---".to_owned());
|
||||||
|
|
||||||
for page in page_map.values() {
|
for page in page_map.values() {
|
||||||
let incoming_pages = incoming_map.get(&page.title).unwrap();
|
let incoming_pages = incoming_map.get(&page.id).unwrap();
|
||||||
let content = content_map.get(&page.title).unwrap();
|
let content = content_map.get(&page.id).unwrap();
|
||||||
|
|
||||||
let globals = liquid::object!({
|
let globals = liquid::object!({
|
||||||
"content": markdown_to_html(&content, &md_options),
|
"content": markdown_to_html(&content, &md_options),
|
||||||
"incoming": incoming_pages,
|
"incoming": incoming_pages,
|
||||||
"title": page.title,
|
"title": page.title,
|
||||||
|
"id": page.id,
|
||||||
"permalink": page.permalink,
|
"permalink": page.permalink,
|
||||||
"data": page.data,
|
"data": page.data,
|
||||||
});
|
});
|
||||||
|
|
Loading…
Reference in New Issue