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