Skip to content

Commit

Permalink
adds some svg info to the output
Browse files Browse the repository at this point in the history
Took 32 minutes
  • Loading branch information
erikyo committed Apr 8, 2024
1 parent 6a64682 commit 8257a79
Showing 1 changed file with 96 additions and 0 deletions.
96 changes: 96 additions & 0 deletions src/encodeSvg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,98 @@ import { Config as SvgoConfig, optimize } from "svgo";
import { readFile, writeFile } from "node:fs/promises";
import { getSvgoOptions } from "./utils.js";
import { OutputData, SVGCompressionOption } from "./types.js";
import { parseString } from "xml2js";

interface SVGMetadata {
width?: string | null;
height?: string | null;
viewBox?: string | null;
title?: string | null;
description?: string | null;
author?: string | null;
creationDate?: string | null;
copyright?: string | null;
softwareUsed?: string | null;
customMetadata?: string[];
}

async function getSVGMetadata(svgString: string): Promise<{
metadata: SVGMetadata;
itemCount: number;
}> {
return new Promise((resolve, reject) => {
parseString(svgString, (err, result) => {
if (err) {
reject(err);
return;
}

const svgElement = result.svg;

const metadata: SVGMetadata = {
width: svgElement.$.width,
height: svgElement.$.height,
viewBox: svgElement.$.viewBox,
};

// If width and height are not defined, use viewBox to determine dimensions
if ((!metadata.width || !metadata.height) && metadata.viewBox) {
const viewBox = metadata.viewBox.split(" ");
if (viewBox.length === 4) {
metadata.width = viewBox[2];
metadata.height = viewBox[3];
}
}

if (svgElement.title && svgElement.title.length > 0) {
metadata.title = svgElement.title[0];
}

if (svgElement.desc && svgElement.desc.length > 0) {
metadata.description = svgElement.desc[0];
}

if (svgElement.metadata && svgElement.metadata.length > 0) {
const metadataElements = svgElement.metadata[0];
for (const key in metadataElements) {
if (key === "$") continue; // Skip attributes
const content = metadataElements[key][0];
switch (key) {
case "author":
metadata.author = content;
break;
case "creationdate":
metadata.creationDate = content;
break;
case "copyright":
metadata.copyright = content;
break;
case "softwareused":
metadata.softwareUsed = content;
break;
default:
if (!metadata.customMetadata) {
metadata.customMetadata = [];
}
metadata.customMetadata.push(`${key}: ${content}`);
break;
}
}
}

// remove the metadata that are undefined or false
for (const key in metadata as SVGMetadata) {
if (!metadata[key as keyof SVGMetadata]) {
delete metadata[key as keyof SVGMetadata];
}
}

const itemCount = Object.keys(svgElement).length - 1; // Subtract 1 for metadata

resolve({ metadata, itemCount });
});
});
}

/**
* The function optimizes an SVG file asynchronously using SVGO and writes the optimized SVG to a
Expand Down Expand Up @@ -47,6 +139,8 @@ export async function encodeSvg(
*/
const result = optimizeSvg(originalSvg, getSvgoOptions(options.plugins));

const { metadata, itemCount } = await getSVGMetadata(result);

// Write the optimized SVG to the destination directory
return writeFile(distFileName, result, "utf8").then(() => {
/**
Expand All @@ -57,6 +151,8 @@ export async function encodeSvg(
size: result.length,
src: srcFilename,
dist: distFileName,
metadata,
itemCount,
} as OutputData;
});
}

0 comments on commit 8257a79

Please sign in to comment.