Browse Source

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
index-subcmd
Bob Vincent Prouillet 5 years ago
parent
commit
9db9fc8fb2
11 changed files with 40 additions and 15 deletions
  1. +3
    -1
      components/config/src/config.rs
  2. +4
    -4
      components/library/src/content/file_info.rs
  3. +3
    -3
      components/library/src/content/page.rs
  4. +3
    -3
      components/library/src/content/section.rs
  5. +1
    -1
      components/library/src/taxonomies/mod.rs
  6. +3
    -1
      components/search/src/lib.rs
  7. +12
    -0
      components/site/src/lib.rs
  8. +5
    -0
      components/site/tests/site_i18n.rs
  9. +1
    -0
      docs/content/documentation/content/multilingual.md
  10. +1
    -0
      docs/content/documentation/getting-started/configuration.md
  11. +4
    -2
      test_site_i18n/config.toml

+ 3
- 1
components/config/src/config.rs View File

@@ -30,11 +30,13 @@ pub struct Language {
pub code: String, pub code: String,
/// Whether to generate a RSS feed for that language, defaults to `false` /// Whether to generate a RSS feed for that language, defaults to `false`
pub rss: bool, pub rss: bool,
/// Whether to generate search index for that language, defaults to `false`
pub search: bool,
} }


impl Default for Language { impl Default for Language {
fn default() -> Language { fn default() -> Language {
Language { code: String::new(), rss: false }
Language { code: String::new(), rss: false, search: false }
} }
} }




+ 4
- 4
components/library/src/content/file_info.rs View File

