index.js 875 B

1234567891011121314151617
  1. export const formatRichText = (html) => {
  2. if (!html) return '';
  3. let newContent = html;
  4. // 1. 移除 img 标签中的 style 属性
  5. newContent = newContent.replace(/<img[^>]*>/gi, function(match) {
  6. match = match.replace(/style="[^"]+"/gi, '').replace(/style='[^']+'/gi, '');
  7. match = match.replace(/width="[^"]+"/gi, '').replace(/width='[^']+'/gi, '');
  8. match = match.replace(/height="[^"]+"/gi, '').replace(/height='[^']+'/gi, '');
  9. return match;
  10. });
  11. // 2. 强制为 img 标签添加自适应样式,并处理为块级元素
  12. newContent = newContent.replace(/\<img/gi, '<img style="max-width:100%;height:auto;display:block;"');
  13. // 3. 将富文本中可能出现的 <text> 标签替换为 <span>
  14. newContent = newContent.replace(/<text/gi, '<span').replace(/<\/text>/gi, '</span>');
  15. return newContent;
  16. };