新聞類的小程序,難免要使用富文本編輯器,而富文本編輯器內(nèi)容,是帶有html標簽的,那么問題來了,小程序并不支持html標簽,所以我們需要將帶有html標簽的內(nèi)容,轉(zhuǎn)換成小程序支付的標簽。
目前流行的,有兩種方法:
優(yōu)勢:代碼少,執(zhí)行效率高
缺點:只是用正則去掉識別不了的html標簽,原內(nèi)容的樣式全部丟失
使用步驟:
Step1: 在app.js 添加方法
Step2: 在頁面的js文件里,獲取app ,并在需要的位置調(diào)用方法
轉(zhuǎn)換結(jié)果:
圖中代碼可見,樣式及標簽都沒有了。
所以此方法,只適用于簡單的去除HTML標簽,下期我會帶來更為完美的解決方案。
(需要代碼的同學請留言)
法一,請查看上一篇文章。
優(yōu)勢:解析能力強,樣式得以保留
缺點:多次調(diào)用小程序setData方法,效率低
Step1: 準備文件
1、下載
2、解壓縮后目錄:
3、將wxPares文件夾,拷到小程序項目的根目錄:
Step2: 配置文件
1、將wxParse.wxml配置到需要展示內(nèi)容的頁面里
2、將wxParse.wxss配置到全局的css里
3、將wxParse.js配置到頁面的js里
并保存到變量里,方便后續(xù)調(diào)用。
Step3: 調(diào)用wxParse進行轉(zhuǎn)換
參數(shù)說明:
wxParse.wxParse(bindName , type, data, target,imagePadding)
1)bindName綁定的數(shù)據(jù)名(必填)
2)type可以為html或者md(必填)
3)data為傳入的具體數(shù)據(jù)(必填)
4)target為Page對象,一般為this(必填)
5)imagePadding為圖片自適應(yīng)padding(默認為0,可選),默認0就好
注:wxParse會根據(jù)你添加的bindName名,動態(tài)的生成data數(shù)據(jù)項,將轉(zhuǎn)換好數(shù)據(jù)存放到此bindName數(shù)據(jù)里(下圖)
Step4: 將轉(zhuǎn)換好的數(shù)據(jù),應(yīng)用到頁面里
查看頁面中圖片與食品是否解析正確,同時樣式是否丟失,無問題,則完成。
?
今天為大家?guī)淼氖且粋€用于在微信小程序中渲染html和Markdown的富文本組件,而且支持代碼高亮,它就是html2wxml!
https://github.com/qwqoffice/html2wxml
1、打開小程序管理后臺,轉(zhuǎn)到設(shè)置 - 第三方服務(wù),點擊添加插件
2、搜索 html2wxml ,選中并添加
3、添加成功
4、回到小程序開發(fā)環(huán)境,編輯 app.json ,添加插件聲明,最新版為 1.3.0
"plugins": {
"htmltowxml": {
"version": "1.3.0",
"provider": "wxa51b9c855ae38f3c"
}
}
5、在對應(yīng)頁面的 json 文件,比如首頁 index.json,添加使用插件組件的聲明
"usingComponents": {
"htmltowxml": "plugin://htmltowxml/view"
}
1、復制整個 html2wxml-component 文件夾到小程序目錄
2、在對應(yīng)頁面的 json 文件,比如首頁 index.json,添加使用組件的聲明,注意路徑
"usingComponents": {
"htmltowxml": "path/to/html2wxml-component/html2wxml"
}
1、復制整個 html2wxml-template 文件夾到小程序目錄
2、在對應(yīng)頁面的 js 文件,比如首頁 index.js,添加引用聲明,并使用html2wxml方法進行數(shù)據(jù)綁定,注意路徑,參數(shù)分別為綁定的數(shù)據(jù)名、已解析的富文本數(shù)據(jù)、當前頁面對象和容器與屏幕邊緣的單邊的距離
var html2wxml = require('path/to/html2wxml-template/html2wxml.js');
html2wxml.html2wxml('article', res.data, this, 5);
3、在對應(yīng)頁面的 wxml 文件,比如首頁 index.wxml,添加引用模板的聲明,并使用模板,注意路徑和綁定的數(shù)據(jù)名
<import src="path/to/html2wxml-template/html2wxml.wxml" />
<template is="html2wxml" data="{{wxmlData:article}}" />
4、在對應(yīng)頁面的 wxss 文件,比如首頁 index.wxss或app.wxss, 引入樣式表和你喜歡的代碼高亮樣式,注意路徑
@import "path/to/html2wxml-template/html2wxml.wxss";
@import "path/to/html2wxml-template/highlight-styles/darcula.wxss";
// 將Page中的content數(shù)據(jù)作為HTML格式渲染
<htmltowxml text="{{content}}" bindWxmlTagATap="wxmlTagATap" ></htmltowxml>
// 禁用代碼高亮功能
<htmltowxml text="{{content}}" highlight="{{false}}" bindWxmlTagATap="wxmlTagATap" ></htmltowxml>
// 禁用代碼行號顯示功能
<htmltowxml text="{{content}}" linenums="{{false}}" bindWxmlTagATap="wxmlTagATap" ></htmltowxml>
// 代碼高亮樣式改為tomorrow
<htmltowxml text="{{content}}" highlightStyle="tomorrow" bindWxmlTagATap="wxmlTagATap" ></htmltowxml>
// 設(shè)置代碼高亮檢測語言 (最多6個,自行搭建服務(wù)不受限制)
<htmltowxml text="{{content}}" highlightLanguages="{{['html','js','php','css','cpp','ruby']}}" bindWxmlTagATap="wxmlTagATap" ></htmltowxml>
// 對HTML數(shù)據(jù)中的img標簽的相對路徑補全域名
<htmltowxml text="{{content}}" imghost="https://www.qwqoffice.com" bindWxmlTagATap="wxmlTagATap" ></htmltowxml>
// 禁用加載中動畫
<htmltowxml text="{{content}}" showLoading="{{false}}" bindWxmlTagATap="wxmlTagATap" ></htmltowxml>
// 將Page中的text數(shù)據(jù)作為Markdown格式渲染
<htmltowxml text="{{text}}" type="md" bindWxmlTagATap="wxmlTagATap" ></htmltowxml>
// 直接渲染Page中的已經(jīng)過解析的obj數(shù)據(jù)
<htmltowxml json="{{obj}}" bindWxmlTagATap="wxmlTagATap" ></htmltowxml>
富文本的解析默認是由QwqOffice完成,存在不穩(wěn)定因素,你可以自行搭建解析服務(wù)或?qū)⒔馕鼋M件引入到你的項目中。
1、復制整個 html2wxml-php 文件夾到項目目錄中
2、引入類文件class.ToWXML.php
include( 'path/to/html2wxml-php/class.ToWXML.php' );
3、實例化html2wxml,進行解析并輸出,示例:
$towxml = new ToWXML();
$json = $towxml->towxml( '<h1>H1標題</h1>', array(
'type' => 'html',
'highlight' => true,
'linenums' => true,
'imghost' => null,
'encode' => false,
'highlight_languages' => array( 'html', 'js', 'php', 'css' )
) );
echo json_encode( $json, JSON_UNESCAPED_UNICODE );
*請認真填寫需求信息,我們會在24小時內(nèi)與您取得聯(lián)系。