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

highlight-words

v2.0.0

Published

Split a piece text into multiple chunks based on a search query, allowing you to highlight the matches.

Downloads

681,494

Readme

Give it a piece of text and a search query, and it splits it into chunks separating matches from non-matches, allowing you to highlight the matches, visually or otherwise, in your app.

Build status Node Version Npm version Npm downloads License

Installation

pnpm add highlight-words
npm i --save highlight-words

Usage

To use it, give it the body of text to search in and the query to search for.

import highlightWords from 'highlight-words';

const chunks = highlightWords({
  text: 'The quick brown fox jumped over the lazy dog',
  query: 'over'
});

console.log(chunks);
/*
[
  {
    key: '62acb210-76dd-4682-b948-8d359a966dcb'
    text: 'The brown fox jumped ',
    match: false
  },
  {
    key: '69779adf-6d7c-45ec-ae9b-49d0cb292e28';
    text: 'over',
    match: true
  },
  {
    key: '46c5b7a0-5414-47c5-81ba-2496f33fe2f6';
    text: ' the lazy dog',
    match: false
  }
]
*/

Play with this example on Code Sandbox.

Alternatively, you can also use a regular expression as part of the query string to search for.

const chunks = highlightWords({
  text: 'The quick brown fox jumped over the lazy dog',
  query: '/(quick|brown|fox)/'
});

console.log(chunks);
/*
[
  {
    key: '62acb210-76dd-4682-b948-8d359a966dcb'
    text: 'The ',
    match: false
  },
  {
    key: '69779adf-6d7c-45ec-ae9b-49d0cb292e28'
    text: 'quick',
    match: true
  },
  {
    key: 'a72a6578-9111-4dca-a30d-676aaf7964c0'
    text: ' ',
    match: false
  },
  {
    key: '24301852-f38b-4b54-b1fe-d8a82f47c49e'
    text: 'brown',
    match: true
  },
  {
    key: 'a72a6578-9111-4dca-a30d-676aaf7964c0'
    text: ' ',
    match: false
  },
  {
    key: '797b3bab-9244-451c-b246-4b03025b0691'
    text: 'fox',
    match: true
  },
  {
    key: 'c86dbb8a-3e5f-4c05-a48c-684abbb517e8'
    text: ' jumped over the lazy dog',
    match: false
  }
]
*/

If using a regular expresssion, you can choose to either enclose the pattern in a string or not. In both cases, you need to format the regular expression properly, i.e. enclose the pattern between slashes, and you need to create capture groups, so that the match is remembered for later use.

Valid regular expression usage

query: '/(quick|brown|fox)/'
query: /(quick|brown|fox)/

Invalid regular expression usage

query: '(quick|brown|fox)'
query: '/quick|brown|fox/'
query: /quick|brown|fox/

Options

You can add a few options for the highlighter.

  • clipBy. If you want to clip the occurences that are not a match and display elipses around them. This can help to provide context around your matches.
  • matchExactly. By default, the highlighter will look for occurences of either words in your query. For example, if you have brown fox as your query, the highlighter will consider both brown and fox as separate matches. If set to true, then only the exact match of brown fox will be considered.

matchExactly will have no effect if you're using a regular expression as your query, since you will have full control over the query in that case.

Arguments

highlightWords accepts an object as an argument, with the following structure:

| Property | Type | Required? | Description | Default | | :------------- | :------ | :-------: | :------------------------------------------------------------ | :------ | | text | String | ✓ | The body of text you want to search in. | empty | | query | String | ✓ | The word(s) or regular expression you want to search for. | empty | | clipBy | Number | | How many words do you want to clip from the non matches. | null | | matchExactly | Boolean | | Should we match the complete query or individual words in it? | false |

What it returns

highlightWords returns an array of objects, each object with the following structure:

| Property | Type | Description | | :------- | :------ | :------------------------------------------------------------------------------------------------------ | | key | String | A unique key to help you when you want to use the chunks in a map function, e.g. with React or Angular. | | text | String | The word or words in the chunk. | | match | Boolean | Is this chunk a match for your search? |

Use it with the framework of your choice

By default, the highlighter won't assume any HTML element to wrap matched text, so you can do whatever you want with the matches.

React

<p>
  {chunks.map(({ text, match, key }) =>
    match ? (
      <span className="highlight" key={key}>
        {text}
      </span>
    ) : (
      <span key={key}>{text}</span>
    )
  )}
  };
</p>

Play with the React example on Code Sandbox.

Angular

<p>
  <span *ngFor="let chunk of chunks; trackBy: key" class="highlight">
    {{ chunk.text }}
  </span>
</p>

Play with the Angular example on Code Sandbox.

Vue

<p>
  <span
    v-for="chunk in chunks"
    :key="chunk.key"
    v-bind:class="{ active: chunk.match }"
  >
    {{ chunk.text }}
  </span>
</p>

Play with the Vue example on Code Sandbox.

Svelte

<p>
  {#each chunks as chunk (chunk.key)}
  <span class:highlight="{chunk.match}">{chunk.text}</span>
  {/each}
</p>

Play with the Svelte example on Code Sandbox.

A note on accessibility

When we are splitting a piece of text into multiple chunks for the purpose of styling each chunk differently, and then using said chunks instead of the original text, we are doing a disservice to our users who might rely on a screen reader. This is because some screen readers will read out the chunks of text individually rather than in one continous flow. For example, if we were to split the text Eeeh! Help me!, highlight-words will return to us several chunks. We then might decide to wrap each chunk's text in a span, like so:

<p>
  <span>E</span>
  <span>e</span>
  <span>e</span>
  <span>h! H</span>
  <span>e</span>
  <span>lp m</span>
  <span>e</span>
  <span>!</span>
</p>

Some screen readers will announce each letter e individually. Not ideal!

Let's make it accessible by using aria attributes to allow screen readers to correctly announce our text.

<p aria-label="Eeeh! Help me!">
  <span aria-hidden="true">E</span>
  <span aria-hidden="true">e</span>
  <span aria-hidden="true">e</span>
  <span aria-hidden="true">h! H</span>
  <span aria-hidden="true">e</span>
  <span aria-hidden="true">lp m</span>
  <span aria-hidden="true">e</span>
  <span aria-hidden="true">!</span>
</p>

or, for less repetition:

<p aria-label="Eeeh! Help me!">
  <span aria-hidden="true">
    <span>E</span>
    <span>e</span>
    <span>e</span>
    <span>h! H</span>
    <span>e</span>
    <span>lp m</span>
    <span>e</span>
    <span>!</span>
  </span>
</p>

For a much better write-up than I could put together, have a read of Michelle Barker's How to Accessibly Split Text.

License

MIT License - fork, modify and use however you want.