Home  >  Article  >  WeChat Applet  >  How to configure Twoxml in the mini program so that it can perfectly support Markdown!

How to configure Twoxml in the mini program so that it can perfectly support Markdown!

青灯夜游
青灯夜游forward
2021-10-14 11:11:082180browse

This article will share with you a detailed tutorial on how to make the applet perfectly support Markdown. I hope it will be helpful to everyone!

How to configure Twoxml in the mini program so that it can perfectly support Markdown!

Recently I am working on a function that needs to display article details. I plan to use Markdown to display the details. I found that the WeChat applet is not very friendly in supporting Markdown. I have no intention of doing so. I found a useful component, Twoxml, which perfectly supports Markdown. Let’s take you step by step to implement the Markdown function. [Related learning recommendations: 小program development tutorial]

Towxml introduction

|Towxml official website: https://github.com /sbfkcel/towxml

Towxml is a rendering library that can convert HTML and Markdown into WeChat applet WXML. It supports the following functions:

How to configure Twoxml in the mini program so that it can perfectly support Markdown!

Using Towxml can Achieve the following Markdown effect

How to configure Twoxml in the mini program so that it can perfectly support Markdown!

Introduce Twoxml into the mini program

##Build Twoxml

    Clone the project to local
  • git clone https://github.com/sbfkcel/towxml.git
  • If you have not installed npm dependencies, install them first

    npm install 或 yarn

  • Edit the configuration file towxml/config.js

    Just keep the functions you need according to your actual needs

  • Run

    npm run build or yarn run build then

    The built file will be in the dist directory, and copy the dist directory to the root directory of the mini program project and change the directory name to towxml to use

How to configure Twoxml in the mini program so that it can perfectly support Markdown!

##Use Twoxml in the mini program

In the previous step, we have introduced the towxml folder into the mini program:

How to configure Twoxml in the mini program so that it can perfectly support Markdown!

1. Import library/app.js

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

2. Introduce the twoxml component into the configuration file of the specific page

// pages/content-detail/content-detail.json
{
  "usingComponents": {
    "towxml":"/towxml/towxml"
  }
}

3. In the page Insert components into

// pages/content-detail/content-detail.wxml
<view class="content-info">
  <towxml nodes="{{article}}"/>
</view>

4. Parse content in jsThe method of parsing content is given here There are two versions, one is the plus worry-free version and the other is the basic version. Let me talk about the plus version first

plus worry-free version

Normally speaking , the markdown source data should be obtained from the server, then we first encapsulate a request method (I encapsulate it in App.js)

App({
  // 引入`towxml3.0`解析方法
  towxml:require(&#39;/towxml/index&#39;),
    //声明一个数据请求方法
  getText: (url, callback) => {
    wx.request({
      url: url,
      header: {
        &#39;content-type&#39;: &#39;application/x-www-form-urlencoded&#39;
      },
      success: (res) => {
        if (typeof callback === &#39;function&#39;) {
          callback(res);
        };
      }
    });
  }
  })

Then process the parsed content in the js file of the specific page

// pages/content-detail/content-detail.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
   article:{}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    app.getText(&#39;https://www.vvadd.com/wxml_demo/demo.txt?v=2&#39;,res => {
      let obj = app.towxml(res.data,&#39;markdown&#39;,{
        theme:&#39;light&#39;, //主题 dark 黑色,light白色,不填默认是light
        base:"www.xxx.com",
        events:{      //为元素绑定的事件方法
          tap:e => {
            console.log(&#39;tap&#39;,e);
          },
          change:e => {
            console.log(&#39;todo&#39;,e);
          }
        }
      });
      //更新解析数据
      this.setData({
        article:obj,
      });
    });
  },
})

Here I am requesting a URL

www.vvadd.com/wxml_demo/d…

. This URL will return me markdown source data. Let’s first take a look at what is in this request address.

How to configure Twoxml in the mini program so that it can perfectly support Markdown!After we obtain the markdown data, assign it a value, and then display it directly on the page. The exciting time has arrived, let’s take a look at the final effect

How to configure Twoxml in the mini program so that it can perfectly support Markdown!Is the effect perfect? ​​Markdown display is perfectly realized.

Basic version

After talking about the plus version, let’s talk about the basics. version, because you may have custom processing operations on markdown data sources, so let’s take a look at the implementation of the basic version

// pages/content-detail/content-detail.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
   article:{}
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    //markdown数据源
      let content= "<h1>h1h1h1h1h1h1</h1><h2>h2h2h2h2</h2><h3>123456</h3>"
    let result = app.towxml(content,&#39;markdown&#39;,{
       base:&#39;www.xxx.com&#39;,             // 相对资源的base路径
       theme:&#39;light&#39;,                   // 主题,默认`light`
      events:{                    // 为元素绑定的事件方法
           tap:(e)=>{
               console.log(&#39;h4&#39;,e);
           }
       }
   });
   // 更新解析数据
   this.setData({
      article:result
   });
  },
})

The basic version is finished, and it is very simple. In fact, it is different from the plus version. It’s not big either. The plus version just encapsulates the data request. Let’s take a look at the effect

How to configure Twoxml in the mini program so that it can perfectly support Markdown!

Summary

Using Towxml to implement markdown is actually relatively simple. It not only supports rich markdown syntax, but also supports charts, flow charts, and mathematical formulas. In real development, the markdown data source is obtained from the server. It is recommended that the server Parse the markdown data source and assign the value directly after obtaining it on the front end to avoid performance problems

For more programming-related knowledge, please visit:

Programming Video

! !

The above is the detailed content of How to configure Twoxml in the mini program so that it can perfectly support Markdown!. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:juejin.cn. If there is any infringement, please contact admin@php.cn delete