diff --git a/components/config/src/config.rs b/components/config/src/config.rs index 18dd2ec9..4f3dab51 100644 --- a/components/config/src/config.rs +++ b/components/config/src/config.rs @@ -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 } } } diff --git a/components/library/src/content/file_info.rs b/components/library/src/content/file_info.rs index 32ed4845..0fe06863 100644 --- a/components/library/src/content/file_info.rs +++ b/components/library/src/content/file_info.rs @@ -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(), diff --git a/components/library/src/content/page.rs b/components/library/src/content/page.rs index 45733571..351a33a7 100644 --- a/components/library/src/content/page.rs +++ b/components/library/src/content/page.rs @@ -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" diff --git a/components/library/src/content/section.rs b/components/library/src/content/section.rs index 21b700ab..49fb8792 100644 --- a/components/library/src/content/section.rs +++ b/components/library/src/content/section.rs @@ -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#" +++ +++ diff --git a/components/library/src/taxonomies/mod.rs b/components/library/src/taxonomies/mod.rs index 4f5d8fa7..f3551240 100644 --- a/components/library/src/taxonomies/mod.rs +++ b/components/library/src/taxonomies/mod.rs @@ -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![ diff --git a/components/search/src/lib.rs b/components/search/src/lib.rs index 846e42e3..86b8655d 100644 --- a/components/search/src/lib.rs +++ b/components/search/src/lib.rs @@ -48,7 +48,9 @@ pub fn build_index(lang: &str, library: &Library) -> Result { 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()) diff --git a/components/site/src/lib.rs b/components/site/src/lib.rs index d1f1d06b..8b1e78ff 100644 --- a/components/site/src/lib.rs +++ b/components/site/src/lib.rs @@ -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)?; diff --git a/components/site/tests/site_i18n.rs b/components/site/tests/site_i18n.rs index 8911a661..a1391f9f 100644 --- a/components/site/tests/site_i18n.rs +++ b/components/site/tests/site_i18n.rs @@ -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")); } diff --git a/docs/content/documentation/content/multilingual.md b/docs/content/documentation/content/multilingual.md index d7889722..a6eaba18 100644 --- a/docs/content/documentation/content/multilingual.md +++ b/docs/content/documentation/content/multilingual.md @@ -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 ] ``` diff --git a/docs/content/documentation/getting-started/configuration.md b/docs/content/documentation/getting-started/configuration.md index de95efb1..cba14728 100644 --- a/docs/content/documentation/getting-started/configuration.md +++ b/docs/content/documentation/getting-started/configuration.md @@ -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 # ] # diff --git a/test_site_i18n/config.toml b/test_site_i18n/config.toml index 47ed8f59..a12f5e9a 100644 --- a/test_site_i18n/config.toml +++ b/test_site_i18n/config.toml @@ -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]