npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@rodneylab/searchlite

v0.0.5

Published

Manipulate HTML returning document with search terms highlighted

Downloads

8

Readme

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.

🗺️ 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.