Highlight 文字高亮
文字關鍵字高亮:依關鍵字將內容切成普通文字與命中段(splitByKeywords,大小寫歸一/最長匹配/非重疊掃描),命中段渲染為 lh-highlight__mark。服務搜尋結果摘要、日誌關鍵字標註等場景。
平台差異(登記):本端無平台特化——切分、命中換算與無障礙標籤全部複用 share core 純函式;視圖側將 Segment[] 渲染為 text 段循環(uni-app 的 inline 文字節點),mark 視覺由 share 樣式源統一承擔。
基礎用法
vue
<template>
<lh-highlight content="lh-ui 是一个跨端组件库家族" keywords="跨端" />
<lh-highlight content="LH-UI 跨端组件库,lh-ui 家族" keywords="lh-ui" />
</template>多關鍵字與大小寫敏感
keywords 支援字串陣列;同位置多關鍵字命中時取最長匹配。caseSensitive 預設 false(大小寫歸一匹配,命中段原樣輸出):
vue
<lh-highlight
content="lh-ui 跨端组件库家族,双血脉架构,八仓齐发"
:keywords="['lh-ui', '跨端组件库', '组件库']"
/>
<lh-highlight content="LH-UI 跨端组件库,lh-ui 家族" keywords="lh-ui" case-sensitive />內容截斷與自訂高亮樣式
maxLength > 0 時先截斷內容並附加上 …,再切分(省略號位置穩定);highlightClass / highlightStyle 透傳至命中段 mark:
vue
<lh-highlight
content="lh-ui 是一个跨端组件库家族,双血脉架构,八仓齐发"
keywords="双血脉"
:max-length="20"
/>
<lh-highlight
content="自定义类名与内联样式"
keywords="自定义"
highlight-class="my-highlight"
highlight-style="color: #B87800; text-decoration: underline;"
/>Props
| 屬性 | 說明 | 類型 | 預設值 |
|---|---|---|---|
| content | 文字內容(必填) | string | — |
| keywords | 關鍵字(字串或陣列;空字串自動忽略) | string | string[] | — |
| caseSensitive | 大小寫敏感(false 時大小寫歸一匹配) | boolean | false |
| highlightClass | 附加到命中段 mark 的類別 | string | — |
| highlightStyle | 命中段 mark 的 inline 樣式字串 | string | — |
| maxLength | 內容最大長度(>0 時截斷並附加 …,再切分) | number | — |
| highlightLabel | 無障礙標籤(未傳取內容前 40 字摘要) | string | — |
| customClass | 自訂類別(透傳至根節點) | string | — |
| sx | sx 快速佈局屬性 | LhSxProps | — |
無障礙
根節點 aria-label 預設取內容前 40 字摘要,可用 highlightLabel 自訂;整段無命中時根節點附加 lh-highlight--plain 修飾類,供使用方區分「無命中」狀態。

