微信小程序富文本解析器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版本后推出来的。

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

// index.wxml
<view class="container">
    <rich-text nodes="{{htmlSnip}}"></rich-text>
    <rich-text nodes="{{nodes}}"></rich-text>
</view>

// index.js
Page({
  onShareAppMessage() {
    return {
      title: 'rich-text',
      path: 'page/component/pages/rich-text/rich-text'
    }
  },

  data: {
    htmlSnip: `<div class="div_class">
      <h1>Title</h1>
      <p class="p">
        Life is&nbsp;<i>like</i>&nbsp;a box of
    <b>&nbsp;chocolates</b>.
      </p>
    </div>
    `,
    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

// 本方法仅适用于微信、QQ 小程序

// 在小程序项目根目录下通过 npm 安装组件包
// 开发者工具中勾选 使用 npm 模块(若没有此选项则不需要)并点击 工具 - 构建 npm
// 在需要使用页面的 json 文件中添加

{
  "usingComponents": {
    "mp-html": "mp-html"
  }
}

2、源码引入

// 本方法适用于所有平台

// 将 源码 中对应平台的代码包(dist/platform)拷贝到 components 目录下,更名为 mp-html
// 在需要使用页面的 json 文件中添加

{
  "usingComponents": {
    "mp-html": "/components/mp-html/index"
  }
}

使用

// index.wxml
<mp-html content="{{html}}" />

// index.js
Page({
  data: {
      html: '<div>Hello World!</div>'
  }
})

优点:

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

2、支持多种框架使用

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

缺点:

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

五、towxml

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

使用

1、放在小程序根目录下

// 1.将构建出来的towxml并解压至小程序项目根目录下,即(小程序/towxml)

// 2. 引入库/app.js

//app.js
App({
	// 引入`towxml3.0`解析方法
	towxml:require('/towxml/index')
})

// 3. 在页面配置文件中引入towxml组件 /pages/index/index.json

// index.json
{
  "usingComponents": {
    "towxml":"/towxml/towxml"
  }
}

// 4. 在页面中插入组件/pages/index/index.wxml

// index.wxml
<view class="container">
  <towxml nodes="{{article}}"/>
</view>

// 5. 解析内容并使用/pages/index/index.js

//获取应用实例
const app = getApp();
Page({
	data: {
		isLoading: true,					// 判断是否尚在加载中
		article: {}						// 内容数据
	},
	onLoad: function () {
		let result = app.towxml(`# Markdown`,'markdown',{
			base:'https://xxx.com',				// 相对资源的base路径
			theme:'dark',					// 主题,默认`light`
			events:{					// 为元素绑定的事件方法
				tap:(e)=>{
					console.log('tap',e);
				}
			}
		});

		// 更新解析数据
		this.setData({
			article:result,
			isLoading: false
		});
		
	}
})

 2、放在components中

// 1.将生成好的 towxml 放进components 

// 2.修改 components\towxml\decode.json 的路径(绝对路径都改为相对路径)

{
  "component": true,
  "usingComponents": {
    "decode": "./decode",
    "audio-player": "./audio-player/audio-player",
    "table": "./table/table",
    "todogroup": "./todogroup/todogroup",
    "img": "./img/img"
  }
}

// 3.将在配置时引用到 towxml app.js

//app.js

App({
	// 引入`towxml3.0`解析方法
  towxml: require('/components/towxml/index'),
  // ...
}

// 4.所有引用到的页面/pages/aa/bb/xxx.json 亦或是全局配置的 app.json

{
  // ...
  "usingComponents": {
    "towxml": "/components/towxml/towxml"
  }
}

使用同放在根目录下的第4、5步骤

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

data:{
       htmlObj:{
              content:{}   //content将用来存储towxml数据
            }
}

<import src="/towxml/entry.wxml"/>   
<template is="entry" data="{{...htmlObj.content}}"/> 

优点:

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

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

缺点:

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

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

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

参考文件:

rich-textweb-viewwxParsemp-htmltowxml

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

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

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

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

  • 3
    点赞
  • 24
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值