Factor out convertSvgMarkupToSymbolData(), add test.

pull/23/head
Atul Varma 2021-02-21 20:54:36 -05:00
rodzic 7e9ffa3738
commit 7322740b14
3 zmienionych plików z 67 dodań i 15 usunięć

Wyświetl plik

@ -0,0 +1,33 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`convertSvgMarkupToSymbolData() works with SVGs that just have a path and no specs 1`] = `
Object {
"bbox": Object {
"x": Object {
"max": 692.249,
"min": 27.751,
},
"y": Object {
"max": 692.249,
"min": 27.751,
},
},
"layers": Array [
Object {
"children": Array [],
"props": Object {
"d": "M 360.000 29.751 C 542.791 29.751 690.249 177.209 690.249 360.000 C 690.249 542.791 542.791 690.249 360.000 690.249 C 177.209 690.249 29.751 542.791 29.751 360.000 C 29.751 177.209 177.209 29.751 360.000 29.751 Z",
"fill": "#ffffff",
"fillRule": "evenodd",
"stroke": "#000000",
"strokeLinecap": "round",
"strokeLinejoin": "round",
"strokeWidth": "4",
},
"tagName": "path",
},
],
"name": "blarg",
"specs": undefined,
}
`;

Wyświetl plik

@ -0,0 +1,11 @@
import { convertSvgMarkupToSymbolData } from "./vocabulary-builder";
const CIRCLE = `<path fill="#ffffff" fill-rule="evenodd" stroke="#000000" stroke-width="4" stroke-linecap="round" stroke-linejoin="round" d="M 360.000 29.751 C 542.791 29.751 690.249 177.209 690.249 360.000 C 690.249 542.791 542.791 690.249 360.000 690.249 C 177.209 690.249 29.751 542.791 29.751 360.000 C 29.751 177.209 177.209 29.751 360.000 29.751 Z"/>`;
describe("convertSvgMarkupToSymbolData()", () => {
it("works with SVGs that just have a path and no specs", () => {
expect(
convertSvgMarkupToSymbolData("blarg.svg", `<svg>${CIRCLE}</svg>`)
).toMatchSnapshot();
});
});

Wyświetl plik

@ -82,6 +82,28 @@ function serializeSvgSymbolElement(
throw new Error(`Unsupported SVG element: <${tagName}>`); throw new Error(`Unsupported SVG element: <${tagName}>`);
} }
export function convertSvgMarkupToSymbolData(
filename: string,
svgMarkup: string
): SvgSymbolData {
const name = path.basename(filename, SVG_EXT).toLowerCase();
const $ = cheerio.load(svgMarkup);
const svgEl = $("svg");
const rawLayers = onlyTags(svgEl.children()).map((ch) =>
serializeSvgSymbolElement($, ch)
);
const [specs, layers] = extractSpecs(rawLayers);
const bbox = getSvgBoundingBox(layers);
const symbol: SvgSymbolData = {
name,
bbox,
layers,
specs,
};
return symbol;
}
export function build() { export function build() {
const filenames = fs.readdirSync(SVG_DIR); const filenames = fs.readdirSync(SVG_DIR);
const vocab: SvgSymbolData[] = []; const vocab: SvgSymbolData[] = [];
@ -91,21 +113,7 @@ export function build() {
const svgMarkup = fs.readFileSync(path.join(SVG_DIR, filename), { const svgMarkup = fs.readFileSync(path.join(SVG_DIR, filename), {
encoding: "utf-8", encoding: "utf-8",
}); });
const $ = cheerio.load(svgMarkup); const symbol = convertSvgMarkupToSymbolData(filename, svgMarkup);
const svgEl = $("svg");
const name = path.basename(filename, SVG_EXT).toLowerCase();
const rawLayers = onlyTags(svgEl.children()).map((ch) =>
serializeSvgSymbolElement($, ch)
);
const [specs, layers] = extractSpecs(rawLayers);
const bbox = getSvgBoundingBox(layers);
const symbol: SvgSymbolData = {
name,
bbox,
layers,
specs,
};
vocab.push(symbol); vocab.push(symbol);
} }
} }