gitbook-plugin-lunr
Advanced tools
Comparing version 1.1.0 to 1.2.0
@@ -59,2 +59,2 @@ require([ | ||
}); | ||
}); | ||
}); |
48
index.js
var lunr = require('lunr'); | ||
var Entities = require('html-entities').AllHtmlEntities; | ||
// Create search index | ||
var searchIndex = lunr(function () { | ||
this.ref('url'); | ||
var Html = new Entities(); | ||
this.field('title', { boost: 10 }); | ||
this.field('body'); | ||
}); | ||
var searchIndex; | ||
// Called with the `this` context provided by Gitbook | ||
function getSearchIndex(context) { | ||
if (!searchIndex) { | ||
// Create search index | ||
var ignoreSpecialCharacters = context.config.get('pluginsConfig.lunr.ignoreSpecialCharacters') || context.config.get('lunr.ignoreSpecialCharacters'); | ||
searchIndex = lunr(function () { | ||
this.ref('url'); | ||
this.field('title', { boost: 10 }); | ||
this.field('keywords', { boost: 15 }); | ||
this.field('body'); | ||
if (!ignoreSpecialCharacters) { | ||
// Don't trim non words characters (to allow search such as "C++") | ||
this.pipeline.remove(lunr.trimmer); | ||
} | ||
}); | ||
} | ||
return searchIndex; | ||
} | ||
// Map of Lunr ref to document | ||
@@ -33,8 +50,11 @@ var documentsStore = {}; | ||
var text, maxIndexSize; | ||
maxIndexSize = this.config.get('pluginsConfig.search.maxIndexSize') || this.config.get('search.maxIndexSize'); | ||
maxIndexSize = this.config.get('pluginsConfig.lunr.maxIndexSize') || this.config.get('lunr.maxIndexSize'); | ||
this.log.debug.ln('index page', page.path); | ||
// Transform as TEXT | ||
text = page.content.replace(/(<([^>]+)>)/ig, ''); | ||
text = page.content; | ||
// Decode HTML | ||
text = Html.decode(text); | ||
// Strip HTML tags | ||
text = text.replace(/(<([^>]+)>)/ig, ''); | ||
@@ -48,2 +68,7 @@ indexSize = indexSize + text.length; | ||
var keywords = []; | ||
if (page.search) { | ||
keywords = page.search.keywords || []; | ||
} | ||
// Add to index | ||
@@ -54,2 +79,3 @@ var doc = { | ||
summary: page.description, | ||
keywords: keywords.join(' '), | ||
body: text | ||
@@ -59,3 +85,3 @@ }; | ||
documentsStore[doc.url] = doc; | ||
searchIndex.add(doc); | ||
getSearchIndex(this).add(doc); | ||
@@ -71,3 +97,3 @@ return page; | ||
return this.output.writeFile('search_index.json', JSON.stringify({ | ||
index: searchIndex, | ||
index: getSearchIndex(this), | ||
store: documentsStore | ||
@@ -74,0 +100,0 @@ })); |
@@ -5,3 +5,3 @@ { | ||
"main": "index.js", | ||
"version": "1.1.0", | ||
"version": "1.2.0", | ||
"engines": { | ||
@@ -11,4 +11,5 @@ "gitbook": ">=3.0.0-pre.0" | ||
"dependencies": { | ||
"lunr": "0.5.12", | ||
"gitbook-plugin-search": "*" | ||
"gitbook-plugin-search": "*", | ||
"html-entities": "1.2.0", | ||
"lunr": "0.5.12" | ||
}, | ||
@@ -26,7 +27,12 @@ "homepage": "https://github.com/GitbookIO/plugin-lunr", | ||
"properties": { | ||
"maxIndexSize": { | ||
"type": "number", | ||
"title": "Limit size for the index", | ||
"default": 1000000 | ||
} | ||
"maxIndexSize": { | ||
"type": "number", | ||
"title": "Limit size for the index", | ||
"default": 1000000 | ||
}, | ||
"ignoreSpecialCharacters": { | ||
"type": "boolean", | ||
"title": "Ignore special characters in words", | ||
"default": false | ||
} | ||
} | ||
@@ -33,0 +39,0 @@ }, |
@@ -33,4 +33,20 @@ # lunr | ||
### Disable indexing of a page | ||
### Adding keywords to a page | ||
You can specify explicit keywords for any page. When searching for these keywords, the page will rank higher in the results. | ||
```md | ||
--- | ||
search: | ||
keywords: ['keyword1', 'keyword2', 'etc.'] | ||
--- | ||
# My Page | ||
This page will rank better if we search for 'keyword1'. | ||
``` | ||
### Disabling indexing of a page | ||
You can disable the indexing of a specific page by adding a YAML header to the page: | ||
@@ -48,1 +64,15 @@ | ||
### Ignoring special characters | ||
By default, special characters will be taken into account, to allow special searches like "C++" or "#word". You can disable this if your text is essentially English prose with the `ignoreSpecialCharacters` option: | ||
```js | ||
{ | ||
"pluginsConfig": { | ||
"lunr": { | ||
"ignoreSpecialCharacters": true | ||
} | ||
} | ||
} | ||
``` |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
33361
137
77
3
1
+ Addedhtml-entities@1.2.0
+ Addedhtml-entities@1.2.0(transitive)