keyword-mark

1.0.6 • Public • Published

keyword-mark

npm i keyword-mark
import keywordMark from 'keyword-mark'

/**
 * This function searches for a keyword in a given content and returns the result.
 * @param {string} content
 * @param {string} keyword
 * @param {Object|null} options
 * @param {boolean} options.full - Whether to search for the full word only (default: true).
 * @param {function} options.handle - A function to handle the search result (default: null).
 * @param {boolean} options.ignoreCase - Whether to ignore case when searching (default: false).
 * @param {string} options.tag - The HTML tag to wrap the search result in (default: 'em').
 * @param {boolean} options.split (default: true).
 * @returns {string}
 */
function keywordMark(content, keyword, options = null)

Example

const content = 'Lorem ipsum dolor sit amet, consectetuer adIPiscing elit'
const keyword = 'ip ad i'

const result1 = keywordMark(content, 'ip')
const result2 = keywordMark(content, keyword)
const result3 = keywordMark(content, keyword, {
  ignoreCase: true,
})
const result4 = keywordMark(content, keyword, {
  full: false,
})
const result5 = keywordMark(content, keyword, {
  handle(match) {
    return `<small>${match}</small>`
  }
})
const result6 = keywordMark(content, keyword, {
  tag: 'strong'
})
const result7 = keywordMark(content, keyword, {
  ignoreCase: true,
  full: false,
  handle(match) {
    return `<u>${match}</u>`
  }
})

console.log(result1)
//Lorem <em>ip</em>sum dolor sit amet, consectetuer adIPiscing elit

console.log(result2)
//Lorem <em>ip</em>sum dolor s<em>i</em>t amet, consectetuer <em>ad</em>IP<em>i</em>sc<em>i</em>ng el<em>i</em>t

console.log(result3)
//Lorem <em>ip</em>sum dolor s<em>i</em>t amet, consectetuer <em>ad</em><em>IP</em><em>i</em>sc<em>i</em>ng el<em>i</em>t

console.log(result4)
//Lorem <em>ip</em>sum <em>d</em>olor s<em>i</em>t <em>a</em>met, consectetuer <em>ad</em>IP<em>i</em>sc<em>i</em>ng el<em>i</em>t

console.log(result5)
//Lorem <small>ip</small>sum dolor s<small>i</small>t amet, consectetuer <small>ad</small>IP<small>i</small>sc<small>i</small>ng el<small>i</small>t

console.log(result6)
//Lorem <strong>ip</strong>sum dolor s<strong>i</strong>t amet, consectetuer <strong>ad</strong>IP<strong>i</strong>sc<strong>i</strong>ng el<strong>i</strong>t

console.log(result7)
//Lorem <u>ip</u>sum <u>d</u>olor s<u>i</u>t <u>a</u>met, consectetuer <u>ad</u><u>IP</u><u>i</u>sc<u>i</u>ng el<u>i</u>t

Package Sidebar

Install

npm i keyword-mark

Weekly Downloads

4

Version

1.0.6

License

MIT

Unpacked Size

3.73 kB

Total Files

3

Last publish

Collaborators

  • yyyymmddhhmmss