Search: Update elasticlunr-rs. Remove old code. Don't generate index if search is disabled
This commit is contained in:
parent
26e16a83eb
commit
66ae6b6e92
|
@ -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 }
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
[book]
|
||||
title = "mdBook Documentation"
|
||||
description = "Create book from markdown files. Like Gitbook but implemented in Rust"
|
||||
author = "Mathieu David"
|
||||
|
|
|
@ -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<utils::SearchDocument>,
|
||||
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<SearchOptions>,
|
||||
/// The index for elasticlunr.js
|
||||
#[serde(skip_serializing_if = "Option::is_none")]
|
||||
index: Option<elasticlunr::index::Index>,
|
||||
index: Option<elasticlunr::Index>,
|
||||
|
||||
}
|
||||
|
||||
|
@ -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 {
|
||||
|
|
|
@ -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(); } );
|
||||
|
|
|
@ -74,6 +74,10 @@ pub fn render_markdown_into_searchindex<F>(
|
|||
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);
|
||||
|
|
Loading…
Reference in New Issue