From 7b562fcd2c4c967d7c783bee646a2bfbab01878d Mon Sep 17 00:00:00 2001 From: Andrew Browne Date: Sat, 6 Aug 2022 14:07:02 -0700 Subject: [PATCH] Add slugify.paths_keep_dates option. Update docs. Manual testing: Existing test_site/config.toml http://127.0.0.1:1111/posts/a-post-with-dates ==== --- a/test_site/config.toml +++ b/test_site/config.toml @@ -18,6 +18,7 @@ extra_syntaxes_and_themes = ["syntaxes", "highlight_themes"] [slugify] paths = "on" +paths_keep_dates = true taxonomies = "on" anchors = "on" http://127.0.0.1:1111/posts/2016-10-08-a-post-with-dates ==== --- a/test_site/config.toml +++ b/test_site/config.toml @@ -17,7 +17,8 @@ highlight_theme = "custom_gruvbox" extra_syntaxes_and_themes = ["syntaxes", "highlight_themes"] [slugify] -paths = "on" +paths = "off" +paths_keep_dates = true taxonomies = "on" anchors = "on" http://127.0.0.1:1111/posts/2016-10-08_a-post-with-dates ==== --- a/test_site/config.toml +++ b/test_site/config.toml @@ -17,7 +17,8 @@ highlight_theme = "custom_gruvbox" extra_syntaxes_and_themes = ["syntaxes", "highlight_themes"] [slugify] -paths = "on" +paths = "safe" +paths_keep_dates = true taxonomies = "on" anchors = "on" http://127.0.0.1:1111/posts/2016-10-08_a-post-with-dates --- components/config/src/config/mod.rs | 20 +++++++++++++++++++ components/config/src/config/slugify.rs | 1 + components/content/src/page.rs | 4 +++- docs/content/documentation/content/page.md | 2 ++ .../getting-started/configuration.md | 4 ++++ 5 files changed, 30 insertions(+), 1 deletion(-) diff --git a/components/config/src/config/mod.rs b/components/config/src/config/mod.rs index 32211667..b41ccc27 100644 --- a/components/config/src/config/mod.rs +++ b/components/config/src/config/mod.rs @@ -658,10 +658,30 @@ anchors = "off" let config = Config::parse(config_str).unwrap(); assert_eq!(config.slugify.paths, SlugifyStrategy::On); + assert_eq!(config.slugify.paths_keep_dates, false); assert_eq!(config.slugify.taxonomies, SlugifyStrategy::Safe); assert_eq!(config.slugify.anchors, SlugifyStrategy::Off); } + #[test] + fn slugify_paths_keep_dates() { + let config_str = r#" +title = "My site" +base_url = "example.com" + +[slugify] +paths_keep_dates = true +taxonomies = "off" +anchors = "safe" + "#; + + let config = Config::parse(config_str).unwrap(); + assert_eq!(config.slugify.paths, SlugifyStrategy::On); + assert_eq!(config.slugify.paths_keep_dates, true); + assert_eq!(config.slugify.taxonomies, SlugifyStrategy::Off); + assert_eq!(config.slugify.anchors, SlugifyStrategy::Safe); + } + #[test] fn cannot_overwrite_theme_mapping_with_invalid_type() { let config_str = r#" diff --git a/components/config/src/config/slugify.rs b/components/config/src/config/slugify.rs index c22065b3..1a67376c 100644 --- a/components/config/src/config/slugify.rs +++ b/components/config/src/config/slugify.rs @@ -6,6 +6,7 @@ use utils::slugs::SlugifyStrategy; #[serde(default)] pub struct Slugify { pub paths: SlugifyStrategy, + pub paths_keep_dates: bool, pub taxonomies: SlugifyStrategy, pub anchors: SlugifyStrategy, } diff --git a/components/content/src/page.rs b/components/content/src/page.rs index 57d9812f..7501a4c6 100644 --- a/components/content/src/page.rs +++ b/components/content/src/page.rs @@ -126,7 +126,9 @@ impl Page { }; if let Some(ref caps) = RFC3339_DATE.captures(&file_path_for_slug) { - slug_from_dated_filename = Some(caps.name("slug").unwrap().as_str().to_string()); + if !config.slugify.paths_keep_dates { + slug_from_dated_filename = Some(caps.name("slug").unwrap().as_str().to_string()); + } if page.meta.date.is_none() { page.meta.date = Some(caps.name("datetime").unwrap().as_str().to_string()); page.meta.date_to_datetime(); diff --git a/docs/content/documentation/content/page.md b/docs/content/documentation/content/page.md index 0f018e04..18d0a8cb 100644 --- a/docs/content/documentation/content/page.md +++ b/docs/content/documentation/content/page.md @@ -16,11 +16,13 @@ create a **page** at `[base_url]/about`). If the file is given any name _other_ than `index.md` or `_index.md`, then it will create a page with that name (without the `.md`). For example, naming a file in the root of your content directory `about.md` would create a page at `[base_url]/about`. + Another exception to this rule is that a filename starting with a datetime (YYYY-mm-dd or [an RFC3339 datetime](https://www.ietf.org/rfc/rfc3339.txt)) followed by an underscore (`_`) or a dash (`-`) will use that date as the page date, unless already set in the front matter. The page name will be anything after `_`/`-`, so the file `2018-10-10-hello-world.md` will be available at `[base_url]/hello-world`. Note that the full RFC3339 datetime contains colons, which is not a valid character in a filename on Windows. +This behavior can be disabled by setting `slugify.paths_keep_date` to `true` (the default is `false`). Note that a `_` separating the date would be slugified into a `-` with the default value for `slugify.paths` of `"on"`. As you can see, creating an `about.md` file is equivalent to creating an `about/index.md` file. The only difference between the two methods is that creating diff --git a/docs/content/documentation/getting-started/configuration.md b/docs/content/documentation/getting-started/configuration.md index f8f2d473..92675197 100644 --- a/docs/content/documentation/getting-started/configuration.md +++ b/docs/content/documentation/getting-started/configuration.md @@ -142,6 +142,10 @@ external_level = "error" paths = "on" taxonomies = "on" anchors = "on" +# Whether to remove date prefixes for page path slugs. +# For example, content/posts/2016-10-08_a-post-with-dates.md => posts/a-post-with-dates +# When true, content/posts/2016-10-08_a-post-with-dates.md => posts/2016-10-08-a-post-with-dates +paths_keep_dates = false [search] # Whether to include the title of the page/section in the index