Git Product home page Git Product logo

searchlite's Introduction

Rodney Lab search lite down Github banner

Rodney Lab logo

searchlite

Rust WASM tool to manipulate HTML searching for an input search term. Searchlite will wrap matching text elements in a pair of HTML <mark> tags. Browsers will highlight these matches by default.

Module can be used within a web app or in serverless middleware to highlight search terms in an HTML response.

Uses html5ever and aho-corasick Rust crates under the hood.

Compile WASM

  1. Clone the project and change into the project directory. Then run these commands:
cargo install wasm-pack # skip if you already have it installed
wasm-pack build --target web
  1. Copy the generated pkg folder into your JavaScript or TypeScript project.
  2. Import and use the code in one of your project source files (expected output is as shown in previous section):
  • Generate highlighted HTML

highlight_search_terms takes two arguments: the input HTML and the search term. Separate multiple search terms with a space (e.g. "apple pear").

import init, { highlight_search_terms as highlight } from "pkg/searchlite.js";

await init();

// alternative if top level await is not available
(async () => {
  await init();
})();

const highlightedHtml = highlight(
  "<h2>Heading</h2><p>Nobody likes maple in their apple flavoured Snapple. APPLE</p>",
  "apple",
);

Output (highlightedHTML):

<h2>Heading</h2>
<p>
  Nobody likes maple in their <mark id="search-match">apple</mark> flavoured
  Sn<mark>apple</mark>. <mark>APPLE</mark>
</p>

Note the id added to the first search match. You can use this to scroll the first match into view.

Searchlite example screen capture shows all instances of the letters `apple` highlighted whether in the work apple (lower case) alone, within the word Snapple or APPLE (upper case)

🗺️ Roadmap

No firm course laid in.

  • Possibly add stemmer, though this might be better handled externally. A stemmer will match on related words; film and filming for an input of films as an example. The Porter Algorithm is often used for this.
  • Possibly add a utility function to generate a match snippet for use in result pages that show matches across various documents.

☎️ Reach Out

Feel free to jump into the Rodney Lab matrix chat room.

searchlite's People

Contributors

rodneylab avatar

Watchers

 avatar  avatar

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.