27 lines
897 B
JavaScript
27 lines
897 B
JavaScript
/**
|
||
* 将完整 HTML 邮件转为适合 div[v-html] 的片段:
|
||
* 取 body.innerHTML,并前置 head 内 <style>,避免版式与改版前不一致。
|
||
*/
|
||
export function normalizeEmailHtmlForInlineDisplay(html) {
|
||
if (!html || typeof html !== 'string') return '';
|
||
const t = html.trim();
|
||
if (!/^<!DOCTYPE|^<\s*html[\s>]/i.test(t)) return html;
|
||
try {
|
||
const doc = new DOMParser().parseFromString(html, 'text/html');
|
||
const body = doc && doc.body;
|
||
if (!body) return html;
|
||
const inner = body.innerHTML;
|
||
if (!inner || !inner.trim()) return html;
|
||
let headInject = '';
|
||
const head = doc.head;
|
||
if (head) {
|
||
head.querySelectorAll('style').forEach((node) => {
|
||
headInject += node.outerHTML;
|
||
});
|
||
}
|
||
return headInject + inner;
|
||
} catch (e) {
|
||
return html;
|
||
}
|
||
}
|