小程序中展示富文本 图片不适配?视频不显示?

最近遇到一个问题在小程序中渲染富文本的内容,如果里面有图片和视频,渲染的时候图片大小超屏幕了,而视频完全没有显示!!!

最后通过正则匹配替换后 图片可以了视频还是不行,看了微信小程序api官网才知道不支持视频渲染,那怎么办呢?

最终方法:

复制代码
    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.replaceAll('style=""', '');
      // newContent = newContent.replace(/<br[^>]*\/>/gi, '');
      //处理图片
      // newContent = newContent.replace(/\<img/gi, '<img style="max-width:100%;width:100%;height:auto;vertical-align: middle;"');
      //处理媒体
      //获取视频的地址
      const videoRegex = /<div data-w-e-type="video"[^>]*>[\s\S]*?<\/div>/gi;
      const regexSrc = /src="([^"]+)"/
      const videoItem = newContent.match(videoRegex);
      if (videoItem.length > 0) {
        for (let i = 0; i < videoItem.length; i++) {
          let src = videoItem[i].match(regexSrc);
          this.detailsMedia.push({url: src[1], type: 'video'})
        }
      }
      newContent = newContent.replace(videoRegex, '')
      //获取图片地址
      const imgRegex = /<img[^>]*\s+src="([^"]+)"[^>]*>/gi
      const imgItem = newContent.match(imgRegex);
      if (imgItem.length > 0) {
        for (let i = 0; i < imgItem.length; i++) {
          let src = imgItem[i].match(regexSrc);
          this.detailsMedia.push({url: src[1], type: 'image'})
        }
      }
      newContent = newContent.replace(imgRegex, '')
      // 获取富文本文本
      // 正则表达式  全局匹配 table tr td标签,并给各自标签添加class类名
      newContent = newContent.replace(/<table/g, '<table class="table" style="width:100%;"')
      newContent = newContent.replace(/<tr/g, '<tr class="tr" style="display: table-row;"')
      newContent = newContent.replace(/<td/g, '<td class="td" style="display: table-cell;text-align: left;"')
      newContent = newContent.replace(/<ol/g, '<ol class="ol" style="text-align: left;padding-left:20rpx;margin:0;"')
      return newContent;
    },

通过提取地址,我自己渲染~哈哈哈

相关推荐
EasyGBS1 天前
视频画面模糊、卡顿、丢失?EasyGBS新增“视频质量诊断”功能,告别人工盯屏
视觉检测·音视频·gb28181·花屏·视频质量诊断·蓝屏检测
zhuweisky1 天前
ArkTS实现鸿蒙手机视频聊天、屏幕分享(HarmonyOS)
音视频·harmonyos·鸿蒙开发
XHW___0011 天前
webrtc 关键模块创建的时机
网络·音视频·webrtc
Leinwin1 天前
VibeVoice-ASR:突破60分钟长音频处理瓶颈,语音识别进入端到端时代
人工智能·音视频·语音识别
EasyDSS1 天前
直播点播/视频会议EasyDSS一站式视频云平台,全场景视频服务开箱即用
音视频·hls·m3u8·点播技术·流媒体直播
光影少年1 天前
AIGC + Taro / 小程序
小程序·aigc·taro
Guheyunyi1 天前
什么是安全监测预警系统?应用场景有哪些?
大数据·运维·人工智能·安全·音视频
2501_915918411 天前
在 iOS 环境下查看 App 详细信息与文件目录
android·ios·小程序·https·uni-app·iphone·webview
浮桥1 天前
uniapp+h5 公众号实现分享海报绘制
uni-app·notepad++
LittroInno1 天前
TVMS视频管理平台 —— 目标识别跟踪
人工智能·计算机视觉·音视频