diff --git a/package.json b/package.json index 89bc00fe7..b3b5cdbed 100644 --- a/package.json +++ b/package.json @@ -34,6 +34,11 @@ "name": "Bogdan Chadkin", "email": "trysound@yandex.ru", "url": "https://github.com/TrySound" + }, + { + "name": "Seth Falco", + "email": "seth@falco.fun", + "url": "https://falco.fun/" } ], "repository": { diff --git a/plugins/reusePaths.js b/plugins/reusePaths.js index 4d91d6f9d..4e7d3e7c8 100644 --- a/plugins/reusePaths.js +++ b/plugins/reusePaths.js @@ -26,6 +26,13 @@ exports.fn = () => { */ const paths = new Map(); + /** + * Set of hrefs that reference the id of another node. + * + * @type {Set} + */ + const hrefs = new Set(); + return { element: { enter: (node) => { @@ -41,12 +48,23 @@ exports.fn = () => { } list.push(node); } + + if (node.name == 'use') { + for (const name of ['href', 'xlink:href']) { + const href = node.attributes[name]; + + if (href != null && href.startsWith('#') && href.length > 1) { + hrefs.add(href.slice(1)); + } + } + } }, exit: (node, parentNode) => { if (node.name === 'svg' && parentNode.type === 'root') { /** * @type {XastElement} + * @see https://developer.mozilla.org/en-US/docs/Web/SVG/Element/defs */ const defsTag = { type: 'element', @@ -74,7 +92,8 @@ exports.fn = () => { }; delete reusablePath.attributes.transform; let id; - if (reusablePath.attributes.id == null) { + const reusablePathId = reusablePath.attributes.id; + if (reusablePathId == null || hrefs.has(reusablePathId)) { id = 'reuse-' + index; index += 1; reusablePath.attributes.id = id; diff --git a/test/plugins/reusePaths.04.svg b/test/plugins/reusePaths.04.svg new file mode 100644 index 000000000..a96d550c4 --- /dev/null +++ b/test/plugins/reusePaths.04.svg @@ -0,0 +1,40 @@ +Don't remove and reuse the ID of the duplicate path if it's already being linked +in an href by another node. + +=== + + + + + + + + + + + + + + + + +@@@ + + + + + + + + + + + + + + + + + +