Towxml 是一个可将HTML
、Markdown
转为微信小程序WXML
(WeiXin Markup Language)的渲染库。
用于解决在微信小程序中Markdown
、HTML
不能直接渲染的问题。
特色
- 支持代码语法高亮
- 支持emoji表情:wink:
- 支持上标、下标、下划线、删除线、表格、视频、图片(几乎所有html元素)……
- 支持typographer字符替换
- 多主题动态支持
- 极致的中文排版优化
- Markdown TodoList
- 支持事件绑定(这样允许自行扩展功能哟,例如:点击页面中的某个元素,更新当前页面内容等…)
- 前后端支持
快速上手
1. 克隆TOWXML到小程序根目录
1
| git clone https://github.com/sbfkcel/towxml.git
|
2. 在小程序app.js
中引入库
1 2 3 4 5 6 7
| const Towxml = require('/towxml/main'); App({ onLaunch: function () { }, towxml:new Towxml() })
|
3. 在小程序页面文件中引入模版
1 2 3 4
|
<import src="/towxml/entry.wxml"/> <template is="entry" data="{{...article}}"/>
|
4. 在小程序对应的js中请求数据
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
|
const app = getApp(); Page({ data: { article:{} }, onLoad: function () { const _ts = this;
wx.request({ url: 'http://xxx/doc.md', header: { 'content-type': 'application/x-www-form-urlencoded' }, success: (res) => { let data = app.towxml.toJson(res.data,'markdown');
data.theme = 'dark';
_ts.setData({ article: data }); } }); } })
|
5. 引入对应的WXSS
1 2 3 4 5 6 7 8 9 10 11 12 13
|
@import '/towxml/style/main.wxss';
@import '/towxml/style/theme/light.wxss';
@import '/towxml/style/theme/dark.wxss';
|
OK,大功告成~~
事件绑定
towxml
支持以下事件绑定,可自行根据需要为内容添加绑定事件。(不支持bindtap
等事件简写方法)
1 2 3 4 5 6 7 8 9 10 11 12
| 'bind:touchstart', 'bind:touchmove', 'bind:touchcancel', 'bind:touchend', 'bind:tap', 'bind:longpress', 'bind:longtap', 'bind:transitionend', 'bind:animationstart', 'bind:animationiteration', 'bind:animationend', 'bind:touchforcechange'
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30
| Page({ data: { isloading: true, article: {} }, onLoad: function () { const _ts = this;
let articleData = app.towxml.toJson('<div name="button" id="button1">测试一个可点击的元素</div>', 'html', _ts);
this['event_bind_touchstart'] = (event)=>{ console.log(event.target.dataset._el); };
this['eventRun_todo_checkboxChange'] = (event)=>{ console.log(event.detail); };
_ts.setData({ article: articleData, isloading: false }); } })
|
在WePY框架中使用towxml
此处以wepy 1.7.3
为例,其它版本请参考
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| wepy init standard myproject
cd myproject
npm install
npm install towxml --save
cd src
git clone https://github.com/sbfkcel/towxml.git
wepy build --watch
|
API
如果为了追求极致的体验,建议将markdown
、html
转换为towxml数据的过程放在服务器上,在小程序中直接请求数据即可。
1. 依赖环境
需要 Node.js
环境。(已经安装请忽略)
2. 安装towxml
3. 接口使用
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| const Towxml = require('towxml'); const towxml = new Towxml();
let wxml = towxml.md2wxml('# Article title');
let wxml = towxml.html2wxml('<h1>Article title</h1>');
let data = towxml.toJson('# Article title','markdown');
let data = towxml.toJson('# Article title');
|
Demo示例
- 将
towxml/demo
添加为小程序工程
- 再克隆
towxml
到demo
目录
- 使用小程序开发工具编译即可
更新说明
2.0.1
2.0.0
1.5.12
1.5.11
- 调整事件绑定策略,剔除
capture-bind
绑定方式
- 调整自定义事件时获取元素属性的方式
event.target.dataset._el
元素的所有属性
- 剔除
data-url
、data-src
、data-alpha
、data-id
、data-name
的支持(因为元素数据传递方式更全,模版代码更少)
1.5.7
1.5.6
License
MIT
Gitalk 加载中 ...