Appearance
markdown / hast — 渲染管线与遍历
基于 unified 生态(remark + rehype)的 markdown → HAST 处理,以及框架无关的 HAST 树遍历。
markdown — Markdown 渲染管线
LaTeX 预处理:\[...\] / \(...\) → $$...$$ / $...$(remark-math 标准格式)。代码块中的 $ 先用占位符 _ELX_DOLLAR_ 保护,处理完恢复,防止被误处理。
插件组装(usePlugins 按顺序):
rehype: rehypePluginsAhead → rehypeRaw(allowHtml) → rehypeKatex(enableLatex)
→ rehypeAnimatedPlugin(enableAnimate) → rehypePlugins
remark: remarkPluginsAhead → remarkMath(enableLatex) → remarkGfm(enableGfm)
→ remarkBreaks(enableBreaks) → remarkPluginstokenStyle:Shiki token → Vue CSSProperties,字体样式位运算解析(&1=italic / &2=bold / &4=underline / &8=strikethrough)。
hast — HAST 树遍历
框架无关的 HAST 遍历与节点信息计算,渲染层(Vue / React / 小程序)复用。
| 导出 | 说明 |
|---|---|
walkHast(root, handlers) | 自顶向下遍历,handlers: createNode / resolveSlot / renderChildren |
getNodeInfos(node, parent, context, keyCounter, customAttrs) | 单节点属性 / 别名 / slot props |
computeAttrs(node, aliasList, vnodeProps, attrs, customAttrs) | 合并自定义属性 |
getNodeInfos 为元素生成渲染信息:
| 元素 | vnodeProps |
|---|---|
h1~h6 | level: number |
code | language / content / inline |
ul/ol | ordered / depth |
li | ordered / depth / index |
td/th/tr | isHead: boolean |
别名解析从具体到通用(h1 → ['h1', 'heading']),供渲染层 slot 命中判断。
与渲染层的关系
@flowporr/x-markdown-vue的MarkdownRenderer是markdown模块的 Vue 响应式壳(usePlugins → useMarkdownProcessor → render)- React 渲染层可复用同一
markdown管线 +walkHast,只换组件壳
自定义处理器
ts
import { createProcessor, render } from '@flowporr/x-markdown-vue' // 渲染层包装
// 或直接用 ai-core 的管线函数
import { /* pipeline 函数 */ } from '@flowporr/ai-core'