uni-app如何解析html标签
一、导入rechText.js
参考网上资料uni-app如何完美解析富文本内容
核心内容如下:
/* graceUI rich-text 加强工具 link : graceui.hcoder.net author : 5213606@qq.com 深海 */ // 正则变量 var graceRichTextReg; // 批量替换的样式 [ 根据项目需求自行设置 ] var GRT = [ // div 样式 ['div', "line-height:2em;"], // h1 样式 ['h1', "font-size:3em; line-height:1.5em;"], // h2 样式 ['h2', "font-size:2em; line-height:1.8em;"], // h3 样式 ['h3', "font-size:1.6em; line-height:2em;"], // h4 样式 ['h4', "font-size:1.2em; line-height:2em;"], // h5 样式 ['h5', "font-size:1em; line-height:2em;"], // h6 样式 ['h6', "font-size:0.9em; line-height:2em;"], // p 样式 ['p', "font-size:1em; line-height:2em;"], // b 样式 ['b', "font-size:1em; line-height:2em;"], // strong 样式 ['strong', "font-size:1em; line-height:2em;"], // code 样式 ['code', "font-size:1em; line-height:1.2em; background:#F6F7F8; padding:8px 2%; width:96%;"], // img 样式 ['img', "width:100%; margin:8px 0;"], // blockquote ['blockquote', "font-size:1em; border-left:3px solid #D1D1D1; line-height:2em; border-radius:5px; background:#F6F7F8; padding:8px 2%;"], // li 样式 ['ul', "padding:5px 0; list-style:none; padding:0; margin:0;"], ['li', "line-height:1.5em; padding:5px 0; list-style:none; padding:0; margin:0; margin-top:10px;"], // table ['table', "width:100%; border-left:1px solid #F2F3F4; border-top:1px solid #F2F3F4;"], ['th', "border-right:1px solid #F2F3F4; border-bottom:1px solid #F2F3F4;"], ['td', "border-right:1px solid #F2F3F4; border-bottom:1px solid #F2F3F4; padding-left:5px;"] ]; module.exports = { format : function(html){ html = html.replace(/<pre.*pre>?/gis, function(word){ word = word.replace(/[\n]/gi,'<br />'); word = word.replace(/ /gi,'<span style="padding-left:2em;"></span>'); return word.replace(/[\t]/gi, '<span style="padding-left:2em;"></span>'); }); html = html.replace(/<pre/gi, '<p style="font-size:1em; margin:12px 0; line-height:1.2em; background:#F6F7F8; border-radius:5px; padding:8px 4%; width:92%;"'); html = html.replace(/<\/pre/gi,"</p"); for(let i = 0; i < GRT.length; i++){ graceRichTextReg = new RegExp('<'+GRT[i][0]+'>|<'+GRT[i][0]+' (.*?)>', 'gi'); html = html.replace(graceRichTextReg , function(word){ // 分析 dom 上是否带有 style="" if(word.indexOf('style=') != -1){ var regIn = new RegExp('<' + GRT[i][0] + '(.*?)style="(.*?)"(.*?)(/?)>', 'gi'); return word.replace(regIn, '<'+ GRT[i][0] +'$1style="$2 ' + GRT[i][1] +'"$3$4>'); }else{ var regIn = new RegExp('<' + GRT[i][0] + '(.*?)(/?)>', 'gi'); return word.replace(regIn, '<'+ GRT[i][0] +'$1 style="' + GRT[i][1] +'$2">'); } }); } return html; } }
二、uni-app引入rechText.js
import graceRichText from '../../../components/richText.js';
三、核心渲染代码
<rich-text :nodes="article"></rich-text>
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· [AI/GPT/综述] AI Agent的设计模式综述