微信小程序富文本解析

女爷i 2021-07-24 15:45 606阅读 0赞
  1. <import src="../../wxParse/wxParse.wxml"/>
  2. <view class="wxParse">
  3. <template is="wxParse" data="{
  4. {wxParseData:article.nodes}}"/>
  5. </view>
  6. <view style="padding: 20px 10px; background-color:#eee;">
  7. <block wx:for="{
  8. {replyTemArray}}" wx:key="">
  9. 回复{
  10. {index}}:<template is="wxParse" data="{
  11. {wxParseData:item}}"/>
  12. </block>
  13. </view>

/wxParse/wxParse.wxml该文件在下面的示例文件中直接拷贝过来用就可以,文件位置需要根据个人调整

20200605111553590.png

  1. var WxParse = require('../../wxParse/wxParse.js');
  2. Page({
  3. data: {
  4. },
  5. onLoad: function () {
  6. var that = this;
  7. /**
  8. * 初始化emoji设置
  9. */
  10. WxParse.emojisInit('[]', "/wxParse/emojis/", {
  11. "00": "00.gif",
  12. "01": "01.gif",
  13. "02": "02.gif",
  14. "03": "03.gif",
  15. "04": "04.gif",
  16. "05": "05.gif",
  17. "06": "06.gif",
  18. "07": "07.gif",
  19. "08": "08.gif",
  20. "09": "09.gif",
  21. "09": "09.gif",
  22. "10": "10.gif",
  23. "11": "11.gif",
  24. "12": "12.gif",
  25. "13": "13.gif",
  26. "14": "14.gif",
  27. "15": "15.gif",
  28. "16": "16.gif",
  29. "17": "17.gif",
  30. "18": "18.gif",
  31. "19": "19.gif",
  32. });
  33. /**
  34. * html解析示例
  35. */
  36. var article = `< !DOCTYPE HTML ><!--注释: wxParse试验文本-->
  37. <div style="text-align:center;margin-top:10px;">
  38. <img src="https://weappdev.com/uploads/default/original/1X/84512e0f4591bcf0dee42c3293f826e0c24b560c.jpg" alt="wxParse-微信小程序富文本解析组件Logo">
  39. <h1 style="color:red;">wxParse-微信小程序富文本解析组件</h1>
  40. <h2 >支持Html及markdown转wxml可视化</h2>
  41. </div>
  42. <div style="margin-top:10px;">
  43. <h3 style="color: #000;">支持video</h3>
  44. <div style="margin-top:10px;">
  45. <video src="http://wxsnsdy.tc.qq.com/105/20210/snsdyvideodownload?filekey=30280201010421301f0201690402534804102ca905ce620b1241b726bc41dcff44e00204012882540400&bizid=1023&hy=SH&fileparam=302c020101042530230204136ffd93020457e3c4ff02024ef202031e8d7f02030f42400204045a320a0201000400"></video>
  46. </div>
  47. </div>
  48. <div style="margin-top:10px;">
  49. <h3 style="color: #000;">支持的标签</h3>
  50. <blockquote>wxParse支持70%的html的标签</blockquote>
  51. <div style="margin-top:10px;">
  52. <span>span标签</span>
  53. <strong style="color: red;">strong标签</strong>
  54. </div>
  55. </div>
  56. <div style="margin-top:10px;">
  57. <h3 style="color: #000;">支持的标签ul/li</h3>
  58. <blockquote>带有内联的li</blockquote>
  59. <div style="margin-top:10px;">
  60. <ul>
  61. <li style="color: red;">我是li 红色</li>
  62. <li style="color: blue;">我是li 蓝色</li>
  63. <li style="color: yelloe;">我是li 黄色</li>
  64. </ul>
  65. </div>
  66. </div>
  67. <div style="margin-top:10px;">
  68. <h3 style="color: #000;">支持内联样式style</h3>
  69. <blockquote>wxParse可以渲染原html带有的style样式</blockquote>
  70. <div style="margin-top:10px;">
  71. <span>span标签</span>
  72. <strong>strong标签</strong>
  73. </div>
  74. </div>
  75. <div style="margin-top:10px;">
  76. <h3 style="color: #000;">支持class潜入</h3>
  77. <blockquote>wxParse可以注入html带有的class属性</blockquote>
  78. </div>
  79. <div style="margin-top:10px;">
  80. <h3 style="color: #000;">支持emojis小表情</h3>
  81. <blockquote>wxParse可以解析固定格式的小表情标签</blockquote>
  82. <div style="margin-top:10px;">
  83. <p>这里可以解析出emoji的表情[00][01][02][03][04][05][06][07][08][09]</p>
  84. </div>
  85. </div>
  86. <div style="margin-top:10px;">
  87. <h3 style="color: #000;">支持图片自适应</h3>
  88. <blockquote>wxParse可以动态计算图片大小并进行自适应适配</blockquote>
  89. <div style="margin-top:10px;">
  90. <img src="http://a.hiphotos.baidu.com/image/pic/item/9a504fc2d5628535959cf4cf94ef76c6a6ef63db.jpg" alt="">
  91. <img src="http://e.hiphotos.baidu.com/image/pic/item/48540923dd54564e1e1ac2d7b7de9c82d0584fe4.jpg" alt="">
  92. </div>
  93. </div>
  94. <div style="margin-top:10px;">
  95. <h3 style="color: #000;">支持图片点击预览,左右滑动预览</h3>
  96. <blockquote>wxParse可以讲一篇文章中的几个图片一起预览</blockquote>
  97. <div style="margin-top:10px;">
  98. 你可以点击上面的图片,将会进入预览视图,同时左右滑动可以切换图片预览
  99. </div>
  100. </div>
  101. <div style="margin-top:10px;">
  102. <h3 style="color: #000;">支持多数据循环渲染</h3>
  103. <blockquote>wxParse支持特定设置下的多数据渲染,适用于在评论、多文本分别渲染等</blockquote>
  104. <div style="margin-top:10px; text-align:center;">
  105. 请继续向下看,循环渲染多条html评论
  106. </div>
  107. </div>
  108. <div style="margin-top:10px;">
  109. <h3 style="color: #000;">支持短table标签</h3>
  110. <blockquote>wxParse目前对于table的支持比较有限</blockquote>
  111. <div style="margin-top:10px; text-align:center;">
  112. <table>
  113. <tr>
  114. <th>标题1</th>
  115. <th>标题2</th>
  116. <th>标题3</th>
  117. <th>标题4</th>
  118. <th>标题5</th>
  119. </tr>
  120. <tr>
  121. <td>cell1</td>
  122. <td>cell2</td>
  123. <td>cell3</td>
  124. <td>cell4</td>
  125. <td>cell5</td>
  126. </tr>
  127. <tr>
  128. <td>cell1</td>
  129. <td>cell2</td>
  130. <td>cell3</td>
  131. <td>cell4</td>
  132. <td>cell5</td>
  133. </tr>
  134. </table>
  135. </div>
  136. </div>
  137. <!--ap-->
  138. `;
  139. WxParse.wxParse('article', 'html', article, that, 5);
  140. /**
  141. * 多数据解析示例
  142. */
  143. var replyHtml0 = `<div style="margin-top:10px;height:50px;">
  144. <p class="reply">
  145. wxParse回复0:不错,喜欢[03][04]
  146. </p>
  147. </div>`;
  148. var replyHtml1 = `<div style="margin-top:10px;height:50px;">
  149. <p class="reply">
  150. wxParse回复1:不错,喜欢[03][04]
  151. </p>
  152. </div>`;
  153. var replyHtml2 = `<div style="margin-top:10px;height:50px;">
  154. <p class="reply">
  155. wxParse回复2:不错,喜欢[05][07]
  156. </p>
  157. </div>`;
  158. var replyHtml3 = `<div style="margin-top:10px;height:50px;">
  159. <p class="reply">
  160. wxParse回复3:不错,喜欢[06][08]
  161. </p>
  162. </div>`;
  163. var replyHtml4 = `<div style="margin-top:10px; height:50px;">
  164. <p class="reply">
  165. wxParse回复4:不错,喜欢[09][08]
  166. </p>
  167. </div>`;
  168. var replyHtml5 = `<div style="margin-top:10px;height:50px;">
  169. <p class="reply">
  170. wxParse回复5:不错,喜欢[07][08]
  171. </p>
  172. </div>`;
  173. var replyArr = [];
  174. replyArr.push(replyHtml0);
  175. replyArr.push(replyHtml1);
  176. replyArr.push(replyHtml2);
  177. replyArr.push(replyHtml3);
  178. replyArr.push(replyHtml4);
  179. replyArr.push(replyHtml5);
  180. for (let i = 0; i < replyArr.length; i++) {
  181. WxParse.wxParse('reply' + i, 'html', replyArr[i], that);
  182. if (i === replyArr.length - 1) {
  183. WxParse.wxParseTemArray("replyTemArray",'reply', replyArr.length, that)
  184. }
  185. }
  186. }
  187. })

1.html解析示例 : WxParse.wxParse(‘article’, ‘html’, article, that, 5);

  1. article这个是解析的内容名称:根据个人替换
  2. 示例原文件地址:[https://github.com/Loust-jl/fwb][https_github.com_Loust-jl_fwb]

发表评论

表情:
评论列表 (有 0 条评论,606人围观)

还没有评论,来说两句吧...

相关阅读