From 01cd799f688ad39cfb03ede3e149512d1c8faef6 Mon Sep 17 00:00:00 2001 From: Ben Bridle Date: Wed, 15 Jan 2025 10:00:31 +1300 Subject: Allow the use of site-local absolute URLs in external links External links with no protocol that start with a forward-slash are interpreted as being links to a file in the generated site. To make these work with the relative-paths paradigm of the generator, the slash is replaced with the page path-to-root to convert it to a relative path. This commit also fixes an issue where unlabelled links with no protocol were being given an empty label. --- src/generate_html.rs | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) (limited to 'src/generate_html.rs') diff --git a/src/generate_html.rs b/src/generate_html.rs index d6624b8..3486352 100644 --- a/src/generate_html.rs +++ b/src/generate_html.rs @@ -281,9 +281,18 @@ fn line_to_html(line: &Line, page: &Page, website: &Website) -> String { let mut label = label.to_string(); // Strip the protocol from the path when using the path as a label. if label.is_empty() { + label = path.to_string(); for protocol in ["mailto://", "http://", "https://"] { if let Some(stripped) = path.strip_prefix(protocol) { - label = stripped.to_string(); } } } + label = stripped.to_string(); + } + } + } + // Support absolute local paths. + let path = match path.strip_prefix('/') { + Some(stripped) => format!("{}{stripped}", page.root()), + None => path.to_string(), + }; let label = sanitize_text(&label); html.push_str(&format!("{label}")); } -- cgit v1.2.3-70-g09d2