# Highlight 文本高亮
關鍵詞高亮:按關鍵詞把內容切成普通文本與命中段(splitByKeywords,
大小寫歸一、重疊詞取最長匹配、轉義正則元字元),命中段渲染為
<mark class="lh-highlight__mark">;整段無命中時根加 --plain 修飾。
文本高亮(單關鍵詞 / 多關鍵詞 / 大小寫敏感 / 自訂樣式 / 截斷)
<template>
<div class="demo-col" style="gap: 18px; max-width: 480px">
<LhHighlight :content="content" keywords="lh-ui" />
<LhHighlight :content="content" :keywords="['雙血脈', '八倉齊發']" />
<LhHighlight :content="content" keywords="LH-UI" case-sensitive />
<LhHighlight
:content="content"
keywords="家族"
highlight-class="custom-mark"
highlight-style="border-bottom: 2px solid var(--lh-warning-color);"
/>
<LhHighlight :content="content" :max-length="20" keywords="雙血脈" />
</div>
</template>
<script>
export default {
data: () => ({
content: 'lh-ui 是一個跨端元件庫家族,雙血脈架構,八倉齊發,文件站台十站齊備',
}),
}
</script>
# 多關鍵詞與最長匹配
keywords 支援字串與陣列兩種形態;陣列按長度降序去重,同位置多關鍵詞
命中時取最長者切分(lh-ui 與 lh 同時傳入時優先匹配 lh-ui):
<LhHighlight :content="content" :keywords="['lh-ui', 'lh']" />
# 自訂命中段樣式
highlightClass 追加到 mark 的類別名,highlightStyle 作為行內樣式字串
注入 mark(小程式端無 style 字串解析能力的場景可藉此傳色):
<LhHighlight
:content="content"
keywords="家族"
highlight-class="custom-mark"
highlight-style="border-bottom: 2px solid var(--lh-warning-color);"
/>
# 作用域插槽
highlight 插槽接收 { keyword, index, segment },用於自訂命中段的內部
渲染(外層始終是帶 highlightClass / highlightStyle 的 mark):
<LhHighlight :content="content" keywords="lh-ui">
<template #highlight="{ keyword, index }">
<span class="my-mark">第 {{ index }} 處命中:{{ keyword }}</span>
</template>
</LhHighlight>
# Props
| 屬性 | 說明 | 類型 | 預設值 |
|---|---|---|---|
| content | 文本內容(必填) | string | — |
| keywords | 關鍵詞(字串或陣列;陣列按長度降序去重,同位置取最長匹配) | string \| string[] | — |
| caseSensitive | 大小寫敏感(預設 false:大小寫歸一匹配,原樣輸出命中文本) | boolean | false |
| highlightClass | 追加到 mark 的類別名 | string | — |
| highlightStyle | mark 行內樣式字串 | string | — |
| maxLength | 內容最大長度(>0 時截斷至該長度並追加 …,再切分) | number | — |
| highlightLabel | 無障礙名稱(未傳取內容前 40 字元摘要) | string | — |
| sx | sx 快速版面配置屬性 | LhSxProps | — |
# Slots
| 插槽名 | 說明 | 作用域參數 |
|---|---|---|
| highlight | 自訂命中段渲染(預設渲染 mark 包命中文本) | keyword(命中的關鍵詞)、index(關鍵詞序號)、segment(切分段) |
# 無障礙
根元素 aria-label 由 highlightLabel 提供,未傳時取內容前 40 字元摘要。
命中段使用瀏覽器原生 <mark> 語義(無命中時不輸出任何 mark)。
Copyright © 2024-2026 风默文学 | 宁ICP备2024006574号 |
苏公网安备32040002010537