Warning: error_log(/data/www/wwwroot/hmttv.cn/caches/error_log.php): failed to open stream: Permission denied in /data/www/wwwroot/hmttv.cn/phpcms/libs/functions/global.func.php on line 537 Warning: error_log(/data/www/wwwroot/hmttv.cn/caches/error_log.php): failed to open stream: Permission denied in /data/www/wwwroot/hmttv.cn/phpcms/libs/functions/global.func.php on line 537
今天為大家帶來的是一個用于在微信小程序中渲染html和Markdown的富文本組件,而且支持代碼高亮,它就是html2wxml!
https://github.com/qwqoffice/html2wxml
1、打開小程序管理后臺,轉到設置 - 第三方服務,點擊添加插件
2、搜索 html2wxml ,選中并添加
3、添加成功
4、回到小程序開發環境,編輯 app.json ,添加插件聲明,最新版為 1.3.0
"plugins": {
"htmltowxml": {
"version": "1.3.0",
"provider": "wxa51b9c855ae38f3c"
}
}
5、在對應頁面的 json 文件,比如首頁 index.json,添加使用插件組件的聲明
"usingComponents": {
"htmltowxml": "plugin://htmltowxml/view"
}
1、復制整個 html2wxml-component 文件夾到小程序目錄
2、在對應頁面的 json 文件,比如首頁 index.json,添加使用組件的聲明,注意路徑
"usingComponents": {
"htmltowxml": "path/to/html2wxml-component/html2wxml"
}
1、復制整個 html2wxml-template 文件夾到小程序目錄
2、在對應頁面的 js 文件,比如首頁 index.js,添加引用聲明,并使用html2wxml方法進行數據綁定,注意路徑,參數分別為綁定的數據名、已解析的富文本數據、當前頁面對象和容器與屏幕邊緣的單邊的距離
var html2wxml=require('path/to/html2wxml-template/html2wxml.js');
html2wxml.html2wxml('article', res.data, this, 5);
3、在對應頁面的 wxml 文件,比如首頁 index.wxml,添加引用模板的聲明,并使用模板,注意路徑和綁定的數據名
<import src="path/to/html2wxml-template/html2wxml.wxml" />
<template is="html2wxml" data="{{wxmlData:article}}" />
4、在對應頁面的 wxss 文件,比如首頁 index.wxss或app.wxss, 引入樣式表和你喜歡的代碼高亮樣式,注意路徑
@import "path/to/html2wxml-template/html2wxml.wxss";
@import "path/to/html2wxml-template/highlight-styles/darcula.wxss";
// 將Page中的content數據作為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>
// 設置代碼高亮檢測語言 (最多6個,自行搭建服務不受限制)
<htmltowxml text="{{content}}" highlightLanguages="{{['html','js','php','css','cpp','ruby']}}" bindWxmlTagATap="wxmlTagATap" ></htmltowxml>
// 對HTML數據中的img標簽的相對路徑補全域名
<htmltowxml text="{{content}}" imghost="https://www.qwqoffice.com" bindWxmlTagATap="wxmlTagATap" ></htmltowxml>
// 禁用加載中動畫
<htmltowxml text="{{content}}" showLoading="{{false}}" bindWxmlTagATap="wxmlTagATap" ></htmltowxml>
// 將Page中的text數據作為Markdown格式渲染
<htmltowxml text="{{text}}" type="md" bindWxmlTagATap="wxmlTagATap" ></htmltowxml>
// 直接渲染Page中的已經過解析的obj數據
<htmltowxml json="{{obj}}" bindWxmlTagATap="wxmlTagATap" ></htmltowxml>
富文本的解析默認是由QwqOffice完成,存在不穩定因素,你可以自行搭建解析服務或將解析組件引入到你的項目中。
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 );
、將富文本html內容轉換為純文本
formatrichtext=(richtext, len=0)=> {
let content=richtext.replace(/<.+?>/g, '');
content=content.replace(/ /ig, ''); /* 去除 */
content=content.replace(/\s/ig, ''); /* 去除空格 */
return content;
}
2、限制展示的文本長度
隨著時間的推移,瀏覽器已經實現了多個剪貼板 API,并且在各種舊的和當前的瀏覽器中寫入剪貼板而不觸發錯誤是相當棘手的。在每個支持以某種方式復制到剪貼板的瀏覽器中,clipboard-polyfill都會嘗試盡可能接近異步剪貼板 API。
github地址:https://github.com/lgarron/clipboard-polyfill
特別提醒:
以前有個類庫叫clipboard-js,已經被廢棄,遷移到了clipboard-polyfill
安裝
npm install clipboard-polyfill
引入
import * as clipboard from "clipboard-polyfill"
使用
*請認真填寫需求信息,我們會在24小時內與您取得聯系。