Skip to content

Commit

Permalink
Add option to include date in search index
Browse files Browse the repository at this point in the history
  • Loading branch information
gamingrobot committed Jan 5, 2024
1 parent e5f3026 commit 732a0e9
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 3 deletions.
3 changes: 3 additions & 0 deletions components/config/src/config/search.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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(),
}
Expand Down
40 changes: 37 additions & 3 deletions components/search/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ use std::collections::{HashMap, HashSet};
use libs::ammonia;
use libs::elasticlunr::{lang, Index, IndexBuilder};
use libs::once_cell::sync::Lazy;
use libs::time::format_description::well_known::Rfc3339;
use libs::time::OffsetDateTime;

use config::{Config, Search};
use content::{Library, Section};
Expand Down Expand Up @@ -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));
}
Expand All @@ -57,6 +63,7 @@ fn fill_index(
search_config: &Search,
title: &Option<String>,
description: &Option<String>,
datetime: &Option<OffsetDateTime>,
path: &str,
content: &str,
) -> Vec<String> {
Expand All @@ -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());
}
Expand Down Expand Up @@ -133,6 +148,7 @@ fn add_section_to_index(
search_config,
&section.meta.title,
&section.meta.description,
&None,
&section.path,
&section.content,
),
Expand All @@ -151,6 +167,7 @@ fn add_section_to_index(
search_config,
&page.meta.title,
&page.meta.description,
&page.meta.datetime,
&page.path,
&page.content,
),
Expand Down Expand Up @@ -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);
Expand All @@ -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());
Expand All @@ -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);
}
}
2 changes: 2 additions & 0 deletions docs/content/documentation/getting-started/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 732a0e9

Please sign in to comment.