• 微信小程序富文本解析器rich-text、web-view、wxParse、mp-html、towxml对比


    微信小程序解析富文本html大概有几种方式,我用过的有这三种rich-text、web-view、wxParse、mp-html,各有各的优缺点,接下来聊一聊。

    一、rich-text

    二、web-view

    三、wxParse

    四、mp-html

    五、towxml

    一、rich-text

    rich-text富文本组件是小程序1.4.0版本后推出来的。

     官方给出的例子(本文做了精简):

    1. // index.wxml
    2. class="container">
    3. <rich-text nodes="{{htmlSnip}}">rich-text>
    4. <rich-text nodes="{{nodes}}">rich-text>
    5. // index.js
    6. Page({
    7. onShareAppMessage() {
    8. return {
    9. title: 'rich-text',
    10. path: 'page/component/pages/rich-text/rich-text'
    11. }
    12. },
    13. data: {
    14. htmlSnip: `
    15. Title

    16. Life is like a box of
    17.  chocolates.
  • `,
  • nodes: [{
  • name: 'div',
  • attrs: {
  • class: 'div_class',
  • style: 'line-height: 60px; color: #1AAD19;'
  • },
  • children: [{
  • type: 'text',
  • text: 'You never know what you\'re gonna get.'
  • }]
  • }]
  • },
  • })
  • 效果如下:

    缺点:
    1、只能解析html内容,需要做兼容处理

    二、web-view

    web-view是小程序1.6.4版本推出来的新组件。

    优点:
    1、可以直接显示网页内容,而且可以做 a 链接跳转。

    缺点:

    1、其实两个很多微信都低于1.6.4版本,不能使用,需要用前面介绍的两种方法做兼容处理。 

    三、wxParse

    wxParse是拥有7.7k stars已停止维护了的库。如何使用之前有写过相关介绍《微信小程序-后台使用富文本编辑器返回数据,小程序编译富文本编辑器返回的数据》

    优点:

    1、可以解析 html/markdown 两种脚本,功能很强大,

    2、可以解析audio

    缺点:

    1、在解析富文本过程中,多次调用小程序的setData()方法,对性能有一定影响

    2、可以解析audio,但是不能保持他原有的样式

    3、表格编译样式不能保持原有样式

    四、mp-html

    mp-html是拥有2.5k stars的库。查看更多介绍

    引入方式

    1、npm

    1. // 本方法仅适用于微信、QQ 小程序
    2. // 在小程序项目根目录下通过 npm 安装组件包
    3. // 开发者工具中勾选 使用 npm 模块(若没有此选项则不需要)并点击 工具 - 构建 npm
    4. // 在需要使用页面的 json 文件中添加
    5. {
    6. "usingComponents": {
    7. "mp-html": "mp-html"
    8. }
    9. }

    2、源码引入

    1. // 本方法适用于所有平台
    2. // 将 源码 中对应平台的代码包(dist/platform)拷贝到 components 目录下,更名为 mp-html
    3. // 在需要使用页面的 json 文件中添加
    4. {
    5. "usingComponents": {
    6. "mp-html": "/components/mp-html/index"
    7. }
    8. }

    使用

    1. // index.wxml
    2. "{{html}}" />
    3. // index.js
    4. Page({
    5. data: {
    6. html: '
      Hello World!
      '
    7. }
    8. })

    优点:

    1、能够支持多种 html 格式,具有强大的稳定性

    2、支持多种框架使用

    3、长内容可以分次分页渲染

    缺点:

    1、遇到某些特殊字符(=,&等)会中断解析,需要特殊处理

    五、towxml

    towxml是一个可将HTMLMarkdown转为微信小程序WXML(WeiXin Markup Language)的渲染库。用于解决在微信小程序中MarkdownHTML不能直接渲染的问题。

    使用

    1、放在小程序根目录下

    1. // 1.将构建出来的towxml并解压至小程序项目根目录下,即(小程序/towxml)
    2. // 2. 引入库/app.js
    3. //app.js
    4. App({
    5. // 引入`towxml3.0`解析方法
    6. towxml:require('/towxml/index')
    7. })
    8. // 3. 在页面配置文件中引入towxml组件 /pages/index/index.json
    9. // index.json
    10. {
    11. "usingComponents": {
    12. "towxml":"/towxml/towxml"
    13. }
    14. }
    15. // 4. 在页面中插入组件/pages/index/index.wxml
    16. // index.wxml
    17. class="container">
    18. <towxml nodes="{{article}}"/>
    19. // 5. 解析内容并使用/pages/index/index.js
    20. //获取应用实例
    21. const app = getApp();
    22. Page({
    23. data: {
    24. isLoading: true, // 判断是否尚在加载中
    25. article: {} // 内容数据
    26. },
    27. onLoad: function () {
    28. let result = app.towxml(`# Markdown`,'markdown',{
    29. base:'https://xxx.com', // 相对资源的base路径
    30. theme:'dark', // 主题,默认`light`
    31. events:{ // 为元素绑定的事件方法
    32. tap:(e)=>{
    33. console.log('tap',e);
    34. }
    35. }
    36. });
    37. // 更新解析数据
    38. this.setData({
    39. article:result,
    40. isLoading: false
    41. });
    42. }
    43. })

     2、放在components中

    1. // 1.将生成好的 towxml 放进components
    2. // 2.修改 components\towxml\decode.json 的路径(绝对路径都改为相对路径)
    3. {
    4. "component": true,
    5. "usingComponents": {
    6. "decode": "./decode",
    7. "audio-player": "./audio-player/audio-player",
    8. "table": "./table/table",
    9. "todogroup": "./todogroup/todogroup",
    10. "img": "./img/img"
    11. }
    12. }
    13. // 3.将在配置时引用到 towxml app.js
    14. //app.js
    15. App({
    16. // 引入`towxml3.0`解析方法
    17. towxml: require('/components/towxml/index'),
    18. // ...
    19. }
    20. // 4.所有引用到的页面/pages/aa/bb/xxx.json 亦或是全局配置的 app.json
    21. {
    22. // ...
    23. "usingComponents": {
    24. "towxml": "/components/towxml/towxml"
    25. }
    26. }
    27. 使用同放在根目录下的第45步骤

    注意:towxml的模板关联的数据参数,一定不能写在对象里。如果写在对象里(如下),在测试的时候没有任何问题,但是在真机测试的时候会导致数据加载及界面布局错乱!

    1. data:{
    2.        htmlObj:{
    3.               content:{}   //content将用来存储towxml数据
    4.             }
    5. }
    6. <import src="/towxml/entry.wxml"/>   
    7. <template is="entry" data="{{...htmlObj.content}}"/> 

    优点:

    1、可以解析一些废弃或者过新的标签

    2、对于界面的排版优化比较美观

    缺点:

    1、部分解析出来的代码片段没有换行

    2、在解析代码序号的时候生成ulli标签了,但在样式上没有做好处理

    上面4种方法可以在微信小程序中解析html富文本,大家可以根据自己的情况选择适合的方法。

    参考文件:

    rich-textweb-viewwxParsemp-htmltowxml

    微信小程序mp-html富文本解析的坑

    微信小程序富文本解析插件-towxml(扩展富文本图片预览功能)

    小程序富文本解析的「伪需求」,从wxParse到towxml的坑

    有不妥的地方,欢迎大家指出。

  • 相关阅读:
    【蓝桥杯集训100题】scratch售票找零 蓝桥杯scratch比赛专项预测编程题 集训模拟练习题第23题
    自学Python 35 闭包:函数和利用引用环境组合而成的实体
    S1_servlet与数据库连接、filter过滤器、分页 实操的叙述
    Docker的Tomcat启动报错HTTP状态404-未找到“源服务器未能找到目标资源的表示或者是不愿公开一个已经存在的
    Openharmony轻量系统实验--GPIO点灯
    Bean 管理(工厂bean)
    CVE-2023-5129:libwebp开源库10分漏洞
    三、贪心算法
    【数智化人物展】同方有云联合创始人兼总经理江琦:云计算,引领数智化升级的动能...
    Express 3 快速入门 - Express 应用程序生成器
  • 原文地址:https://blog.csdn.net/ly2983068126/article/details/127422096