文章目录

小程序 - 代码高亮 - towxml

一个可将HTML、Markdown转为微信小程序WXML(WeiXin Markup Language)的渲染库


app.js

const Towxml = require('/towxml/main'); //引入towxml库
App({
  towxml: new Towxml(), //创建towxml对象
})

test.wxml

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

test.js

var Api = require('../../utils/api.js');
import config from '../../utils/config.js';
const app = getApp();
Page({
  data: {
    cid: 0,
    commentsNum: 0,
    created: 0,
    viewsNum: 0,
    title: 'title',
    text: 'content'
  },
  onLoad: function (options) {
    var that = this;
    var cid = options.cid;
    that.setData({
      cid: cid
    });
    that.getContent(cid);
  },
  getContent(cid) {
    var that = this;
    var url = config.getApiurl;
    wx.request({
      url: url + '/index/detail',
      data: {
        cid: cid
      },
      header: {
        'Content-Type': 'application/x-www-form-urlencoded'
      },
      success: function (res) {
        var data = res.data[0];
        var commentsNum = data.commentsNum;
        var created = data.created;
        var title = data.title;
        var viewsNum = data.viewsNum;
        var text = data.text;
        //将markdown内容转换为towxml数据
        text = app.towxml.toJson(text, 'markdown');
        //设置文档显示主题,默认'light'
        text.theme = 'light';
        that.setData({
          commentsNum: commentsNum,
          created: created,
          viewsNum: viewsNum,
          title: title,
          text: text
        });
      }
    });
  }
});

github

返回文章列表 打赏
本页链接的二维码
打赏二维码
期待你的评论