Skip to content

Commit

Permalink
Memoize the slug, slugify, and inputPathToUrl filters. #840
Browse files Browse the repository at this point in the history
  • Loading branch information
zachleat committed Jul 2, 2024
1 parent 1c2fbbd commit a9fe852
Show file tree
Hide file tree
Showing 5 changed files with 224 additions and 131 deletions.
46 changes: 25 additions & 21 deletions src/Plugins/InputPathToUrl.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { TemplatePath } from "@11ty/eleventy-utils";
import isValidUrl from "../Util/ValidUrl.js";
import memoize from "../Util/MemoizeFunction.js";

function normalizeInputPath(inputPath, inputDir, contentMap) {
// inputDir is optional at the beginning of the developer supplied-path
Expand Down Expand Up @@ -62,27 +63,30 @@ function FilterPlugin(eleventyConfig) {
contentMap = inputPathToUrl;
});

eleventyConfig.addFilter("inputPathToUrl", function (filepath) {
if (!contentMap) {
throw new Error("Internal error: contentMap not available for `inputPathToUrl` filter.");
}

if (isValidUrl(filepath)) {
return filepath;
}

let inputDir = eleventyConfig.directories.input;
let suffix = "";
[suffix, filepath] = parseFilePath(filepath);
filepath = normalizeInputPath(filepath, inputDir, contentMap);

let urls = contentMap[filepath];
if (!urls || urls.length === 0) {
throw new Error("`inputPathToUrl` filter could not find a matching target for " + filepath);
}

return `${urls[0]}${suffix}`;
});
eleventyConfig.addFilter(
"inputPathToUrl",
memoize(function (filepath) {
if (!contentMap) {
throw new Error("Internal error: contentMap not available for `inputPathToUrl` filter.");
}

if (isValidUrl(filepath)) {
return filepath;
}

let inputDir = eleventyConfig.directories.input;
let suffix = "";
[suffix, filepath] = parseFilePath(filepath);
filepath = normalizeInputPath(filepath, inputDir, contentMap);

let urls = contentMap[filepath];
if (!urls || urls.length === 0) {
throw new Error("`inputPathToUrl` filter could not find a matching target for " + filepath);
}

return `${urls[0]}${suffix}`;
}),
);
}

function TransformPlugin(eleventyConfig, defaultOptions = {}) {
Expand Down
Loading

0 comments on commit a9fe852

Please sign in to comment.