https://github.com/oramasearch/highlight
🖍️ Highlight any text in JavaScript
https://github.com/oramasearch/highlight
Last synced: over 1 year ago
JSON representation
🖍️ Highlight any text in JavaScript
- Host: GitHub
- URL: https://github.com/oramasearch/highlight
- Owner: oramasearch
- License: other
- Created: 2023-10-05T09:48:40.000Z (almost 3 years ago)
- Default Branch: main
- Last Pushed: 2024-11-05T12:27:41.000Z (almost 2 years ago)
- Last Synced: 2024-11-05T13:26:44.428Z (almost 2 years ago)
- Language: TypeScript
- Homepage: https://www.npmjs.com/package/@orama/highlight
- Size: 153 KB
- Stars: 54
- Watchers: 1
- Forks: 4
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE.md
Awesome Lists containing this project
README
# Highlight
[](https://github.com/oramasearch/highlight/actions/workflows/test.yml)
Orama Highlight allows you to easily highlight substrings in a given input.
# Installation
```bash
npm i @orama/highlight
bun i @orama/highlight
```
# Usage
```js
import { Highlight } from '@orama/highlight'
const inputString = 'The quick brown fox jumps over the lazy dog'
const toHighlight = 'brown fox jump'
const highlighter = new Highlight()
const highlighted = highlighter.highlight(inputString, toHighlight)
console.log(highlighted.positions)
// [
// {
// start: 10,
// end: 14
// }, {
// start: 16,
// end: 18
// }, {
// start: 20,
// end: 23
// }
// ]
console.log(highlighted.HTML)
// "The quick brown fox jumps over the lazy dog"
console.log(highlighted.trim(10))
// "...uick brown..."
```
You can always customize the library behavior by passing some options to the class constructor:
```js
const highlighted = new Highlight({
caseSensitive: true, // Only highlight words that respect the second parameter's casing. Default is false
wholeWords: true, // Only highlight entire words, no prefixes
HTMLTag: 'div', // Default is "mark"
CSSClass: 'my-custom-class' // default is 'orama-highlight'
})
```
# License
[Apache 2.0](/LICENSE.md)