From d8bd0865107c687a07e5385e4a91b3e92c43bca9 Mon Sep 17 00:00:00 2001 From: Zach Leatherman Date: Tue, 3 Sep 2024 11:58:45 -0500 Subject: [PATCH] Fixes #3424, adds filter callback --- src/Plugins/IdAttributePlugin.js | 20 ++++++-- test/IdAttributePluginTest.js | 78 ++++++++++++++++++++++++++++++-- 2 files changed, 90 insertions(+), 8 deletions(-) diff --git a/src/Plugins/IdAttributePlugin.js b/src/Plugins/IdAttributePlugin.js index df5e2a2fe..1e63f6f9c 100644 --- a/src/Plugins/IdAttributePlugin.js +++ b/src/Plugins/IdAttributePlugin.js @@ -27,25 +27,37 @@ function IdAttributePlugin(eleventyConfig, options = {}) { options.slugify = MemoizeUtil(slugifyFilter); } if (!options.selector) { - options.selector = "h1,h2,h3,h4,h5,h6"; + options.selector = "[id],h1,h2,h3,h4,h5,h6"; } options.decodeEntities = options.decodeEntities ?? true; eleventyConfig.htmlTransformer.addPosthtmlPlugin( "html", - function (/*pluginOptions = {}*/) { + function (pluginOptions = {}) { + if (typeof options.filter === "function") { + if (options.filter(pluginOptions) === false) { + return function () {}; + } + } + return function (tree) { // One per page let conflictCheck = {}; tree.match(matchHelper(options.selector), function (node) { - if (!node.attrs?.id && node.content) { + let id; + if (node.attrs?.id) { + id = node.attrs?.id; + } else if (!node.attrs?.id && node.content) { node.attrs = node.attrs || {}; let textContent = getTextNodeContent(node); if (options.decodeEntities) { textContent = decodeHTML(textContent); } - let id = options.slugify(textContent); + id = options.slugify(textContent); + } + + if (id) { if (conflictCheck[id]) { conflictCheck[id]++; id = `${id}-${conflictCheck[id]}`; diff --git a/test/IdAttributePluginTest.js b/test/IdAttributePluginTest.js index 6e969f9ca..ebd7e859f 100644 --- a/test/IdAttributePluginTest.js +++ b/test/IdAttributePluginTest.js @@ -12,8 +12,6 @@ test("Using the IdAttribute plugin #3356", async (t) => { }, }); - elev.disableLogger(); - let results = await elev.toJSON(); t.is(results[0].content, `

This is a heading

This is another heading

This is another heading

This is another heading

`); }); @@ -27,8 +25,80 @@ test("Using the IdAttribute plugin with escaped quoted text", async (t) => { }, }); - elev.disableLogger(); - let results = await elev.toJSON(); t.is(results[0].content.trim(), `

This is a "heading"

`); }); + +test("Issue #3424, id attribute conflicts (id attribute supplied first)", async (t) => { + let elev = new Eleventy("./test/stubs-virtual/", "./test/stubs-virtual/_site", { + config: function (eleventyConfig) { + eleventyConfig.addPlugin(IdAttributePlugin); + + eleventyConfig.addTemplate("test.html", `

Testing

`, {}); + }, + }); + + let results = await elev.toJSON(); + t.is(results[0].content.trim(), `

Testing

`); +}); + +test("Issue #3424, id attribute conflicts (id attribute supplied last)", async (t) => { + let elev = new Eleventy("./test/stubs-virtual/", "./test/stubs-virtual/_site", { + config: function (eleventyConfig) { + eleventyConfig.addPlugin(IdAttributePlugin); + + eleventyConfig.addTemplate("test.html", `

Testing

`, {}); + }, + }); + + let results = await elev.toJSON(); + t.is(results[0].content.trim(), `

Testing

`); +}); + +test("Issue #3424, id attribute conflicts (hard coded id conflicts)", async (t) => { + let elev = new Eleventy("./test/stubs-virtual/", "./test/stubs-virtual/_site", { + config: function (eleventyConfig) { + eleventyConfig.addPlugin(IdAttributePlugin); + + eleventyConfig.addTemplate("test.html", `

Testing

Testing

`, {}); + }, + }); + + let results = await elev.toJSON(); + t.is(results[0].content.trim(), `

Testing

Testing

`); +}); + +test("Issue #3424, id attribute conflicts (two hard coded id conflicts)", async (t) => { + let elev = new Eleventy("./test/stubs-virtual/", "./test/stubs-virtual/_site", { + config: function (eleventyConfig) { + eleventyConfig.addPlugin(IdAttributePlugin); + + eleventyConfig.addTemplate("test.html", `

Testing

Testing

`, {}); + }, + }); + + let results = await elev.toJSON(); + t.is(results[0].content.trim(), `

Testing

Testing

`); +}); + +test("Issue #3424, filter callback skips", async (t) => { + let elev = new Eleventy("./test/stubs-virtual/", "./test/stubs-virtual/_site", { + config: function (eleventyConfig) { + eleventyConfig.addPlugin(IdAttributePlugin, { + filter: function({ page }) { + if(page.inputPath.endsWith("test-skipped.html")) { + return false; + } + return true; + } + }); + + eleventyConfig.addTemplate("test.html", `

Testing

Testing

`, {}); + eleventyConfig.addTemplate("test-skipped.html", `

Testing

Testing

`, {}); + }, + }); + + let results = await elev.toJSON(); + t.is(results[0].content.trim(), `

Testing

Testing

`); + t.is(results[1].content.trim(), `

Testing

Testing

`); +});