Skip to content

Commit

Permalink
✨ advanced bg for export
Browse files Browse the repository at this point in the history
  • Loading branch information
JichouP committed Mar 1, 2023
1 parent 51c7f56 commit 92e3bc2
Showing 1 changed file with 19 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/convertImage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,25 @@ export async function embedImageToHtml(html: string): Promise<Document> {
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;
}

Expand Down

0 comments on commit 92e3bc2

Please sign in to comment.