export function highlight()

in src/utils/highlight.ts [12:43]


export function highlight(
  content: string,
  tokens: string[],
  forceMatched?: boolean
): string {
  const html: string[] = [];

  for (const token of tokens) {
    const index = content.toLowerCase().indexOf(token);
    if (index >= 0) {
      if (index > 0) {
        html.push(highlight(content.substr(0, index), tokens));
      }
      html.push(
        `<mark>${escapeHtml(content.substr(index, token.length))}</mark>`
      );
      const end = index + token.length;
      if (end < content.length) {
        html.push(highlight(content.substr(end), tokens));
      }
      break;
    }
  }

  if (html.length === 0) {
    return forceMatched
      ? `<mark>${escapeHtml(content)}</mark>`
      : escapeHtml(content);
  }

  return html.join("");
}