Overview of mp-html
masterrich-text component is often limited in tag support and event handling. mp-html solves this by providing a lightweight, high-performance component that supports a wide range of HTML tags and interactive features.repository·master·Indexed 26 days ago
https://github.com/jin-yufeng/mp-htmlA lightweight rich text component for mini-program platforms and uni-app. It supports a wide range of HTML tags (including tables, video, and SVG), event handling, and is extensible via plugins for features such as Markdown, LaTeX, rich text editing, syntax highlighting via PrismJS, emoji parsing, and local image caching.
rich-text component is often limited in tag support and event handling. mp-html solves this by providing a lightweight, high-performance component that supports a wide range of HTML tags and interactive features.mp-html provides the following capabilities:
uni-app.table, video, svg, and more.latex formulas.When performing secondary development, make all modifications within the src directory. The project uses a single source code base (primarily following WeChat Mini Program syntax) that is automatically converted to other platforms during the build process.
Important Note on Property Access:
To ensure compatibility across platforms (specifically for Alipay), always access component properties via this.properties instead of this.data.
Because nvue uses native rendering, mp-html renders via web-view to achieve similar effects to HTML. This has performance implications and specific limitations:
Limitations:
lazy-load property is not supported.flex-direction: row, you must set a width for the component or set flex: 1 to fill the remaining width.Note on Installation: Prior to certain fixes, nvue mode did not support uni_modules and required local installation (copying dist/uni-app to your project root).
To use mp-html in a uni-app project via the uni_modules method:
使用 HBuilder X 导入插件 (Import plugin using HBuilder X) button in the plugin market to import directly, or download the ZIP and extract it to your project's uni_modules/mp-html directory.(n)vue files without manual imports:<!-- No import required, can be used directly -->
<mp-html :content="html" />export default {
data() {
return {
html: '<div>Hello World!</div>'
}
}
}uni_modules/mp-html directory in HBuilderX and select 从插件市场更新 (Update from plugin market).<mp-html :content="html" />The component handles tables using three different rendering methods depending on complexity:
rich-text: Best performance for tables without links or images.table layout: Used for tables with special tags but no merged cells.grid layout: Used for complex tables with merged cells.Key Features:
scroll-table to allow tables to scroll horizontally independently of the main content.border, cellspacing, cellpadding, and align.Copy the contents of dist/uni-app from the source into your project root.
<template>
<view>
<mp-html :content="html" />
</view>
</template>
<script>
import mpHtml from '@/components/mp-html/mp-html'
export default {
// HBuilderX 2.5.5+ can use easycom for automatic import
components: {
mpHtml
},
data () {
return {
html: '<div>Hello World!</div>'
}
}
}
</script>The latex plugin enables the rendering of LaTeX math formulas within mp-html. Once the plugin is included, any text wrapped in single dollar signs (e.g., $xxx$) will be parsed and rendered according to LaTeX rules.
Key Behaviors:
editable plugin, formulas will not render while in editing mode, allowing users to directly modify the raw LaTeX text.katex-mini for parsing.Apply styles to the rendered HTML using three priority levels:
style attribute directly on HTML tags.tag-style prop to set default styles for specific tag names.externStyle field in tools/config.js. This supports class selectors (and tag name selectors since v2.1.0).Note: You can use !important to override lower-priority styles. Additionally, the style plugin can be used to match styles defined within <style> tags in the HTML content.
The default package does not include extended features like editing or LaTeX to keep the size small. To use these, you must build a custom package.
Available Plugins:
audio: Music playereditable: Rich text editingemoji: Emoji parsinghighlight: Code block highlightingmarkdown: Markdown renderingsearch: Keyword searchstyle: Matches styles in style tagstxv-video: Uses Tencent Videoimg-cache: Image cachinglatex: LaTeX formula renderingHow to build a custom package with plugins:
npm install mp-htmlplugins field in tools/config.js to select the required plugins.node_modules/mp-html directory:npm install
npm run build:uni-appdist/uni-app to your project root.npm install
npm run build:uni-appYou can install the mp-html package using npm or yarn to manage it as a dependency in your project.
# 通过 npm 获取
npm install mp-html
# 或通过 yarn 获取
yarn add mp-htmlYou can use the native mini-program packages in Taro.
Important Notes:
copy-link becomes copyLink) or use the setContent method to set content.