Skip to content
# t-markdown

Markdown 富文本渲染(Canvas)

t-markdown 组件介绍 ​

t-markdown 是基于 Canvas 的高性能 Markdown 渲染组件,块级排版 + 可视区虚拟化绘制。支持标题、段落、粗体/斜体/删除线、行内代码与公式、链接、图片预览、有序/无序列表、引用、代码块(语法高亮 + 一键复制)、表格、脚注、HTML 以及多轮会话消息气泡等布局,内置触摸与 PC 鼠标惯性滚动、贴底跟随与橡皮筋回弹。

文档文本与代码高亮数据可由后端解析(如 markdown-server /render 返回的标准 ast)后整体传入 nodes,前端无需自行高亮。

t-markdown 平台兼容 ​

HarmonyH5AndroidiOS小程序UTS
✅✅✅✅✅✅

t-markdown 基础用法 ​

基础用法参考 demo/yoga/yoga.uvue。通过 nodes 传入 markdown 块级节点数组(顶层可为各类块节点或 message 消息容器),通过 options 定制渲染样式。静态报告场景建议将 followBottom 设为 false 固定停留在顶部查看。

vue
<template>
	<view class="p-15">
		<t-card class="mb-15"
				title="Markdown 富文本"
				sub-title="Canvas高性能渲染,支持表格代码块,会话消息气泡等复杂排版"></t-card>
		<view class="w-100% h-560px b-1px,s,#ebeef5 r-8 oh">
			<t-markdown class="w-100% h-100%"
						:options="{ pad: 15, followBottom: false }"
						:nodes="nodes"></t-markdown>
		</view>
	</view>
</template>
<script setup>
	const nodes = ref<UTSJSONObject[]>([
		{
			type: 'heading',
			level: 2,
			children: [
				{ type: 'text', content: 'Canvas Markdown 渲染引擎' }
			]
		},
		{
			type: 'paragraph',
			children: [
				{ type: 'text', content: '支持 ' },
				{ type: 'strong', children: [{ type: 'text', content: '加粗' }] },
				{ type: 'text', content: '、' },
				{ type: 'emphasis', children: [{ type: 'text', content: '斜体' }] },
				{ type: 'text', content: '、' },
				{ type: 'inlineCode', content: '行内代码' },
				{ type: 'text', content: ' 与' },
				{ type: 'link', href: 'https://uniapp.dcloud.net.cn', children: [{ type: 'text', content: '超链接' }] },
				{ type: 'text', content: ',图片宽高未知时会等比预加载测量。' }
			]
		},
		{
			type: 'blockquote',
			children: [
				{ type: 'paragraph', children: [{ type: 'text', content: '引用区块:块级排版 + 可视区虚拟化绘制,数据再多也不怕。' }] }
			]
		},
		{
			type: 'code',
			lang: 'ts',
			content: 'const md = new TMarkdown()\nmd.render(nodes)'
		},
		{
			type: 'list',
			ordered: true,
			children: [
				{ type: 'listItem', children: [{ type: 'paragraph', children: [{ type: 'text', content: '代码块语法高亮 + 一键复制' }] }] },
				{ type: 'listItem', children: [{ type: 'paragraph', children: [{ type: 'text', content: '支持无序/有序列表嵌套' }] }] }
			]
		}
	])
</script>

t-markdown 消息气泡会话 ​

参考 demo/markdown/markdown.uvue。将每一轮会话包成 { type: 'message', role, children } 顶层节点即可实现多轮聊天布局:user 消息走右侧深色气泡,assistant 消息走左侧透明正文,流式追加时自动重排并贴底跟随。

vue
<template>
	<view class="h-100%">
		<t-markdown class="f"
					:options="{ pad: 15 }"
					:nodes="nodes"
					@linktap="onLinkTap"></t-markdown>
	</view>
