Skip to content

Commit b2ad669

Browse files
matticoMichael-F-Bryan
authored andcommitted
Search with Elasticlunr, updated (#604)
* Add search with elasticlunr.js This commit adds search functionality to mdBook, based on work done by @Phaiax. The in-browser search code uses elasticlunr.js to execute the search, using an index generated at book build time by elasticlunr-rs. * Add generator comment Someone on Reddit was wondering how the rust book was generated and said they checked the source. Thought I'd put this here. Might be a good idea to have a little footer "made with mdBook", but this'll do for now. * Remove search/editor file override behavior * Use for loop for book iterator * Improve HTML regex * Fix search CORS in file URIs * Use ammonia to sanitize HTML * Filter html5ever log messages
1 parent bb043ef commit b2ad669

39 files changed

+3862
-257
lines changed

.gitattributes

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,7 @@
22

33
* text=auto eol=lf
44
*.rs rust
5+
*.woff -text
6+
*.ttf -text
7+
*.otf -text
8+
*.png -text

Cargo.lock

Lines changed: 92 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,10 @@ iron = { version = "0.5", optional = true }
5050
staticfile = { version = "0.4", optional = true }
5151
ws = { version = "0.7", optional = true}
5252

53+
# Search feature
54+
elasticlunr-rs = { version = "0.2", optional = true }
55+
ammonia = { version = "1.1", optional = true }
56+
5357
[build-dependencies]
5458
error-chain = "0.11"
5559

@@ -60,12 +64,13 @@ walkdir = "2.0"
6064
pulldown-cmark-to-cmark = "1.1.0"
6165

6266
[features]
63-
default = ["output", "watch", "serve"]
67+
default = ["output", "watch", "serve", "search"]
6468
debug = []
6569
output = []
6670
regenerate-css = []
6771
watch = ["notify", "time", "crossbeam"]
6872
serve = ["iron", "staticfile", "ws"]
73+
search = ["elasticlunr-rs", "ammonia"]
6974

7075
[[bin]]
7176
doc = false

book-example/book.toml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,12 @@ mathjax-support = true
88

99
[output.html.playpen]
1010
editable = true
11+
12+
[output.html.search]
13+
limit-results = 20
14+
use-boolean-and = true
15+
boost-title = 2
16+
boost-hierarchy = 2
17+
boost-paragraph = 1
18+
expand = true
19+
heading-split-level = 2

book-example/src/format/config.md

Lines changed: 55 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ create-missing = false
1616

1717
[output.html]
1818
additional-css = ["custom.css"]
19+
20+
[output.html.search]
21+
limit-results = 15
1922
```
2023

2124
## Supported configuration options
@@ -81,14 +84,48 @@ The following configuration options are available:
8184
stylesheets that will be loaded after the default ones where you can
8285
surgically change the style.
8386
- **additional-js:** If you need to add some behaviour to your book without
84-
removing the current behaviour, you can specify a set of javascript files
87+
removing the current behaviour, you can specify a set of JavaScript files
8588
that will be loaded alongside the default one.
86-
- **playpen:** A subtable for configuring various playpen settings.
87-
- **no-section-label**: mdBook by defaults adds section label in table of
89+
- **no-section-label:** mdBook by defaults adds section label in table of
8890
contents column. For example, "1.", "2.1". Set this option to true to
8991
disable those labels. Defaults to `false`.
90-
91-
**book.toml**
92+
- **playpen:** A subtable for configuring various playpen settings.
93+
- **search:** A subtable for configuring the in-browser search
94+
functionality. mdBook must be compiled with the `search` feature enabled
95+
(on by default).
96+
97+
Available configuration options for the `[output.html.playpen]` table:
98+
99+
- **editable:** Allow editing the source code. Defaults to `false`.
100+
- **copy-js:** Copy JavaScript files for the editor to the output directory.
101+
Defaults to `true`.
102+
103+
[Ace]: https://ace.c9.io/
104+
105+
Available configuration options for the `[output.html.search]` table:
106+
107+
- **limit-results:** The maximum number of search results. Defaults to `30`.
108+
- **teaser-word-count:** The number of words used for a search result teaser.
109+
Defaults to `30`.
110+
- **use-boolean-and:** Define the logical link between multiple search words.
111+
If true, all search words must appear in each result. Defaults to `true`.
112+
- **boost-title:** Boost factor for the search result score if a search word
113+
appears in the header. Defaults to `2`.
114+
- **boost-hierarchy:** Boost factor for the search result score if a search
115+
word appears in the hierarchy. The hierarchy contains all titles of the
116+
parent documents and all parent headings. Defaults to `1`.
117+
- **boost-paragraph:** Boost factor for the search result score if a search
118+
word appears in the text. Defaults to `1`.
119+
- **expand:** True if search should match longer results e.g. search `micro`
120+
should match `microwave`. Defaults to `true`.
121+
- **heading-split-level:** Search results will link to a section of the document
122+
which contains the result. Documents are split into sections by headings
123+
this level or less.
124+
Defaults to `3`. (`### This is a level 3 heading`)
125+
- **copy-js:** Copy JavaScript files for the search implementation to the
126+
output directory. Defaults to `true`.
127+
128+
This shows all available options in the **book.toml**:
92129
```toml
93130
[book]
94131
title = "Example book"
@@ -105,6 +142,18 @@ additional-js = ["custom.js"]
105142
[output.html.playpen]
106143
editor = "./path/to/editor"
107144
editable = false
145+
146+
[output.html.search]
147+
enable = true
148+
searcher = "./path/to/searcher"
149+
limit-results = 30
150+
teaser-word-count = 30
151+
use-boolean-and = true
152+
boost-title = 2
153+
boost-hierarchy = 1
154+
boost-paragraph = 1
155+
expand = true
156+
heading-split-level = 3
108157
```
109158

110159

@@ -145,4 +194,4 @@ override the book's title without needing to touch your `book.toml`.
145194
146195
The latter case may be useful in situations where `mdbook` is invoked
147196
from a script or CI, where it sometimes isn't possible to update the
148-
`book.toml` before building.
197+
`book.toml` before building.

book-example/src/misc/contributors.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,5 @@ If you have contributed to mdBook and I forgot to add you, don't hesitate to add
1414
- [Michael-F-Bryan](https://github.com/Michael-F-Bryan)
1515
- [Chris Spiegel](https://github.com/cspiegel)
1616
- [projektir](https://github.com/projektir)
17+
- [Phaiax](https://github.com/Phaiax)
18+
- [Matt Ickstadt](https://github.com/mattico)

ci/script.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,15 @@
33
set -ex
44

55
main() {
6+
cross build --target $TARGET --all --no-default-features
67
cross build --target $TARGET --all
78
cross build --target $TARGET --all --release
89

910
if [ ! -z $DISABLE_TESTS ]; then
1011
return
1112
fi
1213

14+
cross test --target $TARGET --no-default-features
1315
cross test --target $TARGET
1416
cross test --target $TARGET --release
1517
}

src/bin/mdbook.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,8 @@ fn init_logger() {
9292
} else {
9393
// if no RUST_LOG provided, default to logging at the Info level
9494
builder.filter(None, LevelFilter::Info);
95+
// Filter extraneous html5ever not-implemented messages
96+
builder.filter(Some("html5ever"), LevelFilter::Error);
9597
}
9698

9799
builder.init();

0 commit comments

Comments
 (0)