From 190ced95dd2c8f90e41db057e4d4ea1e58d6f29e Mon Sep 17 00:00:00 2001 From: Titus Wormer Date: Sat, 11 Sep 2021 08:45:46 +0200 Subject: [PATCH] rehype-minify-url: fix `link[rel=canonical]` MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Previously the `[href]` of a `link[rel=canonical]` was minified. It shouldn’t be. --- packages/rehype-minify-url/index.js | 8 +++++++- packages/rehype-minify-url/test.js | 23 +++++++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/packages/rehype-minify-url/index.js b/packages/rehype-minify-url/index.js index 11d3b9c..ff12980 100644 --- a/packages/rehype-minify-url/index.js +++ b/packages/rehype-minify-url/index.js @@ -70,7 +70,13 @@ export default function rehypeMinifyUrl(options) { if ( hasProperty(node, prop) && own.call(urlAttributes, prop) && - isElement(node, urlAttributes[prop]) + isElement(node, urlAttributes[prop]) && + !( + node.tagName === 'link' && + prop === 'href' && + Array.isArray(props.rel) && + props.rel.includes('canonical') + ) ) { props[prop] = minify(props[prop], relate) } diff --git a/packages/rehype-minify-url/test.js b/packages/rehype-minify-url/test.js index 3ac3f84..230ff83 100644 --- a/packages/rehype-minify-url/test.js +++ b/packages/rehype-minify-url/test.js @@ -87,6 +87,29 @@ test('rehype-minify-url', (t) => { u('root', [{type: 'element', tagName: 'a', children: []}]) ) + t.deepEqual( + rehype() + .use(min, options) + .runSync( + u('root', [ + { + type: 'element', + tagName: 'link', + properties: {href: options.from, rel: ['canonical']}, + children: [] + } + ]) + ), + u('root', [ + { + type: 'element', + tagName: 'link', + properties: {href: options.from, rel: ['canonical']}, + children: [] + } + ]) + ) + t.deepEqual( rehype() .use(() => (_, file) => {