Skip to content

Instantly share code, notes, and snippets.

View splenwilz's full-sized avatar

Godswill William splenwilz

View GitHub Profile
@splenwilz
splenwilz / Highlight.ts
Last active April 5, 2024 09:23
This TypeScript function, `highlightKeyword`, is designed to highlight occurrences of keywords within HTML content. It removes existing <span> tags before highlighting, and it avoids replacing text within specific HTML attributes such as href, alt, or title. To use this function, simply pass the content, query (keyword or keywords separated by c…
/**
* Highlights occurrences of keywords within content.
* @param {string} content - The content in which to highlight keywords.
* @param {string} query - The keyword or keywords to highlight (comma-separated).
* @param {boolean} removeSpans - Whether to remove existing <span> tags before highlighting. Default is true.
* @returns {string} The content with highlighted keywords.
*/
export function highlightKeyword(content: string, query: string, removeSpans = true): string {
if (!query) return content;