Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

hit-highlighter

Package Overview
Dependencies
Maintainers
1
Versions
14
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

hit-highlighter

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

  • 2.0.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
12
increased by20%
Maintainers
1
Weekly downloads
 
Created
Source

hit-highlighter

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.

NPM version NPM downloads MIT License Build Status JavaScript Style Guide

Browser demo

Browser demo

Check out the interactive demo.

Initialize

Browser

<script src="hit-highlighter.js"></script>

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

Node.js

const highlight = require ('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: '',
  truncateEnd: '... ',
  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'
//]

Keywords

FAQs

Package last updated on 22 Apr 2020

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc