Skip to content

Commit

Permalink
Add linter to check svg path dimensions
Browse files Browse the repository at this point in the history
Float precision is set at 3 which is the default for svgo in .svgo.yml
  • Loading branch information
davidjb committed May 26, 2020
1 parent 7b456f4 commit 8bf5084
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
17 changes: 16 additions & 1 deletion .svglintrc.js
@@ -1,7 +1,10 @@
const data = require("./_data/simple-icons.json");
const { htmlFriendlyToTitle } = require("./scripts/utils.js");
const getBounds = require('svg-path-bounding-box');

const titleRegexp = /(.+) icon$/;
const floatPrecision = 3;
const targetIconSize = 24;

module.exports = {
rules: {
Expand All @@ -14,7 +17,7 @@ module.exports = {
attr: [
{ // ensure that the SVG elm has the appropriate attrs
"role": "img",
"viewBox": "0 0 24 24",
"viewBox": `0 0 ${targetIconSize} ${targetIconSize}`,
"xmlns": "http://www.w3.org/2000/svg",

"rule::selector": "svg",
Expand Down Expand Up @@ -46,6 +49,18 @@ module.exports = {
}
}
},
function(reporter, $, ast) {
const iconPath = $.find("path").attr("d");
const bounds = getBounds(iconPath);
const width = +bounds.width.toFixed(floatPrecision);
const height = +bounds.height.toFixed(floatPrecision);

if (width === 0 && height === 0) {
reporter.error(`Path bounds reported as 0 x 0; check the path is valid`);
} else if (width !== targetIconSize && height !== targetIconSize) {
reporter.error(`Size of <path> must be exactly ${targetIconSize} in one dimension; currently ${width} x ${height}`);
}
},
]
}
};
15 changes: 15 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Expand Up @@ -22,6 +22,7 @@
"jest": "26.0.1",
"jsonlint2": "1.7.1",
"npm-run-all": "4.1.5",
"svg-path-bounding-box": "^1.0.4",
"svglint": "1.0.5",
"svgo": "1.3.2",
"uglify-js": "3.9.3"
Expand Down

0 comments on commit 8bf5084

Please sign in to comment.