Skip to content

Commit

Permalink
Fix the issue of generating the search index for multiple language (g…
Browse files Browse the repository at this point in the history
…etzola#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
  • Loading branch information
nottxy authored and Keats committed Sep 3, 2019
1 parent 5aadd3d commit 9db9fc8
Show file tree
Hide file tree
Showing 11 changed files with 40 additions and 15 deletions.
4 changes: 3 additions & 1 deletion components/config/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
}
}

Expand Down
8 changes: 4 additions & 4 deletions components/library/src/content/file_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand All @@ -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(),
Expand All @@ -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(),
Expand All @@ -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(),
Expand Down
6 changes: 3 additions & 3 deletions components/library/src/content/page.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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#"
+++
+++
Expand All @@ -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#"
+++
+++
Expand All @@ -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"
Expand Down
6 changes: 3 additions & 3 deletions components/library/src/content/section.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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#"
+++
+++
Expand All @@ -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#"
+++
+++
Expand All @@ -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#"
+++
+++
Expand Down
2 changes: 1 addition & 1 deletion components/library/src/taxonomies/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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![
Expand Down
4 changes: 3 additions & 1 deletion components/search/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down
12 changes: 12 additions & 0 deletions components/site/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)?;

Expand Down
5 changes: 5 additions & 0 deletions components/site/tests/site_i18n.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
}
1 change: 1 addition & 0 deletions docs/content/documentation/content/multilingual.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
]
```
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
# ]
#
Expand Down
6 changes: 4 additions & 2 deletions test_site_i18n/config.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -22,7 +24,7 @@ taxonomies = [

languages = [
{code = "fr", rss = true},
{code = "it", rss = false},
{code = "it", rss = false, search = true },
]

[extra]
Expand Down

0 comments on commit 9db9fc8

Please sign in to comment.