Skip to content

Commit dc9ac02

Browse files
committed
Get width and height from xml
1 parent 96a3059 commit dc9ac02

File tree

8 files changed

+2131
-11
lines changed

8 files changed

+2131
-11
lines changed

action/dist/index.js

+2,046-6
Large diffs are not rendered by default.

action/dist/index.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

action/dist/licenses.txt

+50
Original file line numberDiff line numberDiff line change
@@ -1992,6 +1992,31 @@ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
19921992
SOFTWARE.
19931993

19941994

1995+
fast-xml-parser
1996+
MIT
1997+
MIT License
1998+
1999+
Copyright (c) 2017 Amit Kumar Gupta
2000+
2001+
Permission is hereby granted, free of charge, to any person obtaining a copy
2002+
of this software and associated documentation files (the "Software"), to deal
2003+
in the Software without restriction, including without limitation the rights
2004+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
2005+
copies of the Software, and to permit persons to whom the Software is
2006+
furnished to do so, subject to the following conditions:
2007+
2008+
The above copyright notice and this permission notice shall be included in all
2009+
copies or substantial portions of the Software.
2010+
2011+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
2012+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
2013+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
2014+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
2015+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
2016+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
2017+
SOFTWARE.
2018+
2019+
19952020
fd-slicer
19962021
MIT
19972022
Copyright (c) 2014 Andrew Kelley
@@ -3735,6 +3760,31 @@ IN THE SOFTWARE.
37353760

37363761

37373762

3763+
strnum
3764+
MIT
3765+
MIT License
3766+
3767+
Copyright (c) 2021 Natural Intelligence
3768+
3769+
Permission is hereby granted, free of charge, to any person obtaining a copy
3770+
of this software and associated documentation files (the "Software"), to deal
3771+
in the Software without restriction, including without limitation the rights
3772+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
3773+
copies of the Software, and to permit persons to whom the Software is
3774+
furnished to do so, subject to the following conditions:
3775+
3776+
The above copyright notice and this permission notice shall be included in all
3777+
copies or substantial portions of the Software.
3778+
3779+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
3780+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
3781+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
3782+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
3783+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
3784+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
3785+
SOFTWARE.
3786+
3787+
37383788
strtok3
37393789
MIT
37403790
Copyright (c) 2017, Borewit

action/package.json

+1
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
"cross-env": "^7.0.3",
3939
"cross-fetch": "^3.1.5",
4040
"eslint-plugin-prettier": "^4.2.1",
41+
"fast-xml-parser": "^4.1.3",
4142
"form-data": "^4.0.0",
4243
"handlebars": "^4.7.7",
4344
"jimp": "^0.22.0",

action/scripts/testSvg.ts

+9-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
11
// @ts-ignore
22
import { convertFile } from 'convert-svg-to-png';
3+
import { XMLParser } from 'fast-xml-parser';
34
// @ts-ignore
45
import fs from 'fs';
56

6-
const path = '../docs/static/img/examples/jacoco-logo.svg';
7+
const path = '../docs/static/img/examples/jest-logo.svg';
78

89
(async () => {
9-
await convertFile(path);
10+
const svg = fs.readFileSync(path, 'utf8');
11+
const parser = new XMLParser({ ignoreAttributes: false });
12+
const svgObject = parser.parse(svg);
13+
const viewBox: string = String(svgObject.svg['@_viewBox']);
14+
const [, , width, height] = viewBox.split(' ').map((value: string) => parseInt(value, 10));
15+
console.log(`Image size: ${width} x ${height}`);
16+
await convertFile(path, { outputFilePath: 'test.png', width, height });
1017
})();

action/src/declaration.d.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
11
declare module 'convert-svg-to-png' {
2-
const convertFile: (svg: string, options?: { outputFilePath: string }) => Promise<void>;
2+
const convertFile: (
3+
svg: string,
4+
options?: { outputFilePath?: string; width?: number; height?: number }
5+
) => Promise<void>;
36
}

