# Highlight

Keyword highlighting: the content is split into plain text and matched segments by keyword (splitByKeywords, with case normalization, longest match on overlapping words and escaping of regex metacharacters); matched segments render as <mark class="lh-highlight__mark">; when nothing matches at all the root gets the --plain modifier.

Highlight (single keyword / multiple keywords / case-sensitive / custom styles / truncation)
<template>
  <div class="demo-col" style="gap: 18px; max-width: 480px">
    <LhHighlight :content="content" keywords="lh-ui" />
    <LhHighlight :content="content" :keywords="['two bloodlines', 'eight repos shipped together']" />
    <LhHighlight :content="content" keywords="LH-UI" case-sensitive />
    <LhHighlight
      :content="content"
      keywords="family"
      highlight-class="custom-mark"
      highlight-style="border-bottom: 2px solid var(--lh-warning-color);"
    />
    <LhHighlight :content="content" :max-length="20" keywords="two bloodlines" />
  </div>
</template>

<script>
export default {
  data: () => ({
    content: 'lh-ui is a cross-platform component library family: a two-bloodline architecture, eight repos shipped together, ten docs sites ready',
  }),
}
</script>

# Multiple keywords and longest match

keywords accepts both a string and an array; the array is deduplicated in descending length order, and when several keywords match at the same position the longest one wins the split (when lh-ui and lh are both passed, lh-ui matches first):

<LhHighlight :content="content" :keywords="['lh-ui', 'lh']" />

# Customizing matched segments

highlightClass is appended to the mark class name, and highlightStyle is injected into the mark as an inline style string (handy for passing in a color on mini-program runtimes that cannot parse style strings):

<LhHighlight
  :content="content"
  keywords="family"
  highlight-class="custom-mark"
  highlight-style="border-bottom: 2px solid var(--lh-warning-color);"
/>

# Scoped slot

The highlight slot receives { keyword, index, segment } and customizes the inner rendering of a matched segment (the outer element is always a mark carrying highlightClass / highlightStyle):

<LhHighlight :content="content" keywords="lh-ui">
  <template #highlight="{ keyword, index }">
    <span class="my-mark">Match {{ index }}: {{ keyword }}</span>
  </template>
</LhHighlight>

# Props

Prop Description Type Default
content Text content (required) string —
keywords Keywords (string or array; the array is deduplicated in descending length order, longest match at the same position) string \| string[] —
caseSensitive Case-sensitive (default false: case-normalized matching, the matched text is output as-is) boolean false
highlightClass Class name appended to the mark string —
highlightStyle Inline style string for the mark string —
maxLength Maximum content length (>0 truncates to this length and appends … before splitting) number —
highlightLabel Accessibility name (falls back to a 40-character summary of the content) string —
sx sx shorthand layout props LhSxProps —

# Slots

Slot Description Scoped props
highlight Custom matched-segment rendering (renders a mark wrapping the matched text by default) keyword (the matched keyword), index (keyword index), segment (the split segment)

# Accessibility

The root element's aria-label comes from highlightLabel, falling back to a 40-character summary of the content when it is not passed. Matched segments use the browser's native <mark> semantics (no mark is output when nothing matches).

Last Updated: 12/4/2026, 12:49:57 AM

Copyright © 2024-2026 FengMo WenXue