# document-highlighter

> Highlight a search-query in a HTML document

Latest version **2.1.0** (published 2023-01-10) · 0 weekly downloads

## Install

```sh
npm install document-highlighter
pnpm add document-highlighter
yarn add document-highlighter
bun add document-highlighter
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.1.0 |
| Published | 2023-01-10 |
| First published | 2014-01-06 |
| Weekly downloads | 0 |
| TypeScript types | none |
| Module format | CommonJS |
| Node | >=0.10 |
| Dependencies | 0 |
| Unpacked size | 920.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 57 |
| Author | Matthieu Bacconnier |
| Maintainers | matthieu.bacconnier, merlin_nd |
| Keywords | highlight, highlighting, html, snippet, query, queries, search |

## Links

- npm: https://www.npmjs.com/package/document-highlighter
- Repository: https://github.com/Neamar/document-highlighter
- Homepage: https://github.com/Neamar/document-highlighter#readme
- Issues: https://github.com/Neamar/document-highlighter/issues
- npm.io page: https://npm.io/package/document-highlighter

## Alternatives

- [@tsparticles/shape-image](https://npm.io/package/@tsparticles/shape-image.md) — 303.7K weekly downloads
- [@tsparticles/shape-line](https://npm.io/package/@tsparticles/shape-line.md) — 233.7K weekly downloads
- [stringify-attributes](https://npm.io/package/stringify-attributes.md) — 58.6K weekly downloads
- [mobile-drag-drop](https://npm.io/package/mobile-drag-drop.md) — 46.3K weekly downloads
- [@comunica/actor-rdf-parse-html](https://npm.io/package/@comunica/actor-rdf-parse-html.md) — 29.2K weekly downloads

## Recent versions

- 2.1.0 (latest) — 2023-01-10
- 2.0.7 — 2015-12-22
- 2.0.6 — 2015-02-09
- 2.0.5 — 2015-01-27
- 2.0.4 — 2014-12-18
- 2.0.3 — 2014-10-30
- 2.0.2 — 2014-09-29
- 2.0.1 — 2014-08-28
- 2.0.0 — 2014-08-01
- 1.0.8 — 2014-04-21
- 1.0.7 — 2014-02-10
- 1.0.5 — 2014-02-03
- 1.0.4 — 2014-01-20
- 1.0.3 — 2014-01-15
- 1.0.2 — 2014-01-08
- … 2 more at https://npm.io/package/document-highlighter/versions

## README

Content aware document Highlighter
=======================
![Build Status](https://travis-ci.org/Neamar/document-highlighter.png)
![Coverage Status](https://coveralls.io/repos/Neamar/document-highlighter/badge.png?branch=master)

## What is `document highlighter`?
Add highlight to a raw / HTML document for the specified query. Handle unicode, stop-words and punctuation.
Generate HTML-compliant highlights, even for complex markup.

## Samples
### Plain text
#### Simple case
The following text :

> The index analysis module acts as a configurable registry of Analyzers that can be used in order to both break indexed (analyzed) fields when a document is indexed and process query strings. It maps to the Lucene Analyzer.

When highlighted for the query `The index analysis string` will become:

> **The index analysis** module acts as a configurable registry of Analyzers that can be used in order to both break indexed (analyzed) fields when a document is indexed and process query **strings**. It maps to the Lucene Analyzer.

Note generated markup is minimal (one item per match, and not one item per word).

#### Stopwords
Document highlighter handles stopwords and punctuation according to the language specified. For instance, the following text:

> Install this library, and start using it.

When highlighted for the query `install library` will become:

> **Install this library**, and start using it.

### HTML
This also works for HTML documents, e.g. :

> This document contains _italics_ and stuff.

When highlighted for the query `it contains some italic empty` will become:
> This document **contains _italics_** and stuff.

Document highlighter maintains original markup and add wrapping tags as needed.

## Usage
### Highlight plain text documents
```javascript
var highlighter = require('document-highlighter');

var hl = highlighter.text(
    'In JavaScript, you can define a callback handler in regex string replace operations',
    'callback handler in operations'
);

console.log(hl.text);
// "In JavaScript, you can define a <strong>callback handler in</strong> regex string replace <strong>operations</strong>"

console.log(hl.indices);
// [
//   { startIndex: 32, endIndex: 51, content: 'callback handler in' },
//   { startIndex: 73, endIndex: 83, content: 'operations' }
// ]
```

### Highlight HTML documents
```javascript
var highlighter = require('document-highlighter');

var hl = highlighter.html(
    '<em>Eat drink and be merry</em> for tomorrow we die',
    'merry for tomorrow'
);

console.log(hl.html);
// <em>Eat drink and be <strong>merry</strong></em><strong class="secondary"> for tomorrow</strong> we die

console.log(hl.text);
// Eat drink and be <strong>merry for tomorrow</strong> we die
```

### Customize highlight markup
```javascript
var highlighter = require('document-highlighter');

var hl = highlighter.text(
    'In JavaScript, you can define a callback handler in regex string replace operations',
    'callback handler in operations',
    {
        before: '<span class="hlt">',
        after: '</span>',
    }
);

console.log(hl.text);
// "In JavaScript, you can define a <span class="hlt">callback handler in</span> regex string replace <span class="hlt">operations</span>"
```

> Note: in HTML mode, your highlight may be split up in multiple items in order to keep your existing markup (block level elements stop inline highlighting). The default is to add a `.secondary` class; but you can override this using the `beforeSecond` key in the option.

In some case, you may want to customize highlighting for all calls to the highlighter. You can use `defaultOptions` parameter. Note you cannot directly override this with a new object; you need to update the keys one by one.

```javascript
var highlighter = require('document-highlighter');
highlighter.defaultOptions.before = '<span class="hlt">';
highlighter.defaultOptions.after = '</span>';

var hl = highlighter.text(
    'In JavaScript, you can define a callback handler in regex string replace operations',
    'callback handler in operations'
);

console.log(hl.text);
// "In JavaScript, you can define a <span class="hlt">callback handler in</span> regex string replace <span class="hlt">operations</span>"
```

---
_Source: https://npm.io/package/document-highlighter · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