@@ -194,7 +194,7 @@ mod tests {
#[test] #[test]
fn can_find_valid_language_in_page() { fn can_find_valid_language_in_page() {
let mut config = Config::default(); 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( let mut file = FileInfo::new_page(
&Path::new("/home/vincent/code/site/content/posts/tutorials/python.fr.md"), &Path::new("/home/vincent/code/site/content/posts/tutorials/python.fr.md"),
&PathBuf::new(), &PathBuf::new(),
@@ -207,7 +207,7 @@ mod tests {
#[test] #[test]
fn can_find_valid_language_in_page_with_assets() { fn can_find_valid_language_in_page_with_assets() {
let mut config = Config::default(); 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( let mut file = FileInfo::new_page(
&Path::new("/home/vincent/code/site/content/posts/tutorials/python/index.fr.md"), &Path::new("/home/vincent/code/site/content/posts/tutorials/python/index.fr.md"),
&PathBuf::new(), &PathBuf::new(),
@@ -233,7 +233,7 @@ mod tests {
#[test] #[test]
fn errors_on_unknown_language_in_page_with_i18n_on() { fn errors_on_unknown_language_in_page_with_i18n_on() {
let mut config = Config::default(); 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( let mut file = FileInfo::new_page(
&Path::new("/home/vincent/code/site/content/posts/tutorials/python.fr.md"), &Path::new("/home/vincent/code/site/content/posts/tutorials/python.fr.md"),
&PathBuf::new(), &PathBuf::new(),
@@ -245,7 +245,7 @@ mod tests {
#[test] #[test]
fn can_find_valid_language_in_section() { fn can_find_valid_language_in_section() {
let mut config = Config::default(); 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( let mut file = FileInfo::new_section(
&Path::new("/home/vincent/code/site/content/posts/tutorials/_index.fr.md"), &Path::new("/home/vincent/code/site/content/posts/tutorials/_index.fr.md"),
&PathBuf::new(), &PathBuf::new(),


+ 3
- 3
components/library/src/content/page.rs View File

@@ -736,7 +736,7 @@ Hello world
#[test] #[test]
fn can_specify_language_in_filename() { fn can_specify_language_in_filename() {
let mut config = Config::default(); 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#" let content = r#"
+++ +++
+++ +++
@@ -753,7 +753,7 @@ Bonjour le monde"#
#[test] #[test]
fn can_specify_language_in_filename_with_date() { fn can_specify_language_in_filename_with_date() {
let mut config = Config::default(); 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#" let content = r#"
+++ +++
+++ +++
@@ -772,7 +772,7 @@ Bonjour le monde"#
#[test] #[test]
fn i18n_frontmatter_path_overrides_default_permalink() { fn i18n_frontmatter_path_overrides_default_permalink() {
let mut config = Config::default(); 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#" let content = r#"
+++ +++
path = "bonjour" path = "bonjour"


+ 3
- 3
components/library/src/content/section.rs View File

@@ -350,7 +350,7 @@ mod tests {
#[test] #[test]
fn can_specify_language_in_filename() { fn can_specify_language_in_filename() {
let mut config = Config::default(); 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#" let content = r#"
+++ +++
+++ +++
@@ -372,7 +372,7 @@ Bonjour le monde"#
#[test] #[test]
fn can_make_links_to_translated_sections_without_double_trailing_slash() { fn can_make_links_to_translated_sections_without_double_trailing_slash() {
let mut config = Config::default(); 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#" let content = r#"
+++ +++
+++ +++
@@ -389,7 +389,7 @@ Bonjour le monde"#
#[test] #[test]
fn can_make_links_to_translated_subsections_with_trailing_slash() { fn can_make_links_to_translated_subsections_with_trailing_slash() {
let mut config = Config::default(); 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#" let content = r#"
+++ +++
+++ +++


+ 1
- 1
components/library/src/taxonomies/mod.rs View File

@@ -361,7 +361,7 @@ mod tests {
#[test] #[test]
fn can_make_taxonomies_in_multiple_languages() { fn can_make_taxonomies_in_multiple_languages() {
let mut config = Config::default(); 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); let mut library = Library::new(2, 0, true);


config.taxonomies = vec![ config.taxonomies = vec![


+ 3
- 1
components/search/src/lib.rs View File

@@ -48,7 +48,9 @@ pub fn build_index(lang: &str, library: &Library) -> Result<String> {
let mut index = Index::with_language(language, &["title", "body"]); let mut index = Index::with_language(language, &["title", "body"]);


for section in library.sections_values() { 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()) Ok(index.to_json())


+ 12
- 0
components/site/src/lib.rs View File

@@ -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 // then elasticlunr.min.js
create_file(&self.output_path.join("elasticlunr.min.js"), search::ELASTICLUNR_JS)?; create_file(&self.output_path.join("elasticlunr.min.js"), search::ELASTICLUNR_JS)?;




+ 5
- 0
components/site/tests/site_i18n.rs View File

@@ -148,4 +148,9 @@ fn can_build_multilingual_site() {
assert!(file_exists!(public, "fr/tags/index.html")); 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", "bonjour"));
assert!(!file_contains!(public, "fr/tags/index.html", "hello")); 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
- 0
docs/content/documentation/content/multilingual.md View File

@@ -12,6 +12,7 @@ to your `config.toml`. For example:
```toml ```toml
languages = [ languages = [
{code = "fr", rss = true}, # there will be a RSS feed for French content {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 {code = "it"}, # there won't be a RSS feed for Italian content
] ]
``` ```


+ 1
- 0
docs/content/documentation/getting-started/configuration.md View File

@@ -62,6 +62,7 @@ taxonomies = []
# Example: # Example:
# languages = [ # languages = [
# {code = "fr", rss = true}, # there will be a RSS feed for French content # {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 # {code = "it"}, # there won't be a RSS feed for Italian content
# ] # ]
# #


+ 4
- 2
test_site_i18n/config.toml View File

@@ -9,7 +9,9 @@ compile_sass = false
highlight_code = false highlight_code = false


# Whether to build a search index to be used later on by a JavaScript library # 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 generate_rss = true


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


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


[extra] [extra]


Loading…
Cancel
Save