Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
The table of contents is too big for display.
Diff view
Diff view
  •  
  •  
  •  
91 changes: 82 additions & 9 deletions manage-image-assets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,59 @@ const sizes = [
{ width: 320, height: 320 },
];

// librsvg (used by sharp) cannot render WebP inside <image> — convert embeds to PNG.
const WEBP_DATA_URI_RE =
/((?:xlink:)?href=["'])data:image\/webp;base64,([A-Za-z0-9+/=]+)(["'])/g;

async function replaceWebpEmbedsWithPng(svg: string): Promise<{
svg: string;
replaced: number;
}> {
const matches: RegExpExecArray[] = [];
let match: RegExpExecArray | null;

WEBP_DATA_URI_RE.lastIndex = 0;
while ((match = WEBP_DATA_URI_RE.exec(svg)) !== null) {
matches.push(match);
}

if (matches.length === 0) {
return { svg, replaced: 0 };
}

let result = svg;
for (const match of matches) {
const [full, prefix, base64, suffix] = match;
const pngBuf = await sharp(Buffer.from(base64, "base64")).png().toBuffer();
const replacement = `${prefix}data:image/png;base64,${pngBuf.toString(
"base64"
)}${suffix}`;
result = result.replace(full, replacement);
}

return { svg: result, replaced: matches.length };
}

async function prepareSvgInput(file: string): Promise<Buffer | null> {
const original = await fsPromise.readFile(file, "utf8");

if (!original.trim()) {
console.error(`Skipping empty SVG: ${file}`);
return null;
}

const { svg, replaced } = await replaceWebpEmbedsWithPng(original);

if (replaced > 0) {
console.warn(
`Replaced ${replaced} WebP embed(s) with PNG in ${file} (librsvg cannot render WebP in SVG <image>)`
);
await fsPromise.writeFile(file, svg);
}

return Buffer.from(svg);
}

async function deletePNGFiles() {
const files = await glob("**/*_*x*.png"); // This pattern matches PNGs that were likely generated by the conversion function

Expand All @@ -21,22 +74,40 @@ async function deletePNGFiles() {
}

async function convertSVGToPNGForFile(file: string) {
const pendingSizes = sizes.filter((size) => {
const outputPath = file.replace(
".svg",
`_${size.width}x${size.height}.png`
);

if (fs.existsSync(outputPath)) {
console.log("Skipped conversion for existing file:", outputPath);
return false;
}

return true;
});

if (pendingSizes.length === 0) {
return [];
}

const svgInput = await prepareSvgInput(file);
if (!svgInput) {
return [];
}

return (
await Promise.all(
sizes.map(async (size) => {
pendingSizes.map(async (size) => {
const outputPath = file.replace(
".svg",
`_${size.width}x${size.height}.png`
);

if (fs.existsSync(outputPath)) {
console.log("Skipped conversion for existing file:", outputPath);

return null;
}

try {
await sharp(file)
// Copy the buffer per size — sharp can consume/reuse input buffers.
await sharp(Buffer.from(svgInput))
.resize({ width: size.width, height: size.height, fit: "inside" })
.toFile(outputPath);

Expand All @@ -58,7 +129,9 @@ async function convertSVGToPNG() {

const res = (await Promise.all(files.map(convertSVGToPNGForFile))).flat();

execSync(`git add ${res.join(" ")}`);
if (res.length > 0) {
execSync(`git add ${res.join(" ")}`);
}
}

if (process.argv[2] === "delete") {
Expand Down
File renamed without changes
File renamed without changes
File renamed without changes
1 change: 1 addition & 0 deletions tokens/2btc-f.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tokens/2btc-f_160x160.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tokens/2btc-f_320x320.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 5 additions & 0 deletions tokens/2pool.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tokens/2pool_160x160.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tokens/2pool_320x320.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
11 changes: 11 additions & 0 deletions tokens/3c-crvusd.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tokens/3c-crvusd_160x160.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tokens/3c-crvusd_320x320.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
11 changes: 11 additions & 0 deletions tokens/3crv-op.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tokens/3crv-op_160x160.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tokens/3crv-op_320x320.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
23 changes: 23 additions & 0 deletions tokens/3eurpool-f.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tokens/3eurpool-f_160x160.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tokens/3eurpool-f_320x320.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 5 additions & 0 deletions tokens/4poolusd-f.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tokens/4poolusd-f_160x160.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tokens/4poolusd-f_320x320.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
14 changes: 14 additions & 0 deletions tokens/a3crv.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tokens/a3crv_160x160.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added tokens/a3crv_320x320.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Loading