From 66ae6b6e928935ba623fefd4549dec21ddd7a8b4 Mon Sep 17 00:00:00 2001 From: Phaiax Date: Tue, 21 Nov 2017 20:28:19 +0100 Subject: [PATCH] Search: Update elasticlunr-rs. Remove old code. Don't generate index if search is disabled --- Cargo.toml | 2 +- book-example/book.toml | 1 + src/renderer/html_handlebars/hbs_renderer.rs | 21 +++--- src/theme/book.js | 78 +------------------- src/utils/mod.rs | 4 + 5 files changed, 17 insertions(+), 89 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 7d7f048d..71b54311 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -29,7 +29,7 @@ toml = "0.4" open = "1.1" regex = "0.2.1" tempdir = "0.3.4" -elasticlunr = { git = "https://github.com/mattico/elasticlunr-rs" } +elasticlunr-rs = "0.2.1" # Watch feature notify = { version = "4.0", optional = true } diff --git a/book-example/book.toml b/book-example/book.toml index cfcb11bb..7a9a60c3 100644 --- a/book-example/book.toml +++ b/book-example/book.toml @@ -1,3 +1,4 @@ +[book] title = "mdBook Documentation" description = "Create book from markdown files. Like Gitbook but implemented in Rust" author = "Mathieu David" diff --git a/src/renderer/html_handlebars/hbs_renderer.rs b/src/renderer/html_handlebars/hbs_renderer.rs index 9b6f01fc..82d2d590 100644 --- a/src/renderer/html_handlebars/hbs_renderer.rs +++ b/src/renderer/html_handlebars/hbs_renderer.rs @@ -312,7 +312,7 @@ impl Renderer for HtmlHandlebars { is_index = false; } - // Search index + // Search index (call this even if searching is disabled) make_searchindex(book, search_documents, &html_config.search)?; // Print version @@ -656,6 +656,8 @@ fn make_searchindex(book: &MDBook, search_documents : Vec, searchconfig : &Search) -> Result<()> { + // These structs mirror the configuration javascript object accepted by + // http://elasticlunr.com/docs/configuration.js.html #[derive(Serialize)] struct SearchOptionsField { @@ -669,7 +671,6 @@ fn make_searchindex(book: &MDBook, breadcrumbs: SearchOptionsField, } - /// The searchoptions for elasticlunr.js #[derive(Serialize)] struct SearchOptions { bool: String, @@ -681,11 +682,14 @@ fn make_searchindex(book: &MDBook, #[derive(Serialize)] struct SearchindexJson { + /// Propagate the search enabled/disabled setting to the html page enable: bool, #[serde(skip_serializing_if = "Option::is_none")] + /// The searchoptions for elasticlunr.js searchoptions: Option, + /// The index for elasticlunr.js #[serde(skip_serializing_if = "Option::is_none")] - index: Option, + index: Option, } @@ -703,22 +707,17 @@ fn make_searchindex(book: &MDBook, let json_contents = if searchconfig.enable { - let mut index = elasticlunr::index::Index::new("id", - &["title".into(), "body".into(), "breadcrumbs".into()]); + let mut index = elasticlunr::Index::new(&["title", "body", "breadcrumbs"]); for sd in search_documents { + // Concat the html link with the anchor ("abc.html#anchor") let anchor = if let Some(s) = sd.anchor.1 { format!("{}#{}", sd.anchor.0, &s) } else { sd.anchor.0 }; - let mut map = HashMap::new(); - map.insert("id".into(), anchor.clone()); - map.insert("title".into(), sd.title); - map.insert("body".into(), sd.body); - map.insert("breadcrumbs".into(), sd.hierarchy.join(" » ")); - index.add_doc(&anchor, map); + index.add_doc(&anchor, &[sd.title, sd.body, sd.hierarchy.join(" » ")]); } SearchindexJson { diff --git a/src/theme/book.js b/src/theme/book.js index 5151bace..23bf2c8a 100644 --- a/src/theme/book.js +++ b/src/theme/book.js @@ -46,63 +46,6 @@ $( document ).ready(function() { } } , - create_test_searchindex : function () { - var searchindex = elasticlunr(function () { - this.addField('body'); - this.addField('title'); - this.addField('breadcrumbs') - this.setRef('id'); - }); - var base_breadcrumbs = ""; - var active_chapter = $('.sidebar ul a.active'); - base_breadcrumbs = active_chapter.text().split('. ', 2)[1]; // demo - while (true) { - var parent_ul = active_chapter.parents('ul'); - if (parent_ul.length == 0) break; - var parent_li = parent_ul.parents('li'); - if (parent_li.length == 0) break; - var pre_li = parent_li.prev('li'); - if (pre_li.length == 0) break; - base_breadcrumbs = pre_li.text().split('. ', 2)[1] + ' » ' + base_breadcrumbs; - active_chapter = pre_li; - } - var paragraphs = this.content.children(); - var curr_title = ""; - var curr_body = ""; - var curr_ref = ""; - var push = function(ref) { - if ((curr_title.length > 0 || curr_body.length > 0) && curr_ref.length > 0) { - var doc = { - "id": curr_ref, - "body": curr_body, - "title": curr_title, - "breadcrumbs": base_breadcrumbs //"Header1 » Header2" - } - searchindex.addDoc(doc); - } - curr_body = ""; - curr_title = ""; - curr_ref = ""; - }; - paragraphs.each(function(index, element) { - // todo uppercase - var el = $(element); - if (el.prop('nodeName').toUpperCase() == "A") { - // new header, push old paragraph to index - push(index); - curr_title = el.text(); - curr_ref = el.attr('href'); - } else { - curr_body += " \n " + el.text(); - } - // last paragraph - if (index == paragraphs.length - 1) { - push(index); - } - }); - this.searchindex = searchindex; - } - , parseURL : function (url) { var a = document.createElement('a'); a.href = url; @@ -325,9 +268,6 @@ $( document ).ready(function() { init : function () { var this_ = this; - // For testing purposes: Index current page - //this.create_test_searchindex(); - $.getJSON("searchindex.json", function(json) { if (json.enable == false) { @@ -336,23 +276,7 @@ $( document ).ready(function() { } this_.searchoptions = json.searchoptions; - //this_.searchindex = elasticlunr.Index.load(json.index); - - // TODO: Workaround: reindex everything - var searchindex = elasticlunr(function () { - this.addField('body'); - this.addField('title'); - this.addField('breadcrumbs') - this.setRef('id'); - }); - window.mjs = json; - window.search = this_; - var docs = json.index.documentStore.docs; - for (var key in docs) { - searchindex.addDoc(docs[key]); - } - this_.searchindex = searchindex; - + this_.searchindex = elasticlunr.Index.load(json.index); // Set up events this_.searchicon.click( function(e) { this_.searchIconClickHandler(); } ); diff --git a/src/utils/mod.rs b/src/utils/mod.rs index 2eeebfe8..8cf67979 100644 --- a/src/utils/mod.rs +++ b/src/utils/mod.rs @@ -74,6 +74,10 @@ pub fn render_markdown_into_searchindex( heading_to_anchor : F) where F : Fn(&str) -> String { + if ! searchconfig.enable { + return; + } + let mut opts = Options::empty(); opts.insert(OPTION_ENABLE_TABLES); opts.insert(OPTION_ENABLE_FOOTNOTES);