更新记录

0.1.0(2026-05-09) 下载此版本

初始版本:

  • 支持 Vue2 / Vue3、H5/Web、微信小程序
  • 支持 text 与 segments 渲染 AI 答案、解析和 LaTeX 公式
  • 内置 mp-html 与 latex 渲染能力

平台兼容性

uni-app(3.7.7)

Vue2 Vue3 Chrome Safari app-vue app-nvue Android iOS 鸿蒙
80+ 13+ - - - - -
微信小程序 支付宝小程序 抖音小程序 百度小程序 快手小程序 京东小程序 鸿蒙元服务 QQ小程序 飞书小程序 小红书小程序 快应用-华为 快应用-联盟
2.10.4+ - - - - - - - - - - -

AI LaTeX 富文本渲染

hyc-latex-render 是一个面向 AI 答案/解析场景的 uni-app 富文本公式组件。它基于 mp-html 和 latex 插件封装,支持 Vue2、Vue3、H5/Web 和微信小程序。

安装

把插件目录复制到项目:

uni_modules/hyc-latex-render

发布到 DCloud 插件市场前,请按你的账号信息调整 package.json 里的 idrepositorycontact 等字段。

如果项目开启 easycom,可以直接在页面使用:

<hyc-latex-render :segments="aiExplanationSegments" variant="explanation" />

必须引入公式样式

在业务项目 App.vue 的全局样式中引入:

@import "@/uni_modules/hyc-latex-render/styles/katex.css";

特别是微信小程序端,公式字体、上下标、分式和根号都依赖这份全局样式。

基础用法

<template>
  <view class="page">
    <hyc-latex-render
      :text="answer"
      variant="answer"
    />

    <hyc-latex-render
      :segments="explanationSegments"
      variant="explanation"
    />
  </view>
</template>

<script>
export default {
  data() {
    return {
      answer: '(1) $\\frac{q}{m}=\\frac{2v_0^2}{Eh}$',
      explanationSegments: [
        { type: 'text', text: '粒子做类平抛运动,运动时间 ' },
        { type: 'formula', latex: 't=\\frac{h}{v_0}', display: false },
        { type: 'text', text: '。' }
      ]
    }
  }
}
</script>

如果你直接拿的是错题详情接口对象,也可以让组件自己按字段读取:

<hyc-latex-render
  :mistake="mistake"
  field="answer"
  variant="answer"
/>

<hyc-latex-render
  :mistake="mistake"
  field="explanation"
  variant="explanation"
/>

字段映射:

field 读取 segments 读取文本
answer aiAnswerSegments aiAnswer
explanation aiExplanationSegments aiExplanation
teacherExplanation / teacher teacherExplanationSegments teacherExplanation
remark remarkSegments remark

支持的输入

1. 普通文本

text 中可以直接写 $...$$$...$$\(...\)\[...\]

<hyc-latex-render text="由 $h=v_0t$ 得 $t=\\frac{h}{v_0}$。" />

2. 后端分段协议

推荐后端返回 segments,这样比从字符串里猜公式更稳定:

[
  { type: 'text', text: '答案为 ' },
  {
    type: 'formula',
    latex: '\\frac{q}{m}=\\frac{2v_0^2}{Eh}',
    display: false
  }
]

字段说明:

字段 类型 说明
type text / formula / math 文本或公式
text String 文本内容;公式缺少 latex 时也可兜底使用
latex String LaTeX 公式内容,不需要带 $
display Boolean true 表示块级公式,false 表示行内公式

Props

属性 类型 默认值 说明
text String '' 普通文本,支持 $...$$$...$$
mistake Object null 后端错题详情对象,可配合 field 使用
field String '' mistake 中读取的字段:answer / explanation / teacherExplanation / remark
preferSegments Boolean/String true 有 segments 时优先使用 segments,避免 raw text 中 $ 误判
segments Array [] AI 分段内容,优先级高于 text
placeholder String 暂无内容 空内容占位
variant String body body / answer / explanation,控制默认字号字重
selectable Boolean/String force 是否支持长按选择复制
copyLink Boolean/String false 外链点击复制
setTitle Boolean/String false 是否把 title 设置为页面标题
showImgMenu Boolean/String false 图片长按菜单
containerStyle String width:100%;overflow-x:auto; 富文本容器样式
tagStyle Object {} 透传给 mp-html 的标签样式
textColor String '' 覆盖默认文字颜色
fontSize String '' 覆盖默认字号
fontWeight String '' 覆盖默认字重
lineHeight String/Number '' 覆盖默认行高

Events

事件 说明
load DOM 结构加载完成
ready 图片等资源加载完成
imgtap 图片点击
linktap 链接点击
error 媒体加载错误

方法

this.$refs.richText.getText()

返回渲染后的纯文本内容。

兼容说明

  • Vue2 / Vue3:组件使用 Options API,避免 Vue2 项目无法编译 script setup
  • H5 / Web:使用 mp-html 递归渲染,长公式会跟随容器横向滚动。
  • 微信小程序:组件内部设置了 styleIsolation: 'shared'addGlobalClass: true,同时需要在 App.vue 全局引入 styles/katex.css
  • App Vue:理论可用,但首版没有把 App 端作为承诺平台。
  • nvue:暂不支持。

注意事项

  • 这是 AI 富文本公式渲染组件,不是通用 HTML 安全过滤器。不要直接渲染未经过后端信任处理的用户 HTML。
  • latex 渲染来自 mp-html 的 latex 插件和 katex-mini,不是完整 MathJax,复杂公式支持范围以 katex-mini 为准。
  • 如果微信小程序端字体文件加载异常,可以把 CSS 中的 KaTeX 字体资源转存到自己的 CDN,并加入小程序合法域名。

隐私、权限声明

1. 本插件需要申请的系统权限列表:

2. 本插件采集的数据、发送的服务器地址、以及数据用途说明:

插件不采集任何数据。

3. 本插件是否包含广告,如包含需详细说明广告表达方式、展示频率:

许可协议

MIT License

Copyright (c) 2026

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

暂无用户评论。