action/src/plugins/imageDiff/plugin.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import * as core from '@actions/core';
22
import { convertFile } from 'convert-svg-to-png';
33
import { randomUUID } from 'crypto';
4+
import { XMLParser } from 'fast-xml-parser';
45
import fs from 'fs';
56
import Jimp from 'jimp';
67
import _ from 'lodash';
@@ -140,7 +141,13 @@ export const getNormalizedImage = async (
140141
core.info(`[${taskId}] Converting ${fileType} ${inputFilePath} to ${outputFilePath}...`);
141142
try {
142143
if (extension.toLowerCase() === 'svg') {
143-
await convertFile(inputFilePath, { outputFilePath });
144+
const svg = fs.readFileSync(inputFilePath, 'utf8');
145+
const svgParser = new XMLParser({ ignoreAttributes: false });
146+
const svgObject = svgParser.parse(svg);
147+
const [, , svgWidth, svgHeight] = String(svgObject.svg['@_viewBox'])
148+
.split(' ')
149+
.map((value: string) => parseInt(value, 10));
150+
await convertFile(inputFilePath, { outputFilePath, width: svgWidth, height: svgHeight });
144151
} else {
145152
const baselineFile = await Jimp.read(inputFilePath);
146153
if (width !== undefined && height !== undefined) {

action/yarn.lock

+12
Original file line numberDiff line numberDiff line change
@@ -2689,6 +2689,13 @@ fast-levenshtein@^2.0.6:
26892689
resolved "https://registry.yarnpkg.com/fast-levenshtein/-/fast-levenshtein-2.0.6.tgz#3d8a5c66883a16a30ca8643e851f19baa7797917"
26902690
integrity sha512-DCXu6Ifhqcks7TZKY3Hxp3y6qphY5SJZmrWMDrKcERSOXWQdMhU9Ig/PYrzyw/ul9jOIyh0N4M0tbC5hodg8dw==
26912691

2692+
fast-xml-parser@^4.1.3:
2693+
version "4.1.3"
2694+
resolved "https://registry.yarnpkg.com/fast-xml-parser/-/fast-xml-parser-4.1.3.tgz#0254ad0d4d27f07e6b48254b068c0c137488dd97"
2695+
integrity sha512-LsNDahCiCcJPe8NO7HijcnukHB24tKbfDDA5IILx9dmW3Frb52lhbeX6MPNUSvyGNfav2VTYpJ/OqkRoVLrh2Q==
2696+
dependencies:
2697+
strnum "^1.0.5"
2698+
26922699
fastq@^1.6.0:
26932700
version "1.15.0"
26942701
resolved "https://registry.yarnpkg.com/fastq/-/fastq-1.15.0.tgz#d04d07c6a2a68fe4599fea8d2e103a937fae6b3a"
@@ -4934,6 +4941,11 @@ strip-json-comments@^3.1.0, strip-json-comments@^3.1.1:
49344941
resolved "https://registry.yarnpkg.com/strip-json-comments/-/strip-json-comments-3.1.1.tgz#31f1281b3832630434831c310c01cccda8cbe006"
49354942
integrity sha512-6fPc+R4ihwqP6N/aIv2f1gMH8lOVtWQHoqC4yK6oSDVVocumAsfCqjkXnqiYMhmMwS/mEHLp7Vehlt3ql6lEig==
49364943

4944+
strnum@^1.0.5:
4945+
version "1.0.5"
4946+
resolved "https://registry.yarnpkg.com/strnum/-/strnum-1.0.5.tgz#5c4e829fe15ad4ff0d20c3db5ac97b73c9b072db"
4947+
integrity sha512-J8bbNyKKXl5qYcR36TIO8W3mVGVHrmmxsd5PAItGkmyzwJvybiw2IVq5nqd0i4LSNSkB/sx9VHllbfFdr9k1JA==
4948+
49374949
strtok3@^6.2.4:
49384950
version "6.3.0"
49394951
resolved "https://registry.yarnpkg.com/strtok3/-/strtok3-6.3.0.tgz#358b80ffe6d5d5620e19a073aa78ce947a90f9a0"

0 commit comments

Comments
 (0)