Skip to content

Commit

Permalink
Merge branch 'main' into auto-copy-plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
zachleat authored Dec 11, 2024
2 parents e90e5ff + e38a287 commit fcbadd4
Show file tree
Hide file tree
Showing 7 changed files with 113 additions and 21 deletions.
6 changes: 3 additions & 3 deletions src/Errors/EleventyErrorHandler.js
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,9 @@ class EleventyErrorHandler {
while (ref) {
let nextRef = ref.originalError;

// Nunjucks wraps errors and puts the original in error.cause
if (nextRef?.cause?.originalError) {
nextRef = nextRef.cause.originalError;
// Unwrap cause from error and assign it to what Eleventy expects
if (nextRef?.cause) {
nextRef.originalError = nextRef.cause?.originalError ?? nextRef?.cause;
}

if (!nextRef && EleventyErrorUtil.hasEmbeddedError(ref.message)) {
Expand Down
7 changes: 4 additions & 3 deletions src/Plugins/IdAttributePlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,11 @@ function IdAttributePlugin(eleventyConfig, options = {}) {
} else if (options.checkDuplicates === "error") {
// Existing `id` conflicts with assigned heading id, throw error
throw new Error(
"Duplicate `id` attribute (" +
'You have more than one HTML `id` attribute using the same value (id="' +
id +
") in markup on " +
pluginOptions.page.inputPath,
'") in your template (' +
pluginOptions.page.inputPath +
"). You can disable this error in the IdAttribute plugin with the `checkDuplicates: false` option.",
);
}
} else {
Expand Down
38 changes: 26 additions & 12 deletions src/Plugins/InputPathToUrl.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ import { TemplatePath } from "@11ty/eleventy-utils";
import isValidUrl from "../Util/ValidUrl.js";

function getValidPath(contentMap, testPath) {
let normalized = TemplatePath.addLeadingDotSlash(testPath);
// if the path is coming from Markdown, it may be encoded
let normalized = TemplatePath.addLeadingDotSlash(decodeURIComponent(testPath));

// it must exist in the content map to be valid
if (contentMap[normalized]) {
Expand Down Expand Up @@ -45,6 +46,10 @@ function normalizeInputPath(targetInputPath, inputDir, sourceInputPath, contentM
}

function parseFilePath(filepath) {
if (filepath.startsWith("#") || filepath.startsWith("?")) {
return [filepath, ""];
}

try {
/* u: URL {
href: 'file:///tmpl.njk#anchor',
Expand All @@ -64,8 +69,8 @@ function parseFilePath(filepath) {
// Note that `node:url` -> pathToFileURL creates an absolute path, which we don’t want
// URL(`file:#anchor`) gives back a pathname of `/`
let u = new URL(`file:${filepath}`);
filepath = filepath.replace(u.search, "");
filepath = filepath.replace(u.hash, "");
filepath = filepath.replace(u.search, ""); // includes ?
filepath = filepath.replace(u.hash, ""); // includes #

return [
// search includes ?, hash includes #
Expand Down Expand Up @@ -95,8 +100,15 @@ function FilterPlugin(eleventyConfig) {
let inputDir = eleventyConfig.directories.input;
let suffix = "";
[suffix, targetFilePath] = parseFilePath(targetFilePath);
// @ts-ignore
targetFilePath = normalizeInputPath(targetFilePath, inputDir, this.page.inputPath, contentMap);
if (targetFilePath) {
targetFilePath = normalizeInputPath(
targetFilePath,
inputDir,
// @ts-ignore
this.page.inputPath,
contentMap,
);
}

let urls = contentMap[targetFilePath];
if (!urls || urls.length === 0) {
Expand Down Expand Up @@ -134,13 +146,15 @@ function TransformPlugin(eleventyConfig, defaultOptions = {}) {

let suffix = "";
[suffix, targetFilepathOrUrl] = parseFilePath(targetFilepathOrUrl);
targetFilepathOrUrl = normalizeInputPath(
targetFilepathOrUrl,
inputDir,
// @ts-ignore
this.page.inputPath,
contentMap,
);
if (targetFilepathOrUrl) {
targetFilepathOrUrl = normalizeInputPath(
targetFilepathOrUrl,
inputDir,
// @ts-ignore
this.page.inputPath,
contentMap,
);
}

let urls = contentMap[targetFilepathOrUrl];
if (!targetFilepathOrUrl || !urls || urls.length === 0) {
Expand Down
3 changes: 3 additions & 0 deletions src/Util/Require.js
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,9 @@ async function dynamicImportAbsolutePath(absolutePath, type, returnRaw = false)
if (key === "default") {
continue;
}
if (key === "module.exports") {
continue;
}
if (target[key] !== target.default[key]) {
match = false;
}
Expand Down
2 changes: 1 addition & 1 deletion test/IdAttributePluginTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ test("Issue #3424, id attribute conflicts (two hard coded id conflicts)", async
elev.disableLogger();

let e = await t.throwsAsync(() => elev.toJSON());
t.is(e.originalError.originalError.toString(), "Error: Duplicate `id` attribute (testing) in markup on ./test/stubs-virtual/test.html");
t.is(e.originalError.originalError.toString(), `Error: You have more than one HTML \`id\` attribute using the same value (id="testing") in your template (./test/stubs-virtual/test.html). You can disable this error in the IdAttribute plugin with the \`checkDuplicates: false\` option.`);
});

test("Issue #3424, filter callback skips", async (t) => {
Expand Down
72 changes: 72 additions & 0 deletions test/InputPathToUrlPluginTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -172,3 +172,75 @@ test("Issue #3417 Using the transform with relative path (no dot slash)", async
`<a href="/source/target/">Target</a>`
);
});

test("Issue #3581 #build-cost-🧰", async (t) => {
let elev = new Eleventy("./test/stubs-virtual/", "./test/stubs-virtual/_site", {
configPath: false,
config: function (eleventyConfig) {
eleventyConfig.addPlugin(TransformPlugin);

eleventyConfig.addTemplate("source/test.njk", `<a href="#built-cost-🧰">Target</a>`)
},
});

let results = await elev.toJSON();

t.is(
getContentFor(results, "/source/test/index.html"),
`<a href="#built-cost-🧰">Target</a>`
);
});

test("Issue #3583 Markdown diacritics", async (t) => {
let elev = new Eleventy("./test/stubs-virtual/", "./test/stubs-virtual/_site", {
configPath: false,
config: function (eleventyConfig) {
eleventyConfig.addTemplate("test.md", `[Target](</hypothèse/>)`)
},
});

let results = await elev.toJSON();

t.is(
getContentFor(results, "/test/index.html"),
`<p><a href="/hypoth%C3%A8se/">Target</a></p>`
);
});

test("Issue #3583 Diacritics", async (t) => {
let elev = new Eleventy("./test/stubs-virtual/", "./test/stubs-virtual/_site", {
configPath: false,
config: function (eleventyConfig) {
eleventyConfig.addPlugin(TransformPlugin);

eleventyConfig.addTemplate("test.md", `[Target](/hypothèse.md)`)
eleventyConfig.addTemplate("hypothèse.md", "lol")
},
});

let results = await elev.toJSON();

t.is(
getContentFor(results, "/test/index.html"),
`<p><a href="/hypothèse/">Target</a></p>`
);
});

test("Issue #3583 Diacritics Markdown raw", async (t) => {
let elev = new Eleventy("./test/stubs-virtual/", "./test/stubs-virtual/_site", {
configPath: false,
config: function (eleventyConfig) {
eleventyConfig.addPlugin(TransformPlugin);

eleventyConfig.addTemplate("test.md", `[Target](</hypothèse.md>)`)
eleventyConfig.addTemplate("hypothèse.md", "lol")
},
});

let results = await elev.toJSON();

t.is(
getContentFor(results, "/test/index.html"),
`<p><a href="/hypothèse/">Target</a></p>`
);
});
6 changes: 4 additions & 2 deletions test_node/MdxTest.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,14 @@
// See https://github.com/nodejs/node/issues/47747
import test from "node:test";
import assert from "node:assert";
import { register } from "node:module";
import module from "node:module";
import { renderToStaticMarkup } from "react-dom/server";

import Eleventy from "../src/Eleventy.js";

register("@mdx-js/node-loader", import.meta.url);
if ("register" in module) {
module.register("@mdx-js/node-loader", import.meta.url);
}

test("Eleventy with MDX", async () => {
let elev = new Eleventy("./test/stubs-fancyjs/test.mdx", undefined, {
Expand Down

0 comments on commit fcbadd4

Please sign in to comment.