html清除背景色和纠正图片尺寸的函数
This commit is contained in:
parent
91d1a1c9f2
commit
35cd8b1ce9
24
composables/clearnBackgroundColor.js
Normal file
24
composables/clearnBackgroundColor.js
Normal file
|
|
@ -0,0 +1,24 @@
|
||||||
|
/**
|
||||||
|
* 移除HTML内容中的背景样式并调整图片宽度
|
||||||
|
* @param {string} html - 需要处理的HTML字符串
|
||||||
|
* @returns {string} 处理后的HTML字符串
|
||||||
|
*/
|
||||||
|
export function removeBackgroundStyle(html) {
|
||||||
|
if (!html) return "";
|
||||||
|
|
||||||
|
// 1. 处理图片:设置宽度为100%屏幕宽度
|
||||||
|
let processedHtml = html.replace(
|
||||||
|
/<img([^>]*)src=["']([^"']*)["']([^>]*)>/gi,
|
||||||
|
'<img $1 src="$2" $3 style="max-width:100%;height:auto;display:block;">',
|
||||||
|
);
|
||||||
|
|
||||||
|
// 2. 移除所有元素的背景色样式
|
||||||
|
processedHtml = processedHtml.replace(/background-color:[^;"]*;?/gi, "");
|
||||||
|
|
||||||
|
// 3. 移除可能冲突的宽度/高度属性
|
||||||
|
processedHtml = processedHtml
|
||||||
|
.replace(/width=["'][^"']*["']/gi, "")
|
||||||
|
.replace(/height=["'][^"']*["']/gi, "");
|
||||||
|
|
||||||
|
return processedHtml;
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user