# 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