Skip to content

Commit

Permalink
✨ advanced bg for preview
Browse files Browse the repository at this point in the history
  • Loading branch information
JichouP committed Mar 1, 2023
1 parent baa1b1d commit 51c7f56
Showing 1 changed file with 20 additions and 1 deletion.
21 changes: 20 additions & 1 deletion src/convertImage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,26 @@ export async function convertHtml(html: string): Promise<Document> {
if (!src) continue;
const link = await convertPathToLocalLink(decodeURI(src));
if (!link) continue;
el.setAttribute('src', link);
el.setAttribute('src', link.replace(/\\/g, '/'));
}
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 convertPathToLocalLink(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;
}

0 comments on commit 51c7f56

Please sign in to comment.