diff options
author | Ben Bridle <ben@derelict.engineering> | 2025-01-15 10:00:31 +1300 |
---|---|---|
committer | Ben Bridle <ben@derelict.engineering> | 2025-01-15 10:01:19 +1300 |
commit | 01cd799f688ad39cfb03ede3e149512d1c8faef6 (patch) | |
tree | 472a84dacd34f3564474bf531271690634e77088 /src/generate_html.rs | |
parent | 4476a2ed495b712449987299aab8453456c5a748 (diff) | |
download | toaster-01cd799f688ad39cfb03ede3e149512d1c8faef6.zip |
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.
Diffstat (limited to 'src/generate_html.rs')
-rw-r--r-- | src/generate_html.rs | 11 |
1 files changed, 10 insertions, 1 deletions
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!("<a href='{path}' class='external'>{label}</a>")); } |