RichText
Sanitizes and displays HTML or nodes; separate image blocks support preview and links emit events.
Import
Use <hui-rich-text> after configuring uni-app easycom. Import public types and functions from @nscode/handy-uni. Ordinary Vue applications install the @nscode/handy-uni/web preview plugin.
Examples
Each example is standalone. Controls show the current value or event result on the page.
<template>
<view>
<hui-rich-text
:content="content"
:preview-image="false"
@link-click="(value) => (lastEvent = JSON.stringify(value))"
@image-click="(value) => (lastEvent = JSON.stringify(value))"
/>
<text>{{ lastEvent }}</text>
</view>
</template>
<script setup lang="ts">
import { ref } from 'vue'
const lastEvent = ref('尚无事件 / No event')
const content =
'<p><strong>正文 / Article</strong></p><p><a href="https://example.com">链接 / Link</a></p><img src="' +
'data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADAAAAAgCAIAAADbtmxLAAAB/0lEQVR4nM3QVUIUABAA0DmEgCDYgoCBqEgoiIiBSkp3d9d2dy/d3d3d3SWooBJ2YAL/XmPeER4ccyaoupKOu1M0PGma3oyTvqzT/pyzgbzzwQLtUNHFcIlepOxStOJKbJpBfIZhYtaN5Byj1DxjYoEpueg2tcScXnaXWXGPXXWfW/OAX/dI2GAjbnoqbbGVt9krOxzTu55n9rhk97nlDnjkD3kVjvgUj/mVTgSUTwVVzoRUz4XVLkTUL0U1rsQ0r8W1rie0vwQVF6KaG1ndg3rCi67lwzzlxz4TwD0XxL8QItQJE+tGSPWj5JdjlFfj0q8lZF5Pyr6ZknuLkG9CKjSjFN+hlVowyi1ZlVacamte7UNB/WNR4xNJ8zNZq52i3SGt0ymj2zmr1zWn3z1v0LNg2Lto1Ldk3L9sMrBiOrhqNrRmPrxuMbJhObppNbblRXzbRmLHK0DVk9T5GlD1JHdtAqqelO4tQNWT2vMGUPUQet8Cqh5i3ztA1UPq3wZUPeSBHUDVQxncBVQ91KE9QNVDG34PqHroIx8AVQ9j9COg6mGOfQJUPazxz4Cqhz3xBVD1cCa/Aqoe7tQ3QNXDm/4OqHr4Mz8AVY9gdh9Q9QjnfgKqHtH8L0DVI174Dah6JIt/AFWPdOkvoOqRLf8DVD3ylQNA1aNYPQRUPcq1o/83VquIElJ47QAAAABJRU5ErkJggg==' +
'" alt="风景 / Landscape"><p>结束 / End</p>'
</script>
Props
| Prop | Type | Default | Details |
|---|---|---|---|
content | string | RichTextNode[] | — | HTML string or rich-text node list. |
imageMode | RichTextImageMode | 'split' | Configures image mode. |
previewImage | boolean | true | Configures preview image. |
tagStyle | Record<string, string> | — | Per-tag safe style overrides. |
transformUrl | TransformUrl | — | Configures transform url. |
selectable | boolean | false | Enables selection and change events. |
lazy | boolean | true | Configures lazy. |
Root classes and styles use customClass and customStyle. Numbers representing dimensions use px; durations use ms unless explicitly documented.
Events
| Event | Arguments |
|---|---|
image-click | (context: { src: string; index: number }) |
link-click | (context: { href: string }) |
Controlled state only changes when the parent accepts its update event. Events with multiple words use kebab-case. Disabled or stale asynchronous actions do not emit a successful state change.
Slots
| Slot | Scope |
|---|---|
default | — |
Methods
None.
Types
RichTextImageMode、RichTextProps、RichTextEmits.
Platform notes
HTML strings pass a shared allowlist removing event attributes, scripts and unsafe URLs. Node arrays are passed to native rich-text; ordinary Web converts them to safe HTML, without splitting image blocks. Links emit link-click and the application decides navigation. Standalone images use Image blocks. Images inside tables, links and pre/code remain inline; WeChat rich-text cannot report their inner events.
Unchanged content and tagStyle fields reuse the parsed result. transformUrl reads the latest function only when parsing; update content or tagStyle after changing URL rules to parse again.
Device smoke checklist.