Skip to content

Commit

Permalink
Fixes #52, changes failure case for permalink to return raw string
Browse files Browse the repository at this point in the history
  • Loading branch information
zachleat committed Feb 22, 2023
1 parent 56604ae commit c45e699
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 5 deletions.
19 changes: 14 additions & 5 deletions src/eleventyWebcTemplate.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
const path = require("path");
const debug = require("debug")("Eleventy:WebC");


const { EleventyRenderPlugin } = require("@11ty/eleventy");
const CompileString = EleventyRenderPlugin.String;
Expand Down Expand Up @@ -55,11 +57,18 @@ module.exports = function(eleventyConfig, options = {}) {
compileOptions: {
permalink: function(contents, inputPath) {
if(contents && typeof contents === "string") {
return (data) => {
return moduleScript.evaluateScript(contents, {
...this,
...data,
}, `Check the permalink for ${inputPath}`);
return async (data) => {
try {
// Hard to know if this is JavaScript code or just a raw string value.
let evaluatedString = await moduleScript.evaluateScript(contents, {
...this,
...data,
}, `Check the permalink for ${inputPath}`);
return evaluatedString;
} catch(e) {
debug("Error evaluating dynamic permalink, returning raw string contents instead: %o\n%O", contents, e);
return contents;
}
}
}

Expand Down
5 changes: 5 additions & 0 deletions test/permalink-string/eleventy.config.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
const EleventyWebcPlugin = require("../../eleventyWebcPlugin.js");

module.exports = function (eleventyConfig) {
eleventyConfig.addPlugin(EleventyWebcPlugin);
}
4 changes: 4 additions & 0 deletions test/permalink-string/page.webc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
permalink: "index.html"
---
Hello
4 changes: 4 additions & 0 deletions test/permalink-string/page2.webc
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
permalink: "`index2.html`"
---
Hello
12 changes: 12 additions & 0 deletions test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -482,3 +482,15 @@ Hello
</body>
</html>`);
});

test("Permalink string, issue #52", async t => {
let elev = new Eleventy("./test/permalink-string/", "./test/permalink-string/_site", {
configPath: "./test/permalink-string/eleventy.config.js"
});

let results = await elev.toJSON();
let [result1, result2] = results.sort((a, b) => a.inputPath < b.inputPath ? -1 : 1);

t.is(result1.url, "/");
t.is(result2.url, "index2.html");
});

0 comments on commit c45e699

Please sign in to comment.