</template>
<script setup>
	// 会话队列:每轮渲染块数组
	const messages = ref<any[]>([])

	// 组装成 message 消息块序列
	const nodes = computed(() : any[] => {
		const out : any[] = []
		const list = messages.value || []
		for (const m of list) {
			out.push({ type: 'message', role: m.role, children: m.render || [] })
		}
		return out
	})

	function send(text : string) : void {
		const list = (messages.value || []).slice()
		list.push({
			role: 'user',
			render: [{ type: 'paragraph', children: [{ type: 'text', content: text }] }]
		})
		messages.value = list
	}

	function onLinkTap(e : any) : void {
		console.log('linktap', e.href, e.text)
	}
</script>

t-markdown Props ​

属性名说明类型默认值
nodesmarkdown 块级节点数组(必传)。顶层可为 message{role, children} 消息容器或各类块节点Array[]
options渲染配置对象,键名见下方 Options 表;均可省略,未配置时使用内置默认值Object{}

nodes 节点结构 ​

节点类型关键字段说明
messagerole: user/assistant,children多轮会话消息容器,user 右侧气泡、assistant 正文
headinglevel,children标题(1~6 级)
paragraphchildren段落
textcontent纯文本
strong / emphasis / deletechildren加粗 / 斜体 / 删除线
inlineCodecontent行内代码
linkhref,children行内链接,点击触发 linktap
imagesrc,width?,height?图片,宽高缺省时等比预加载测量
break-换行
codecontent,lang?,lines?代码块;lines 为逐行语法高亮 token,缺失时按纯文本绘制
blockquotechildren引用块
listordered,children有序/无序列表,列表项 listItem
tablealign,children表格
thematicBreak-分隔线
math / inlineMath-公式块 / 行内公式
footnoteReference / footnoteDefinition-脚注引用 / 脚注定义
html-HTML 内容

t-markdown Options ​

options 中可配置以下渲染键(均可省略,类型与默认值见下表):

内容与配色 ​

配置键说明类型默认值
pad内容留白number0
textColor正文色string#333333
linkColor链接色string#007acc
mathColor公式色string#555555
quoteBar引用条颜色string#dfe2e5
hr分隔线颜色string#eaecef
muted弱化文字色(如列表符号)string#888888
footnote脚注色string#999999
imgBg图片占位底色string#f0f1f3
check任务列表选中色string#2979ff
checkboxUn任务列表未选色string#b5bcc4

代码块 ​

配置键说明类型默认值
codeBg代码块底色string#1e1e1e
codeFg代码文字色string#e1e4e8
codeInlineBg行内代码底色string#f6f6f6
codeInlineColor行内代码文字色string#e83e8c
codeRadius代码块圆角number10
codeHeaderH代码顶栏高度number34
codeHeadGap顶栏左右留白number16
codeHead代码顶栏底色string#2b2b2b
codeLabel语言标签颜色string#9aa5b1
copyBtn复制按钮底色string#2979ff
copyText复制按钮文字色string#ffffff
dotA / dotB / dotC顶栏装饰圆点颜色string#ff5f57 / #febc2e / #28c840

表格 ​

配置键说明类型默认值
grid表格线颜色string#eaecef
thead表头底色string#f2f3f5
zebra斑马纹行底色string#f6f8fa
tableRadius表格外框圆角number8

消息气泡会话 ​

配置键说明类型默认值
userBubble用户消息气泡底色string#2979ff
bubbleTextColor用户气泡内文字颜色string#ffffff
bubbleTextOffset气泡文字垂直居中额外下移量number4
bubblePad气泡内边距number12
bubbleRadius气泡圆角number14
bubbleMaxRatio气泡最大宽度占内容区比例number0.76
bubbleGapY消息之间垂直间距number10

滚动行为 ​

配置键说明类型默认值
followBottom是否自动贴底跟随新内容;静态内容可传 false 停顶部booleantrue
releaseIdleMs留白静默释放延迟number700

t-markdown Events ​

事件名说明回调参数
linktap点击行内链接时触发{ href, text }