Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

convert relative urls to absolute #165

Merged
merged 1 commit into from
May 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/bartholomew.rs
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,9 @@ pub fn render(req: Request) -> Result<Response> {

let status_opt = doc.head.status;
let loc_opt = doc.head.redirect.clone();

if doc.head.path_info.is_none() {
doc.head.path_info = Some(path_info.clone());
}
match loc_opt {
Some(location) => {
let status =
Expand Down
80 changes: 46 additions & 34 deletions src/content.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ pub struct Head {
pub redirect: Option<String>,
/// If set to true, this will enable shortcode support for the document
pub enable_shortcodes: Option<bool>,
/// This provides the path info the file
/// If empty, the path info is filled in using the request url
pub path_info: Option<String>,
/// A map of string/string pairs that are user-customizable.
pub extra: Option<HashMap<String, String>>,
}
Expand Down Expand Up @@ -251,32 +254,40 @@ fn visit_files(dir: PathBuf, cb: &mut dyn FnMut(&DirEntry)) -> anyhow::Result<()
}

/// Translate links of the form "./<name>.md" and "<name>.md" into "/<name>"
fn maybe_translate_relative_link(dest: markdown::CowStr) -> markdown::CowStr {
fn maybe_translate_relative_link(dest: markdown::CowStr, path_info: String) -> markdown::CowStr {
//if absolute url, return as is
let re = Regex::new(r"^(?:[a-z+]+:)?//").unwrap();
if re.is_match(&dest) {
return dest;
};

let firstleg = dest.replace(".md", "");

let result = match firstleg.strip_prefix("./") {
Some(val) => val,
_ => firstleg.as_str(),
};

// If urls are not urls from the root of the website or anchor tags
// Convert them to absolute urls to avoid issues where a url end with a slash
if !result.starts_with('/') && !result.starts_with('#') {
let mut path = PathBuf::from(path_info);
path.pop();
path = path.join(result);
return path.to_str().unwrap_or_default().to_owned().into();
}

result.to_string().into()
}

/// Look for relative Markdown links of the form "./<name>.md" or "<name>.md" and translate them into "/<name>"
///
/// See also `maybe_translate_relative_link`.
fn translate_relative_links(event: markdown::Event) -> markdown::Event {
fn translate_relative_links(event: markdown::Event, path_info: String) -> markdown::Event {
match event {
markdown::Event::Start(markdown::Tag::Link(markdown::LinkType::Inline, dest, title)) => {
markdown::Event::Start(markdown::Tag::Link(
markdown::LinkType::Inline,
maybe_translate_relative_link(dest),
maybe_translate_relative_link(dest, path_info),
title,
))
}
Expand Down Expand Up @@ -359,35 +370,36 @@ impl Content {
let mut buf = String::new();
// Might as well turn on all the lights on the Christmas tree
let opt = markdown::Options::all();
let out: String;
let parser = match self.head.enable_shortcodes {
Some(true) => {
let mut handlebars = Handlebars::new();
// Initialize the custom rhai engine with helpers
let rhai_engine = custom_rhai_engine_init();
// Make handlebars use the custom engine
handlebars.set_engine(rhai_engine);

let _ = &self.load_shortcodes_dir(&mut handlebars, shortcodes_dir);

// don't escape HTML so that rhai scripts can return html that will
// be rendered as HTML
handlebars.register_escape_fn(handlebars::no_escape);

// run the markdown through the template engine to
// enable any script helpers
out = handlebars
.render_template(&self.body, &{})
.unwrap_or_else(|e| {
// print the error
eprintln!("Error rendering markdown: {e}");
// return nothing
e.to_string()
});
markdown::Parser::new_ext(&out, opt).map(translate_relative_links)
}
_ => markdown::Parser::new_ext(&self.body, opt).map(translate_relative_links),
};
let mut out: String = self.body.to_owned();
if let Some(true) = self.head.enable_shortcodes {
let mut handlebars = Handlebars::new();

let rhai_engine = custom_rhai_engine_init();
// Make handlebars use the custom engine
handlebars.set_engine(rhai_engine);

let _ = &self.load_shortcodes_dir(&mut handlebars, shortcodes_dir);

// don't escape HTML so that rhai scripts can return html that will
// be rendered as HTML
handlebars.register_escape_fn(handlebars::no_escape);

// run the markdown through the template engine to
// enable any script helpers
out = handlebars
.render_template(&self.body, &{})
.unwrap_or_else(|e| {
// print the error
eprintln!("Error rendering markdown: {e}");
// return nothing
e.to_string()
});
}

let parser = markdown::Parser::new_ext(&out, opt).map(|event| {
translate_relative_links(event, self.head.path_info.clone().unwrap_or_default())
});

markdown::html::push_html(&mut buf, parser);

buf
Expand Down Expand Up @@ -420,7 +432,7 @@ mod test {
#[test]
fn $name() {
let (input, expected, msg) = $value;
assert_eq!(markdown::CowStr::from(expected), maybe_translate_relative_link(markdown::CowStr::from(input)), "{}", msg);
assert_eq!(markdown::CowStr::from(expected), maybe_translate_relative_link(markdown::CowStr::from(input), "".to_owned()), "{}", msg);
}
)*
}
Expand Down
1 change: 1 addition & 0 deletions src/template.rs
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,7 @@ pub fn error_values(title: &str, msg: &str) -> PageValues {
status: None,
redirect: None,
enable_shortcodes: None,
path_info: None,
},
body: msg.to_string(),
published: true,
Expand Down