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

hit-highlighter

v4.0.1

Published

Small and simple hit highlighter for search engines running in the browser and Node.js

Downloads

67

Readme

hit-highlighter

NPM version NPM downloads MIT License Build Status JavaScript Style Guide

A small and versatile hit highlighter for search engines running in the browser and Node.js. Language agnostic, meaning it supports all languages that can be split into words with code.

Takes a query array and where the values matches within the search result array, it adds hightight code. Goes well with words'n'numbers for extracting words (and numbers) from a string of text.

Also part of daq-proc, which is meant as a hassle free document and query processor for search engines running in the browser.

Breaing change

API is changed, both how to import for CJS and ESM and how to reference when using <script> tag.

Browser demo

Check out the demo to better understand how the hit-highlighter works. Browser demo

Initialize

UMD / Browser

<script src="https://cdn.jsdelivr.net/npm/hit-highlighter/dist/hit-highlighter.umd.min.js"></script>

<script>
  //hh.highlight() available
</script>

CJS

const { highlight } = require ('hit-highlight')
// highlight() available

ESM

import { highlight } from 'hit-highlight'
// highlight() available

Usage

hightlight([query array], [item array], {prpoerties})

query and item are arrays of words. properties is optional to define, since you have defaultProperties:

defaultProperties = {
  itemMaxWords: 0,
  truncateStart: '<span class="truncated">',
  truncateEnd: '</span>',
  hitPaddingMin: 5,
  highlightStart: '<span class="hitHighlight">',
  highlightEnd: '</span>',
  divider: ' '
}

If you want to overwrite anything, i.e. maximum words to show in an item, you can do:

hightlight([query array], [item array], {itemMaxWords: 100})

Default highlighting

const query = ['interesting', 'words']
const item = ['some', 'interesting', 'words', 'to', 'remember']

highlight(query, item)

// returns:
// 'some <span class="hitHighlight">interesting words</span> to remember '

Custom highlight.start and .end

const query = ['interesting', 'words']
const item = ['some', 'interesting', 'words', 'to', 'remember']
const properties = { highlightStart: '**', highlightEnd: '**' }

highlight(query, item, properties)

// returns:
// 'some **interesting words** to remember '

No hits, returing item untouched

const query = ['no', 'hits']
const item = ['some', 'interesting', 'words', 'to', 'remember']

highlight(query, item)

// returns:
// 'some interesting words to remember '