From 7db8a2bbafc4d1efd6b5567b583a55d6c19b2236 Mon Sep 17 00:00:00 2001 From: Morgan Creekmore Date: Thu, 4 Jan 2024 18:28:17 -0600 Subject: [PATCH] Add option to include date in search index #2394 --- components/config/src/config/search.rs | 3 ++ components/search/src/lib.rs | 40 +++++++++++++++++-- .../getting-started/configuration.md | 2 + 3 files changed, 42 insertions(+), 3 deletions(-) diff --git a/components/config/src/config/search.rs b/components/config/src/config/search.rs index 6f64cc20e3..3ce6878cd2 100644 --- a/components/config/src/config/search.rs +++ b/components/config/src/config/search.rs @@ -22,6 +22,8 @@ pub struct Search { /// Includes the description in the search index. When the site becomes too large, you can switch /// to that instead. `false` by default pub include_description: bool, + /// Include the RFC3339 datetime of the page in the search index. `false` by default. + pub include_date: bool, /// Include the path of the page in the search index. `false` by default. pub include_path: bool, /// Foramt of the search index to be produced. Javascript by default @@ -35,6 +37,7 @@ impl Default for Search { include_content: true, include_description: false, include_path: false, + include_date: false, truncate_content_length: None, index_format: Default::default(), } diff --git a/components/search/src/lib.rs b/components/search/src/lib.rs index 073eb09307..9d6b3e61a6 100644 --- a/components/search/src/lib.rs +++ b/components/search/src/lib.rs @@ -1,5 +1,7 @@ use std::collections::{HashMap, HashSet}; +use libs::time::format_description::well_known::Rfc3339; +use libs::time::OffsetDateTime; use libs::ammonia; use libs::elasticlunr::{lang, Index, IndexBuilder}; use libs::once_cell::sync::Lazy; @@ -35,6 +37,10 @@ fn build_fields(search_config: &Search, mut index: IndexBuilder) -> IndexBuilder index = index.add_field("description"); } + if search_config.include_date { + index = index.add_field("date") + } + if search_config.include_path { index = index.add_field_with_tokenizer("path", Box::new(path_tokenizer)); } @@ -57,6 +63,7 @@ fn fill_index( search_config: &Search, title: &Option, description: &Option, + datetime: &Option, path: &str, content: &str, ) -> Vec { @@ -70,6 +77,14 @@ fn fill_index( row.push(description.clone().unwrap_or_default()); } + if search_config.include_date { + if let Some(date) = datetime { + if let Ok(d) = date.format(&Rfc3339) { + row.push(d); + } + } + } + if search_config.include_path { row.push(path.to_string()); } @@ -133,6 +148,7 @@ fn add_section_to_index( search_config, §ion.meta.title, §ion.meta.description, + &None, §ion.path, §ion.content, ), @@ -151,6 +167,7 @@ fn add_section_to_index( search_config, &page.meta.title, &page.meta.description, + &page.meta.datetime, &page.path, &page.content, ), @@ -192,7 +209,7 @@ mod tests { let path = "/a/page/".to_string(); let content = "Some content".to_string(); - let res = fill_index(&config.search, &title, &description, &path, &content); + let res = fill_index(&config.search, &title, &description, &None, &path, &content); assert_eq!(res.len(), 2); assert_eq!(res[0], title.unwrap()); assert_eq!(res[1], content); @@ -207,7 +224,7 @@ mod tests { let path = "/a/page/".to_string(); let content = "Some content".to_string(); - let res = fill_index(&config.search, &title, &description, &path, &content); + let res = fill_index(&config.search, &title, &description, &None, &path, &content); assert_eq!(res.len(), 3); assert_eq!(res[0], title.unwrap()); assert_eq!(res[1], description.unwrap()); @@ -223,9 +240,26 @@ mod tests { let path = "/a/page/".to_string(); let content = "Some content".to_string(); - let res = fill_index(&config.search, &title, &description, &path, &content); + let res = fill_index(&config.search, &title, &description, &None, &path, &content); assert_eq!(res.len(), 2); assert_eq!(res[0], title.unwrap()); assert_eq!(res[1], content[..5]); } + + #[test] + fn can_fill_index_date() { + let mut config = Config::default(); + config.search.include_date = true; + let title = Some("A title".to_string()); + let description = Some("A description".to_string()); + let path = "/a/page/".to_string(); + let content = "Some content".to_string(); + let datetime = Some(OffsetDateTime::parse("2023-01-31T00:00:00Z", &Rfc3339).unwrap()); + + let res = fill_index(&config.search, &title, &description, &datetime, &path, &content); + assert_eq!(res.len(), 3); + assert_eq!(res[0], title.unwrap()); + assert_eq!(res[1], "2023-01-31T00:00:00Z"); + assert_eq!(res[2], content); + } } diff --git a/docs/content/documentation/getting-started/configuration.md b/docs/content/documentation/getting-started/configuration.md index fbf4c01f55..d8ec482d5d 100644 --- a/docs/content/documentation/getting-started/configuration.md +++ b/docs/content/documentation/getting-started/configuration.md @@ -169,6 +169,8 @@ paths_keep_dates = false include_title = true # Whether to include the description of the page/section in the index include_description = false +# Whether to include the RFC3339 datetime of the page in the search index +include_date = false # Whether to include the path of the page/section in the index include_path = false # Whether to include the rendered content of the page/section in the index