# wx-mina-html-view
微信小程序渲染html
<img src="demo.png" width="320">
## 支持的特性:
* 大部分的普通HTML标签(未全部测试)
* `<img>`, `<video>`, `<audio>`
* `<table>`, `<tr>`, `<th>`, `<td>`等表格标签
* `<a>`标签转`<navigator>`
* HTML entities
* 七牛图片裁剪
* 自定义插件
不支持`<form>`, `<input>`, `<select>`等表单元素.
## 使用方法
请参考`pages/index`目录里的文件.
### 拷贝`html-view`目录到你的项目.
### 在页面的js/wxml/wxss中, 引入代码:
#### index.js
```js
// 引入HtmlParser
const HtmlParser = require('../../html-view/index')
// ...
// 解析HTML字符串
const html = new HtmlParser('<p>hello world</p>').nodes
this.setData({ html })
```
#### index.wxml
```html
<import src="../../html-view/index.wxml" />
<template is="html-view" data="{{data: html}}" />
```
#### index.wxss
```css
@import "../../html-view/index.wxss";
```
## API
### new HtmlParser(htmlString, { baseUrl })
新建一个HtmlParser实例.
实例化后通过`nodes`属性获取解析后的对象. 数据结构请参看[himalaya](https://github.com/andrejewski/himalaya).
baseUrl: 将`<a>`, `<img>`, `<video>`, `<audio>`, `<source>`引用的相对地址使用`baseUrl`转换为绝对地址.
```js
const html = new HtmlParser('<p>hello world</p>').nodes
```
### htmlParser.each(fn)
递归`nodes`和以下的的每个元素使用`fn`函数处理
`fn`将收到3个参数:
```js
fn(node, index, array)
```
### htmlParser.filter(fn)
递归`nodes`和以下的的每个元素使用`fn`函数过滤. 如果`fn`返回`true`, 则保留该元素, 否则删除该元素. `fn`收到的参数同`htmlParser.each()`
### htmlParser.map(fn)
递归`nodes`和以下的的每个元素使用`fn`函数处理. `fn`需要返回一个新的元素结构, `fn`收到的参数同`htmlParser.each()`
## 内置处理工具
### 图片URL加上七牛imageView2处理
qiniuImg(domain, quality)
`<img>`地址的域名如果包含`domain`并且没有query string, 则在后面加上imageView2参数: `?imageView2/2/w/WIDTH/q/QUALITY`.
WIDTH为手机实际像素宽度 (pixelRatio * windowWidth).
QUALITY为传入的`quality`, 如果不传`quality`, 这部分则省略.
```js
const qiniuImg = require('../../html-view/each/qiniuImg')
const html = new HtmlParser(htmlString).each(qiniuImg('qnssl.com')).nodes
```
### `<a>`标签转`<navigator>`
resolveAnchor(domain, routes)
domain: `<a>`指向的URL必须是该domain, 才会执行转换
routes: 将URL的pathname使用routes匹配, 找到匹配的路由则执行转换.
routes的定义请前往[Router](https://github.com/fenivana/Router).
`route.params`和`route.options`会被合并到URL的query string中, 合并/覆盖规则: `Object.assign(route.query, route.params, route.options)`
```js
const resolveAnchor = require('../../html-view/each/resolveAnchor')
const html = new HtmlParser(htmlString).each(resolveAnchor('www.example.com', [
// ['/pathname/of/url', '/pages/path/of/local/page/index']
['/foo', '/pages/foo/index'],
['/bar/:id', '/pages/bar/index']
])).nodes
```
## 开发
由于小程序的template不支持递归调用(哈哈什么鬼~), 修改wxml模板请修改`build/template.wxml`文件, 然后执行`npm run build-template`生成`html-view/nodes.wxml`文件
## License
[MIT](LICENSE)
没有合适的资源?快使用搜索试试~ 我知道了~
微信小程序渲染html.zip
共33个文件
js:14个
wxss:6个
wxml:6个
需积分: 2 0 下载量 197 浏览量
2024-01-11
21:35:24
上传
评论
收藏 847KB ZIP 举报
温馨提示
小程序,轻量级设计,无需下载安装,即开即用,释放手机内存压力,轻松应对日常各种需求。无论是购物支付、信息查询,还是休闲娱乐、生活服务,我们都一应俱全,满足您的多元化需求。
资源推荐
资源详情
资源评论
收起资源包目录
微信小程序渲染html.zip (33个子文件)
open_weixxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxcxxxxxxxxxxxxcxvcvcv
pages
foo
index.wxml 55B
index.js 126B
index.wxss 90B
index
index.wxml 95B
index.js 4KB
index.wxss 322B
bar
index.wxml 55B
index.js 126B
index.wxss 90B
app.json 240B
app.js 8B
html-view
reset.wxss 3KB
index.wxml 167B
nodes.wxml 13KB
vendors
url.js 5KB
Router.js 6KB
himalaya.js 8KB
html5-entities.js 48KB
each
qiniuImg.js 419B
resolveAnchor.js 561B
resolveUrl.js 371B
default.js 2KB
index.js 1KB
index.wxss 255B
LICENSE 1KB
app.wxss 0B
package.json 970B
build
template.wxml 1KB
build-template.js 508B
.gitignore 13B
.eslintignore 19B
demo.png 815KB
README.md 3KB
共 33 条
- 1
资源评论
极致人生-010
- 粉丝: 3414
- 资源: 3073
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功