diff options
| author | Ben Bridle <ben@derelict.engineering> | 2026-11-10 11:02:27 +1300 |
|---|---|---|
| committer | Ben Bridle <ben@derelict.engineering> | 2026-11-10 11:05:27 +1300 |
| commit | aecf433eb9725fb65f785d27d946fc58f7c76ea5 (patch) | |
| tree | 0e41aa7c7d4cf06c2bcc3665bd117a88b033425c /src/generate_html.rs | |
| parent | 9f8ab76c64bc9b29e994e63d5e24498d1da14bc2 (diff) | |
| download | toaster-aecf433eb9725fb65f785d27d946fc58f7c76ea5.zip | |
Preserve URL fragment on HTML redirect
The generated HTML redirect pages will now preserve any URL fragment
when redirecting to the new page, so that the URL '/page#heading' can
now redirect to '/real-page#heading'. Previously, the URL fragment
would just be dropped. The URL fragment will additionally be run
through a transformation function to match the URL style generated by
Toaster.
The meta redirect approach still works if JavaScript is not available.
Diffstat (limited to 'src/generate_html.rs')
| -rw-r--r-- | src/generate_html.rs | 23 |
1 files changed, 22 insertions, 1 deletions
diff --git a/src/generate_html.rs b/src/generate_html.rs index b19d8b7..cb2f1f3 100644 --- a/src/generate_html.rs +++ b/src/generate_html.rs @@ -27,6 +27,21 @@ const DEFAULT_TEMPLATE: &str = "\ </body> </html>"; +const SLUG_FUNCTION: &str = "\ +function to_slug(string) { + let slug = ''; + let prev = ' '; + for (let c of string.toLowerCase()) { + if (c==' ') c = '-'; + if (/[a-z0-9]/.test(c)) slug += c; + if ('_~.+/#'.includes(c)) slug += c; + if (c=='-' && prev != '-') slug += '-'; + prev = c; + } + return slug; +} +"; + pub fn generate_html(page: &Page, website: &Website) -> String { let root = page.root(); @@ -93,8 +108,14 @@ pub fn generate_html_redirect(path: &str, website: &Website) -> String { <!DOCTYPE html> <html> <head> + <script> + {SLUG_FUNCTION} + window.location.replace('{path}' + to_slug(window.location.hash)); + </script> <title>Redirecting...</title> - <meta http-equiv='refresh' content='0; url={path}'> + <noscript> + <meta http-equiv='refresh' content='0; url={path}'> + </noscript> {head} </head> </html>") |
