mirror of
https://github.com/getzola/zola
synced 2024-11-10 06:14:19 +00:00
Add option to include date in search index (#2401)
This commit is contained in:
parent
8bdec67f0a
commit
a9ab3592c8
3 changed files with 42 additions and 3 deletions
|
@ -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(),
|
||||
}
|
||||
|
|
|
@ -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};
|
||||
|
@ -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<String>,
|
||||
description: &Option<String>,
|
||||
datetime: &Option<OffsetDateTime>,
|
||||
path: &str,
|
||||
content: &str,
|
||||
) -> Vec<String> {
|
||||
|
@ -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);
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue