From 92e3bc20b2e70298e969c08f2fe022a2ac048296 Mon Sep 17 00:00:00 2001 From: JichouP Date: Wed, 1 Mar 2023 21:06:48 +0900 Subject: [PATCH] =?UTF-8?q?=E2=9C=A8=20advanced=20bg=20for=20export?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/convertImage.ts | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/convertImage.ts b/src/convertImage.ts index c375776..5f79e73 100644 --- a/src/convertImage.ts +++ b/src/convertImage.ts @@ -70,6 +70,25 @@ export async function embedImageToHtml(html: string): Promise { if (!link) continue; el.setAttribute('src', link); } + const figures = doc.getElementsByTagName('figure'); + const reg = /background-image:url\("([^)]+)"\)/; + for (let i = 0; i < figures.length; i++) { + const el = figures[i]; + const style = el.getAttribute('style'); + if (!style || !style.contains('background-image:url')) continue; + const decoded = decodeURI(style); + const result = decoded.match(reg); + const matched = result?.at(1); + if (!matched) continue; + const converted = await convertToBase64(matched); + if (!converted) continue; + const replaced = result?.[0] + .replace(matched, converted) + .replace(/\\/g, '/'); + console.log(replaced); + if (!replaced) continue; + el.setAttribute('style', replaced); + } return doc; }