add support for hard-coded redirects in config.json

This commit is contained in:
Peter Cai 2020-04-13 16:50:36 +08:00
parent 0c61b2085c
commit e727bce188
No known key found for this signature in database
GPG Key ID: 71F5FB4E4F3FD54F
3 changed files with 29 additions and 3 deletions

View File

@ -66,6 +66,10 @@ This is the main configuration file. The file will be compiled statically into t
"description": "<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

View File

@ -104,9 +104,24 @@ async fn proxy_remote_image(req: Request, url: Url) -> MyResult<Response> {
async fn default_route(_req: Request, url: Url) -> MyResult<Response> {
// 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,

View File

@ -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<HashMap<String, String>>
}
include!(concat!(env!("OUT_DIR"), "/build_timestamp.rs"));