mirror of
https://github.com/getzola/zola
synced 2024-12-05 01:49:12 +00:00
Fix the issue of generating the search index for multiple language (#794)
* fix the issue of generating the search index for multiple language * updat docs for generating the search index for multiple language * fix failed tests * add tests for the search index of multiple language
This commit is contained in:
parent
5aadd3d4f2
commit
9db9fc8fb2
11 changed files with 40 additions and 15 deletions
|
@ -30,11 +30,13 @@ pub struct Language {
|
|||
pub code: String,
|
||||
/// Whether to generate a RSS feed for that language, defaults to `false`
|
||||
pub rss: bool,
|
||||
/// Whether to generate search index for that language, defaults to `false`
|
||||
pub search: bool,
|
||||
}
|
||||
|
||||
impl Default for Language {
|
||||
fn default() -> Language {
|
||||
Language { code: String::new(), rss: false }
|
||||
Language { code: String::new(), rss: false, search: false }
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -194,7 +194,7 @@ mod tests {
|
|||
#[test]
|
||||
fn can_find_valid_language_in_page() {
|
||||
let mut config = Config::default();
|
||||
config.languages.push(Language { code: String::from("fr"), rss: false });
|
||||
config.languages.push(Language { code: String::from("fr"), rss: false, search: false });
|
||||
let mut file = FileInfo::new_page(
|
||||
&Path::new("/home/vincent/code/site/content/posts/tutorials/python.fr.md"),
|
||||
&PathBuf::new(),
|
||||
|
@ -207,7 +207,7 @@ mod tests {
|
|||
#[test]
|
||||
fn can_find_valid_language_in_page_with_assets() {
|
||||
let mut config = Config::default();
|
||||
config.languages.push(Language { code: String::from("fr"), rss: false });
|
||||
config.languages.push(Language { code: String::from("fr"), rss: false, search: false });
|
||||
let mut file = FileInfo::new_page(
|
||||
&Path::new("/home/vincent/code/site/content/posts/tutorials/python/index.fr.md"),
|
||||
&PathBuf::new(),
|
||||
|
@ -233,7 +233,7 @@ mod tests {
|
|||
#[test]
|
||||
fn errors_on_unknown_language_in_page_with_i18n_on() {
|
||||
let mut config = Config::default();
|
||||
config.languages.push(Language { code: String::from("it"), rss: false });
|
||||
config.languages.push(Language { code: String::from("it"), rss: false, search: false });
|
||||
let mut file = FileInfo::new_page(
|
||||
&Path::new("/home/vincent/code/site/content/posts/tutorials/python.fr.md"),
|
||||
&PathBuf::new(),
|
||||
|
@ -245,7 +245,7 @@ mod tests {
|
|||
#[test]
|
||||
fn can_find_valid_language_in_section() {
|
||||
let mut config = Config::default();
|
||||
config.languages.push(Language { code: String::from("fr"), rss: false });
|
||||
config.languages.push(Language { code: String::from("fr"), rss: false, search: false });
|
||||
let mut file = FileInfo::new_section(
|
||||
&Path::new("/home/vincent/code/site/content/posts/tutorials/_index.fr.md"),
|
||||
&PathBuf::new(),
|
||||
|
|
|
@ -736,7 +736,7 @@ Hello world
|
|||
#[test]
|
||||
fn can_specify_language_in_filename() {
|
||||
let mut config = Config::default();
|
||||
config.languages.push(Language { code: String::from("fr"), rss: false });
|
||||
config.languages.push(Language { code: String::from("fr"), rss: false, search: false });
|
||||
let content = r#"
|
||||
+++
|
||||
+++
|
||||
|
@ -753,7 +753,7 @@ Bonjour le monde"#
|
|||
#[test]
|
||||
fn can_specify_language_in_filename_with_date() {
|
||||
let mut config = Config::default();
|
||||
config.languages.push(Language { code: String::from("fr"), rss: false });
|
||||
config.languages.push(Language { code: String::from("fr"), rss: false, search: false });
|
||||
let content = r#"
|
||||
+++
|
||||
+++
|
||||
|
@ -772,7 +772,7 @@ Bonjour le monde"#
|
|||
#[test]
|
||||
fn i18n_frontmatter_path_overrides_default_permalink() {
|
||||
let mut config = Config::default();
|
||||
config.languages.push(Language { code: String::from("fr"), rss: false });
|
||||
config.languages.push(Language { code: String::from("fr"), rss: false, search: false });
|
||||
let content = r#"
|
||||
+++
|
||||
path = "bonjour"
|
||||
|
|
|
@ -350,7 +350,7 @@ mod tests {
|
|||
#[test]
|
||||
fn can_specify_language_in_filename() {
|
||||
let mut config = Config::default();
|
||||
config.languages.push(Language { code: String::from("fr"), rss: false });
|
||||
config.languages.push(Language { code: String::from("fr"), rss: false, search: false });
|
||||
let content = r#"
|
||||
+++
|
||||
+++
|
||||
|
@ -372,7 +372,7 @@ Bonjour le monde"#
|
|||
#[test]
|
||||
fn can_make_links_to_translated_sections_without_double_trailing_slash() {
|
||||
let mut config = Config::default();
|
||||
config.languages.push(Language { code: String::from("fr"), rss: false });
|
||||
config.languages.push(Language { code: String::from("fr"), rss: false, search: false });
|
||||
let content = r#"
|
||||
+++
|
||||
+++
|
||||
|
@ -389,7 +389,7 @@ Bonjour le monde"#
|
|||
#[test]
|
||||
fn can_make_links_to_translated_subsections_with_trailing_slash() {
|
||||
let mut config = Config::default();
|
||||
config.languages.push(Language { code: String::from("fr"), rss: false });
|
||||
config.languages.push(Language { code: String::from("fr"), rss: false, search: false });
|
||||
let content = r#"
|
||||
+++
|
||||
+++
|
||||
|
|
|
@ -361,7 +361,7 @@ mod tests {
|
|||
#[test]
|
||||
fn can_make_taxonomies_in_multiple_languages() {
|
||||
let mut config = Config::default();
|
||||
config.languages.push(Language { rss: false, code: "fr".to_string() });
|
||||
config.languages.push(Language { rss: false, code: "fr".to_string(), search: false });
|
||||
let mut library = Library::new(2, 0, true);
|
||||
|
||||
config.taxonomies = vec![
|
||||
|
|
|
@ -48,7 +48,9 @@ pub fn build_index(lang: &str, library: &Library) -> Result<String> {
|
|||
let mut index = Index::with_language(language, &["title", "body"]);
|
||||
|
||||
for section in library.sections_values() {
|
||||
add_section_to_index(&mut index, section, library);
|
||||
if section.lang == lang {
|
||||
add_section_to_index(&mut index, section, library);
|
||||
}
|
||||
}
|
||||
|
||||
Ok(index.to_json())
|
||||
|
|
|
@ -791,6 +791,18 @@ impl Site {
|
|||
),
|
||||
)?;
|
||||
|
||||
for language in &self.config.languages {
|
||||
if language.code != self.config.default_language && language.search {
|
||||
create_file(
|
||||
&self.output_path.join(&format!("search_index.{}.js", &language.code)),
|
||||
&format!(
|
||||
"window.searchIndex = {};",
|
||||
search::build_index(&language.code, &self.library.read().unwrap())?
|
||||
),
|
||||
)?;
|
||||
}
|
||||
}
|
||||
|
||||
// then elasticlunr.min.js
|
||||
create_file(&self.output_path.join("elasticlunr.min.js"), search::ELASTICLUNR_JS)?;
|
||||
|
||||
|
|
|
@ -148,4 +148,9 @@ fn can_build_multilingual_site() {
|
|||
assert!(file_exists!(public, "fr/tags/index.html"));
|
||||
assert!(file_contains!(public, "fr/tags/index.html", "bonjour"));
|
||||
assert!(!file_contains!(public, "fr/tags/index.html", "hello"));
|
||||
|
||||
// one lang index per language
|
||||
assert!(file_exists!(public, "search_index.en.js"));
|
||||
assert!(file_exists!(public, "search_index.it.js"));
|
||||
assert!(!file_exists!(public, "search_index.fr.js"));
|
||||
}
|
||||
|
|
|
@ -12,6 +12,7 @@ to your `config.toml`. For example:
|
|||
```toml
|
||||
languages = [
|
||||
{code = "fr", rss = true}, # there will be a RSS feed for French content
|
||||
{code = "fr", search = true}, # there will be a Search Index for French content
|
||||
{code = "it"}, # there won't be a RSS feed for Italian content
|
||||
]
|
||||
```
|
||||
|
|
|
@ -62,6 +62,7 @@ taxonomies = []
|
|||
# Example:
|
||||
# languages = [
|
||||
# {code = "fr", rss = true}, # there will be a RSS feed for French content
|
||||
# {code = "fr", search = true}, # there will be a Search Index for French content
|
||||
# {code = "it"}, # there won't be a RSS feed for Italian content
|
||||
# ]
|
||||
#
|
||||
|
|
|
@ -9,7 +9,9 @@ compile_sass = false
|
|||
highlight_code = false
|
||||
|
||||
# Whether to build a search index to be used later on by a JavaScript library
|
||||
build_search_index = false
|
||||
build_search_index = true
|
||||
|
||||
default_language = "en"
|
||||
|
||||
generate_rss = true
|
||||
|
||||
|
@ -22,7 +24,7 @@ taxonomies = [
|
|||
|
||||
languages = [
|
||||
{code = "fr", rss = true},
|
||||
{code = "it", rss = false},
|
||||
{code = "it", rss = false, search = true },
|
||||
]
|
||||
|
||||
[extra]
|
||||
|
|
Loading…
Reference in a new issue