From f07a36d31d6b125ab6cb9926e7eddee0b4d35c49 Mon Sep 17 00:00:00 2001 From: Michael M Date: Mon, 24 Jul 2023 20:53:33 +0200 Subject: [PATCH] feat: enable prefixIds svgo plugin (#1110) --- svgo.config.js | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/svgo.config.js b/svgo.config.js index 4d00c7bf..732a0e93 100644 --- a/svgo.config.js +++ b/svgo.config.js @@ -3,6 +3,18 @@ module.exports = { { name: "preset-default", }, + { + name: "prefixIds", + params: { + delim: "-", + prefix: (_, info) => { + if (info.path != null && info.path.length > 0) { + return getBasename(info.path).split(".")[0]; + } + return "prefix"; + }, + }, + }, "convertStyleToAttrs", "removeDimensions", "removeScriptElement", @@ -10,3 +22,13 @@ module.exports = { "sortAttrs", ], }; + +/** + * extract basename from path + * @see https://github.com/svg/svgo/blob/main/plugins/prefixIds.js + */ +const getBasename = (path) => { + const matched = path.match(/[/\\]?([^/\\]+)$/); + if (matched) return matched[1]; + return ""; +};