Skip to content

Commit

Permalink
Adds remove method to HtmlTransformer, useful to downstream Bundle …
Browse files Browse the repository at this point in the history
…Plugin
  • Loading branch information
zachleat committed Jan 28, 2025
1 parent b662229 commit 463edc7
Show file tree
Hide file tree
Showing 5 changed files with 102 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/Plugins/IdAttributePlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { decodeHTML } from "entities";
import slugifyFilter from "../Filters/Slugify.js";
import MemoizeUtil from "../Util/MemoizeFunction.js";

const POSTHTML_PLUGIN_NAME = "11ty/eleventy/id-attribute";

function getTextNodeContent(node) {
if (node.attrs?.["eleventy:id-ignore"] === "") {
delete node.attrs["eleventy:id-ignore"];
Expand Down Expand Up @@ -38,7 +40,7 @@ function IdAttributePlugin(eleventyConfig, options = {}) {

eleventyConfig.htmlTransformer.addPosthtmlPlugin(
"html",
function (pluginOptions = {}) {
function idAttributePosthtmlPlugin(pluginOptions = {}) {
if (typeof options.filter === "function") {
if (options.filter(pluginOptions) === false) {
return function () {};
Expand Down Expand Up @@ -97,7 +99,11 @@ function IdAttributePlugin(eleventyConfig, options = {}) {
return node;
});
};
} /* , {} // pluginOptions */,
},
{
// pluginOptions
name: POSTHTML_PLUGIN_NAME,
},
);
}

Expand Down
24 changes: 24 additions & 0 deletions src/Util/ArrayUtil.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
export function arrayDelete(arr, match) {
if (!Array.isArray(arr)) {
return [];
}

if (!match) {
return arr;
}

// only mutates if found
if (typeof match === "function") {
if (arr.find(match)) {
return arr.filter((entry) => {
return !match(entry);
});
}
} else if (arr.includes(match)) {
return arr.filter((entry) => {
return entry !== match;
});
}

return arr;
}
15 changes: 15 additions & 0 deletions src/Util/HtmlTransformer.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,14 @@ import posthtml from "posthtml";
import urls from "@11ty/posthtml-urls";
import { FilePathUtil } from "./FilePathUtil.js";

import { arrayDelete } from "./ArrayUtil.js";

class HtmlTransformer {
// feature test for Eleventy Bundle Plugin
static SUPPORTS_PLUGINS_ENABLED_CALLBACK = true;

static TYPES = ["callbacks", "plugins"];

constructor() {
// execution order is important (not order of addition/object key order)
this.callbacks = {};
Expand Down Expand Up @@ -78,6 +82,8 @@ class HtmlTransformer {
}

target[ext].push({
// *could* fallback to function name, `value.name`
name: options.name, // for `remove` and debugging
fn: value, // callback or plugin
priority: options.priority, // sorted in descending order
enabled: options.enabled || (() => true),
Expand All @@ -92,6 +98,15 @@ class HtmlTransformer {
this._add(extensions, "plugins", plugin, options);
}

// match can be a plugin function or a filter callback(plugin => true);
remove(extensions, match) {
for (let removeType of HtmlTransformer.TYPES) {
for (let ext of (extensions || "").split(",")) {
this[removeType][ext] = arrayDelete(this[removeType][ext], match);
}
}
}

addUrlTransform(extensions, callback, options = {}) {
this._add(extensions, "callbacks", callback, options);
}
Expand Down
2 changes: 2 additions & 0 deletions src/defaultConfig.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ export default function (config) {
return this.existsCache.exists(filePath);
};

// Remember: the transform added here runs before the `htmlTransformer` transform
config.addPlugin(bundlePlugin, {
bundles: false, // no default bundles included—must be opt-in.
immediate: true,
Expand Down Expand Up @@ -128,6 +129,7 @@ export default function (config) {

// Run the `htmlTransformer` transform
config.addTransform("@11ty/eleventy/html-transformer", async function (content) {
// Runs **AFTER** the bundle plugin transform (except: delayed bundles)
return ut.transformContent(this.outputPath, content, this);
});

Expand Down
53 changes: 53 additions & 0 deletions test/ArrayUtilTest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import test from "ava";
import {arrayDelete} from "../src/Util/ArrayUtil.js";

test("ArrayUtil.arrayDelete empties", async (t) => {
t.deepEqual(arrayDelete(), []);
t.deepEqual(arrayDelete(undefined, 1), []);

t.deepEqual(arrayDelete(null), []);
t.deepEqual(arrayDelete(1), []);
t.deepEqual(arrayDelete(true), []);
t.deepEqual(arrayDelete(false), []);
});

test("ArrayUtil.arrayDelete if array does not have value, it does not mutate", async (t) => {
let empty = [];
t.is(arrayDelete(empty), empty);
t.is(arrayDelete(empty, 1), empty);
t.is(arrayDelete(empty, true), empty);
t.is(arrayDelete(empty, undefined), empty);
});

test("ArrayUtil.arrayDelete if array does not have function matched value, it does not mutate", async (t) => {
let empty = [];
t.is(arrayDelete(empty, () => false), empty);
});


test("ArrayUtil.arrayDelete mutates when array contains match", async (t) => {
let a = [1, 2];
t.not(arrayDelete(a, 1), [2]);
t.deepEqual(arrayDelete(a, 1), [2]);
});

test("ArrayUtil.arrayDelete mutates when array contains function matched value", async (t) => {
let a = [1, 2];
t.not(arrayDelete(a, entry => entry === 1), [2]);
t.deepEqual(arrayDelete(a, entry => entry === 1), [2]);
});

test("ArrayUtil.arrayDelete complex delete", async (t) => {
let a = [1,2,3,4,5,6,7,8];
t.deepEqual(arrayDelete(a, 4), [1,2,3,5,6,7,8]);
});

test("ArrayUtil.arrayDelete function matched delete", async (t) => {
let a = [1,2,3,4,5,6,7,8];
t.deepEqual(arrayDelete(a, entry => entry === 4), [1,2,3,5,6,7,8]);
});

test("ArrayUtil.arrayDelete double delete", async (t) => {
let a = [1,2,3,4,5,6,7,8];
t.deepEqual(arrayDelete(arrayDelete(a, 4), 6), [1,2,3,5,7,8]);
});

0 comments on commit 463edc7

Please sign in to comment.