Add option to include date in search index (#2401)
This commit is contained in:
parent
8bdec67f0a
commit
a9ab3592c8
@ -22,6 +22,8 @@ pub struct Search {
|
|||||||
/// Includes the description in the search index. When the site becomes too large, you can switch
|
/// Includes the description in the search index. When the site becomes too large, you can switch
|
||||||
/// to that instead. `false` by default
|
/// to that instead. `false` by default
|
||||||
pub include_description: bool,
|
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.
|
/// Include the path of the page in the search index. `false` by default.
|
||||||
pub include_path: bool,
|
pub include_path: bool,
|
||||||
/// Foramt of the search index to be produced. Javascript by default
|
/// Foramt of the search index to be produced. Javascript by default
|
||||||
@ -35,6 +37,7 @@ impl Default for Search {
|
|||||||
include_content: true,
|
include_content: true,
|
||||||
include_description: false,
|
include_description: false,
|
||||||
include_path: false,
|
include_path: false,
|
||||||
|
include_date: false,
|
||||||
truncate_content_length: None,
|
truncate_content_length: None,
|
||||||
index_format: Default::default(),
|
index_format: Default::default(),
|
||||||
}
|
}
|
||||||
|
@ -3,6 +3,8 @@ use std::collections::{HashMap, HashSet};
|
|||||||
use libs::ammonia;
|
use libs::ammonia;
|
||||||
use libs::elasticlunr::{lang, Index, IndexBuilder};
|
use libs::elasticlunr::{lang, Index, IndexBuilder};
|
||||||
use libs::once_cell::sync::Lazy;
|
use libs::once_cell::sync::Lazy;
|
||||||
|
use libs::time::format_description::well_known::Rfc3339;
|
||||||
|
use libs::time::OffsetDateTime;
|
||||||
|
|
||||||
use config::{Config, Search};
|
use config::{Config, Search};
|
||||||
use content::{Library, Section};
|
use content::{Library, Section};
|
||||||
@ -35,6 +37,10 @@ fn build_fields(search_config: &Search, mut index: IndexBuilder) -> IndexBuilder
|
|||||||
index = index.add_field("description");
|
index = index.add_field("description");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if search_config.include_date {
|
||||||
|
index = index.add_field("date")
|
||||||
|
}
|
||||||
|
|
||||||
if search_config.include_path {
|
if search_config.include_path {
|
||||||
index = index.add_field_with_tokenizer("path", Box::new(path_tokenizer));
|
index = index.add_field_with_tokenizer("path", Box::new(path_tokenizer));
|
||||||
}
|
}
|
||||||
@ -57,6 +63,7 @@ fn fill_index(
|
|||||||
search_config: &Search,
|
search_config: &Search,
|
||||||
title: &Option<String>,
|
title: &Option<String>,
|
||||||
description: &Option<String>,
|
description: &Option<String>,
|
||||||
|
datetime: &Option<OffsetDateTime>,
|
||||||
path: &str,
|
path: &str,
|
||||||
content: &str,
|
content: &str,
|
||||||
) -> Vec<String> {
|
) -> Vec<String> {
|
||||||
@ -70,6 +77,14 @@ fn fill_index(
|
|||||||
row.push(description.clone().unwrap_or_default());
|
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 {
|
if search_config.include_path {
|
||||||
row.push(path.to_string());
|
row.push(path.to_string());
|
||||||
}
|
}
|
||||||
@ -133,6 +148,7 @@ fn add_section_to_index(
|
|||||||
search_config,
|
search_config,
|
||||||
§ion.meta.title,
|
§ion.meta.title,
|
||||||
§ion.meta.description,
|
§ion.meta.description,
|
||||||
|
&None,
|
||||||
§ion.path,
|
§ion.path,
|
||||||
§ion.content,
|
§ion.content,
|
||||||
),
|
),
|
||||||
@ -151,6 +167,7 @@ fn add_section_to_index(
|
|||||||
search_config,
|
search_config,
|
||||||
&page.meta.title,
|
&page.meta.title,
|
||||||
&page.meta.description,
|
&page.meta.description,
|
||||||
|
&page.meta.datetime,
|
||||||
&page.path,
|
&page.path,
|
||||||
&page.content,
|
&page.content,
|
||||||
),
|
),
|
||||||
@ -192,7 +209,7 @@ mod tests {
|
|||||||
let path = "/a/page/".to_string();
|
let path = "/a/page/".to_string();
|
||||||
let content = "Some content".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.len(), 2);
|
||||||
assert_eq!(res[0], title.unwrap());
|
assert_eq!(res[0], title.unwrap());
|
||||||
assert_eq!(res[1], content);
|
assert_eq!(res[1], content);
|
||||||
@ -207,7 +224,7 @@ mod tests {
|
|||||||
let path = "/a/page/".to_string();
|
let path = "/a/page/".to_string();
|
||||||
let content = "Some content".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.len(), 3);
|
||||||
assert_eq!(res[0], title.unwrap());
|
assert_eq!(res[0], title.unwrap());
|
||||||
assert_eq!(res[1], description.unwrap());
|
assert_eq!(res[1], description.unwrap());
|
||||||
@ -223,9 +240,26 @@ mod tests {
|
|||||||
let path = "/a/page/".to_string();
|
let path = "/a/page/".to_string();
|
||||||
let content = "Some content".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.len(), 2);
|
||||||
assert_eq!(res[0], title.unwrap());
|
assert_eq!(res[0], title.unwrap());
|
||||||
assert_eq!(res[1], content[..5]);
|
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);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -169,6 +169,8 @@ paths_keep_dates = false
|
|||||||
include_title = true
|
include_title = true
|
||||||
# Whether to include the description of the page/section in the index
|
# Whether to include the description of the page/section in the index
|
||||||
include_description = false
|
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
|
# Whether to include the path of the page/section in the index
|
||||||
include_path = false
|
include_path = false
|
||||||
# Whether to include the rendered content of the page/section in the index
|
# Whether to include the rendered content of the page/section in the index
|
||||||
|
Loading…
Reference in New Issue
Block a user