kiwicom / docsify-lunr-search Goto Github PK
View Code? Open in Web Editor NEWSearch plugin for docsify.js which uses https://lunrjs.com/
License: MIT License
Search plugin for docsify.js which uses https://lunrjs.com/
License: MIT License
Context
Lunr leverages "+" as a word prefix to indicate it must be present within a document. The below example searches for documents that must contain “foo”, might contain “bar” and must not contain “baz”:
idx.search("+foo bar -baz")
This particular plugin layers additional functionality after the Lunr search. It leverages the query to highlight the resultant matches.
The bug
The query is fed directly into the RegExp constructor which causes a failure for a query of the form "+foo" with the following error:
Bug location:
Line 142 in 8745b0a
if (body != "") {
var regEx = new RegExp("(" + query + ")", "ig"); // <---- This is where the bug occurs
content += escapeHtml(body.substring(start, end)).replace(
regEx, ("<em class=\"search-keyword\">$1</em>")) + "...";
}
Potential Fix
A quick hack might be to remove + from the query string:
if (query) {
var escapedQuery = query.replace(/\+/g, "");
if (escapedQuery) {
var queryParts = escapedQuery.split(" ");
var substring = body.substring(start, end);
for (var i = 0; i < queryParts.length; i++) {
var queryPart = queryParts[i];
var regEx = new RegExp("(" + queryPart + ")", "ig");
substring = substring.replace(
regEx, ("<em class=\"search-keyword\">$1</em>"));
}
content += substring + "...";
}
}
Is it possible to adapt it so both level 1 (page title) and level 2 headings (section title) show up as titles in the search results?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.