Skip to content

Commit 66ae6b6

Browse files
committed
Search: Update elasticlunr-rs. Remove old code. Don't generate index if search is disabled
1 parent 26e16a8 commit 66ae6b6

File tree

5 files changed

+17
-89
lines changed

5 files changed

+17
-89
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ toml = "0.4"
2929
open = "1.1"
3030
regex = "0.2.1"
3131
tempdir = "0.3.4"
32-
elasticlunr = { git = "https://github.com/mattico/elasticlunr-rs" }
32+
elasticlunr-rs = "0.2.1"
3333

3434
# Watch feature
3535
notify = { version = "4.0", optional = true }

book-example/book.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
[book]
12
title = "mdBook Documentation"
23
description = "Create book from markdown files. Like Gitbook but implemented in Rust"
34
author = "Mathieu David"

src/renderer/html_handlebars/hbs_renderer.rs

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ impl Renderer for HtmlHandlebars {
312312
is_index = false;
313313
}
314314

315-
// Search index
315+
// Search index (call this even if searching is disabled)
316316
make_searchindex(book, search_documents, &html_config.search)?;
317317

318318
// Print version
@@ -656,6 +656,8 @@ fn make_searchindex(book: &MDBook,
656656
search_documents : Vec<utils::SearchDocument>,
657657
searchconfig : &Search) -> Result<()> {
658658

659+
// These structs mirror the configuration javascript object accepted by
660+
// http://elasticlunr.com/docs/configuration.js.html
659661

660662
#[derive(Serialize)]
661663
struct SearchOptionsField {
@@ -669,7 +671,6 @@ fn make_searchindex(book: &MDBook,
669671
breadcrumbs: SearchOptionsField,
670672
}
671673

672-
/// The searchoptions for elasticlunr.js
673674
#[derive(Serialize)]
674675
struct SearchOptions {
675676
bool: String,
@@ -681,11 +682,14 @@ fn make_searchindex(book: &MDBook,
681682

682683
#[derive(Serialize)]
683684
struct SearchindexJson {
685+
/// Propagate the search enabled/disabled setting to the html page
684686
enable: bool,
685687
#[serde(skip_serializing_if = "Option::is_none")]
688+
/// The searchoptions for elasticlunr.js
686689
searchoptions: Option<SearchOptions>,
690+
/// The index for elasticlunr.js
687691
#[serde(skip_serializing_if = "Option::is_none")]
688-
index: Option<elasticlunr::index::Index>,
692+
index: Option<elasticlunr::Index>,
689693

690694
}
691695

@@ -703,22 +707,17 @@ fn make_searchindex(book: &MDBook,
703707

704708
let json_contents = if searchconfig.enable {
705709

706-
let mut index = elasticlunr::index::Index::new("id",
707-
&["title".into(), "body".into(), "breadcrumbs".into()]);
710+
let mut index = elasticlunr::Index::new(&["title", "body", "breadcrumbs"]);
708711

709712
for sd in search_documents {
713+
// Concat the html link with the anchor ("abc.html#anchor")
710714
let anchor = if let Some(s) = sd.anchor.1 {
711715
format!("{}#{}", sd.anchor.0, &s)
712716
} else {
713717
sd.anchor.0
714718
};
715719

716-
let mut map = HashMap::new();
717-
map.insert("id".into(), anchor.clone());
718-
map.insert("title".into(), sd.title);
719-
map.insert("body".into(), sd.body);
720-
map.insert("breadcrumbs".into(), sd.hierarchy.join(" » "));
721-
index.add_doc(&anchor, map);
720+
index.add_doc(&anchor, &[sd.title, sd.body, sd.hierarchy.join(" » ")]);
722721
}
723722

724723
SearchindexJson {

src/theme/book.js

Lines changed: 1 addition & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -46,63 +46,6 @@ $( document ).ready(function() {
4646
}
4747
}
4848
,
49-
create_test_searchindex : function () {
50-
var searchindex = elasticlunr(function () {
51-
this.addField('body');
52-
this.addField('title');
53-
this.addField('breadcrumbs')
54-
this.setRef('id');
55-
});
56-
var base_breadcrumbs = "";
57-
var active_chapter = $('.sidebar ul a.active');
58-
base_breadcrumbs = active_chapter.text().split('. ', 2)[1]; // demo
59-
while (true) {
60-
var parent_ul = active_chapter.parents('ul');
61-
if (parent_ul.length == 0) break;
62-
var parent_li = parent_ul.parents('li');
63-
if (parent_li.length == 0) break;
64-
var pre_li = parent_li.prev('li');
65-
if (pre_li.length == 0) break;
66-
base_breadcrumbs = pre_li.text().split('. ', 2)[1] + ' » ' + base_breadcrumbs;
67-
active_chapter = pre_li;
68-
}
69-
var paragraphs = this.content.children();
70-
var curr_title = "";
71-
var curr_body = "";
72-
var curr_ref = "";
73-
var push = function(ref) {
74-
if ((curr_title.length > 0 || curr_body.length > 0) && curr_ref.length > 0) {
75-
var doc = {
76-
"id": curr_ref,
77-
"body": curr_body,
78-
"title": curr_title,
79-
"breadcrumbs": base_breadcrumbs //"Header1 » Header2"
80-
}
81-
searchindex.addDoc(doc);
82-
}
83-
curr_body = "";
84-
curr_title = "";
85-
curr_ref = "";
86-
};
87-
paragraphs.each(function(index, element) {
88-
// todo uppercase
89-
var el = $(element);
90-
if (el.prop('nodeName').toUpperCase() == "A") {
91-
// new header, push old paragraph to index
92-
push(index);
93-
curr_title = el.text();
94-
curr_ref = el.attr('href');
95-
} else {
96-
curr_body += " \n " + el.text();
97-
}
98-
// last paragraph
99-
if (index == paragraphs.length - 1) {
100-
push(index);
101-
}
102-
});
103-
this.searchindex = searchindex;
104-
}
105-
,
10649
parseURL : function (url) {
10750
var a = document.createElement('a');
10851
a.href = url;
@@ -325,9 +268,6 @@ $( document ).ready(function() {
325268
init : function () {
326269
var this_ = this;
327270

328-
// For testing purposes: Index current page
329-
//this.create_test_searchindex();
330-
331271
$.getJSON("searchindex.json", function(json) {
332272

333273
if (json.enable == false) {
@@ -336,23 +276,7 @@ $( document ).ready(function() {
336276
}
337277

338278
this_.searchoptions = json.searchoptions;
339-
//this_.searchindex = elasticlunr.Index.load(json.index);
340-
341-
// TODO: Workaround: reindex everything
342-
var searchindex = elasticlunr(function () {
343-
this.addField('body');
344-
this.addField('title');
345-
this.addField('breadcrumbs')
346-
this.setRef('id');
347-
});
348-
window.mjs = json;
349-
window.search = this_;
350-
var docs = json.index.documentStore.docs;
351-
for (var key in docs) {
352-
searchindex.addDoc(docs[key]);
353-
}
354-
this_.searchindex = searchindex;
355-
279+
this_.searchindex = elasticlunr.Index.load(json.index);
356280

357281
// Set up events
358282
this_.searchicon.click( function(e) { this_.searchIconClickHandler(); } );

src/utils/mod.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,10 @@ pub fn render_markdown_into_searchindex<F>(
7474
heading_to_anchor : F)
7575
where F : Fn(&str) -> String {
7676

77+
if ! searchconfig.enable {
78+
return;
79+
}
80+
7781
let mut opts = Options::empty();
7882
opts.insert(OPTION_ENABLE_TABLES);
7983
opts.insert(OPTION_ENABLE_FOOTNOTES);

0 commit comments

Comments
 (0)