editorImg.js 1.4 KB

123456789101112131415161718192021222324252627
  1. // +----------------------------------------------------------------------
  2. // | CRMEB [ CRMEB赋能开发者,助力企业发展 ]
  3. // +----------------------------------------------------------------------
  4. // | Copyright (c) 2016~2022 https://www.crmeb.com All rights reserved.
  5. // +----------------------------------------------------------------------
  6. // | Licensed CRMEB并不是自由软件,未经许可不能去掉CRMEB相关版权
  7. // +----------------------------------------------------------------------
  8. // | Author: CRMEB Team <admin@crmeb.com>
  9. // +----------------------------------------------------------------------
  10. export function formatRichText(html) {
  11. let newContent = html.replace(/<img[^>]*>/gi, function (match, capture) {
  12. match = match.replace(/style="[^"]+"/gi, '').replace(/style='[^']+'/gi, '');
  13. match = match.replace(/width="[^"]+"/gi, '').replace(/width='[^']+'/gi, '');
  14. match = match.replace(/height="[^"]+"/gi, '').replace(/height='[^']+'/gi, '');
  15. return match;
  16. });
  17. newContent = newContent.replace(/style="[^"]+"/gi, function (match, capture) {
  18. match = match.replace(/width:[^;]+;/gi, 'max-width:100%;').replace(/width:[^;]+;/gi, 'max-width:100%;');
  19. return match;
  20. });
  21. newContent = newContent.replace(/<br[^>]*\/>/gi, '');
  22. newContent = newContent.replace(
  23. /\<img/gi,
  24. '<img style="max-width:100%;height:auto;display:block;margin-top:0;margin-bottom:0;"',
  25. );
  26. return newContent;
  27. }