uniapp使用rich-text,对后台传入数据进行处理

<rich-text class="content" :nodes="data.content | formatRichText"></rich-text>

过滤器

filters: {
            formatRichText(html) { //控制小程序中图片大小
                let newContent = html.replace(/<img[^>]*>/gi, function(match, capture) {
                    match = match.replace(/style="[^"]+"/gi, '').replace(/style='[^']+'/gi, '');
                    match = match.replace(/width="[^"]+"/gi, '').replace(/width='[^']+'/gi, '');
                    match = match.replace(/height="[^"]+"/gi, '').replace(/height='[^']+'/gi, '');
                    return match;
                });
                newContent = newContent.replace(/style="[^"]+"/gi, function(match, capture) {
                    match = match.replace(/width:[^;]+;/gi, 'max-width:100%;').replace(/width:[^;]+;/gi, 'max-width:100%;');
                    return match;
                });
                // newContent = newContent.replace(/<br[^>]*\/>/gi, '');
                newContent = newContent.replace(/\<img/gi,
                    '<img style="max-width:90%;height:auto;display:inline-block;margin:10rpx auto;"');
                // newContent = newContent.replace(/<br[^>]*\/>/gi, '');
                // newContent = newContent.replace(/<td[^<>]*>/ig, '<td style="padding:0px;height:auto;word-break:break-all;border:1px;">');
                // newContent = newContent.replace(/<td[^<>]*>\s*?<p>/ig, '<td>');
                newContent = newContent.replace(/<table[^>]*>/gi, '<table cellpadding="0" cellspacing="0" max-width="100%" border="1" style="font-size:12px;max-width:100%; text-align:left;text-indent: 0em;line-height:12px;">'); 
                return newContent;
            }
        },

 

 

如果使用uParse,过滤器中的内容有的不能用

posted @ 2022-03-17 17:06  埃菲尔上的加菲猫  阅读(908)  评论(0编辑  收藏  举报