Giter Site home page Giter Site logo

docsify-lunr-search's People

Contributors

paveldedik avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

docsify-lunr-search's Issues

[BUG] Leveraging "+" Lunr Operator

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:

Screen Shot 2019-09-05 at 4 08 28 PM

Bug location:

var regEx = new RegExp("(" + query + ")", "ig");

        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 + "...";
          }
        }

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.