Skip to content

Commit

Permalink
Fixes #3424, adds filter callback
Browse files Browse the repository at this point in the history
  • Loading branch information
zachleat committed Sep 3, 2024
1 parent 5ce01f9 commit d8bd086
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 8 deletions.
20 changes: 16 additions & 4 deletions src/Plugins/IdAttributePlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -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]}`;
Expand Down
78 changes: 74 additions & 4 deletions test/IdAttributePluginTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@ test("Using the IdAttribute plugin #3356", async (t) => {
},
});

elev.disableLogger();

let results = await elev.toJSON();
t.is(results[0].content, `<h1 id="this-is-a-heading">This is a heading</h1><h2 id="already">This is another heading</h2><h2 id="this-is-another-heading">This is another heading</h2><h3 id="this-is-another-heading-2">This is another heading</h3>`);
});
Expand All @@ -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(), `<h1 id="this-is-a-heading">This is a <code>&quot;heading&quot;</code></h1>`);
});

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", `<div id="testing"></div><h1>Testing</h1>`, {});
},
});

let results = await elev.toJSON();
t.is(results[0].content.trim(), `<div id="testing"></div><h1 id="testing-2">Testing</h1>`);
});

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", `<h1>Testing</h1><div id="testing"></div>`, {});
},
});

let results = await elev.toJSON();
t.is(results[0].content.trim(), `<h1 id="testing">Testing</h1><div id="testing-2"></div>`);
});

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", `<h1>Testing</h1><h1 id="testing">Testing</h1>`, {});
},
});

let results = await elev.toJSON();
t.is(results[0].content.trim(), `<h1 id="testing">Testing</h1><h1 id="testing-2">Testing</h1>`);
});

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", `<h1 id="testing">Testing</h1><h1 id="testing">Testing</h1>`, {});
},
});

let results = await elev.toJSON();
t.is(results[0].content.trim(), `<h1 id="testing">Testing</h1><h1 id="testing-2">Testing</h1>`);
});

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", `<h1 id="testing">Testing</h1><h1 id="testing">Testing</h1>`, {});
eleventyConfig.addTemplate("test-skipped.html", `<h1 id="testing">Testing</h1><h1 id="testing">Testing</h1>`, {});
},
});

let results = await elev.toJSON();
t.is(results[0].content.trim(), `<h1 id="testing">Testing</h1><h1 id="testing-2">Testing</h1>`);
t.is(results[1].content.trim(), `<h1 id="testing">Testing</h1><h1 id="testing">Testing</h1>`);
});

0 comments on commit d8bd086

Please sign in to comment.