From e727bce18885a924f069e6f0845f4987560d6410 Mon Sep 17 00:00:00 2001 From: Peter Cai Date: Mon, 13 Apr 2020 16:50:36 +0800 Subject: [PATCH] add support for hard-coded redirects in config.json --- README.md | 6 ++++++ src/lib.rs | 19 +++++++++++++++++-- src/utils.rs | 7 ++++++- 3 files changed, 29 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index c5f4ba9..804b0be 100644 --- a/README.md +++ b/README.md @@ -66,6 +66,10 @@ This is the main configuration file. The file will be compiled statically into t "description": "", "plugin_identifier": "com.example.change.this.to.whatever.you.like", "posts_per_page": 5, + "redirects": { + "/foo": "/bar", + ... + }, "hljs": [ "rust", "javascript", @@ -81,6 +85,8 @@ This is the main configuration file. The file will be compiled statically into t `plugin_identifier`: Used in Standard Notes to distinguish plugins. +`redirects`: OPTIONAL. A map of URLs where the key will be mapped to the value by Paprika using 301 redirects. This is mainly useful for migration from another blogging platform. + `hljs`: An array of language support from `highlight.js` to be included in the final binary. The full `highlight.js` is notoriously huge and there's really no reason to include a bazillion languages you will never actually use in your blog posts. This will be read by `build.rs` to generate a JS shim that will load all languages in the array to the final binary via `webpack` support for `require`. Configuration: theme_config.json diff --git a/src/lib.rs b/src/lib.rs index 20f6858..6f18c62 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -104,9 +104,24 @@ async fn proxy_remote_image(req: Request, url: Url) -> MyResult { async fn default_route(_req: Request, url: Url) -> MyResult { // We assume that anything that falls into this catch-all handler - // would be either posts or 404 - // If the path doesn't end with `/`, normalize it first + // would be posts, 404, or hard-codede redirects let path = url.pathname(); + + // Handle hard-coded redirects in config first + if let Some(redirects) = &CONFIG.redirects { + if let Some(new_path) = redirects.get(&path) { + return Response::new_with_opt_str_and_init( + None, + ResponseInit::new() + .status(301) + .headers(headers!{ + "Location" => &format!("{}{}", url.origin(), new_path) + }.as_ref()) + ).internal_err(); + } + } + + // If the path doesn't end with `/`, normalize it first if !path.ends_with("/") { return Response::new_with_opt_str_and_init( None, diff --git a/src/utils.rs b/src/utils.rs index 141555e..f1a288d 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -1,6 +1,7 @@ use cfg_if::cfg_if; use serde::Deserialize; use js_sys::*; +use std::collections::HashMap; use wasm_bindgen::prelude::*; use wasm_bindgen_futures::*; use web_sys::*; @@ -154,7 +155,11 @@ pub struct Config { // Plugin identifier used for Standard Notes pub plugin_identifier: String, // How many posts to show in one page - pub posts_per_page: usize + pub posts_per_page: usize, + // Hard-coded redirects (for migrating old articles and such) + // Paths here MUST include the starting "/" + // UNLIKE in article headers + pub redirects: Option> } include!(concat!(env!("OUT_DIR"), "/build_timestamp.rs")); \ No newline at end of file