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

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

最后通过正则匹配替换后 图片可以了视频还是不行,看了微信小程序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;
    },

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

相关推荐
八月的雨季 最後的冰吻18 小时前
音视频会议梳理
音视频
FX1317887KP_2 天前
KPF口语高频动词短语清单,搭配AI口语陪练工具,练熟练透
小程序·ket口语·pet口语·fce备考·剑桥英语
合尘猫2 天前
用 6GB 显卡搭短视频产线:MiniMax H3 + ffmpeg 全流程实录(55 秒片 / 40 分钟)
人工智能·音视频
微订校园外卖系统2 天前
自己做校园外卖跑腿小程序用什么系统:多商户与单店架构
小程序·架构·外卖系统·本地生活·微订
2401_878851013 天前
微信小程序实战:掌握“手机状态”感知与设备 API 全解析
智能手机·微信小程序·notepad++
the3clipse3 天前
Windows 11 26H2 声音与音频问题排查指南:没声音、麦克风不收音与蓝牙音频优化
音视频·le audio·windows 11 26h2·win11没声音·麦克风没声音·蓝牙耳机音质·音频服务修复
开开心心就好4 天前
PDF水印怎么去掉?勾选对象再点匹配
java·前端·mysql·支持向量机·pdf·音视频·模拟退火算法
djxzyr4 天前
微信小程序传感器实战:罗盘、加速计、陀螺仪与加速度实时波形
微信小程序·小程序
RobinDevNotes4 天前
Claude Code 剪视频,不上传、不要 API Key
人工智能·python·音视频·开发工具·ipython
伯奥克客服4 天前
2026年视频综合一体机架构选型:从接口协议到流媒体底层的深度技术解析
架构·音视频