Skip to content

Commit

Permalink
Fixes #2819 with node front matter type
Browse files Browse the repository at this point in the history
  • Loading branch information
zachleat committed Nov 3, 2023
1 parent eee7719 commit 6360c0e
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 13 deletions.
2 changes: 2 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@
"@11ty/eleventy-plugin-syntaxhighlight": "^5.0.0",
"@iarna/toml": "^2.2.5",
"@vue/server-renderer": "^3.3.4",
"@zachleat/noop": "^1.0.3",
"ava": "^5.3.1",
"husky": "^8.0.3",
"js-yaml": "^4.1.0",
Expand Down Expand Up @@ -115,6 +116,7 @@
"minimist": "^1.2.8",
"moo": "^0.5.2",
"multimatch": "^6.0.0",
"node-retrieve-globals": "^3.0.1",
"normalize-path": "^3.0.0",
"nunjucks": "^3.2.4",
"please-upgrade-node": "^3.2.0",
Expand Down
22 changes: 12 additions & 10 deletions src/TemplateContent.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ class TemplateContent {
read: false,
render: false,
},
types
types,
);
}

Expand Down Expand Up @@ -142,7 +142,7 @@ class TemplateContent {
return this._frontMatter;
} else {
throw new Error(
"Unfortunately you’re using code that monkey patched some Eleventy internals and it isn’t async-friendly. Change your code to use the async `read()` method on the template instead!"
"Unfortunately you’re using code that monkey patched some Eleventy internals and it isn’t async-friendly. Change your code to use the async `read()` method on the template instead!",
);
}
}
Expand Down Expand Up @@ -175,11 +175,13 @@ class TemplateContent {
let options = this.config.frontMatterParsingOptions || {};
let fm;
try {
// Added in 3.0, passed along to front matter engines
options.filePath = this.inputPath;
fm = matter(content, options);
} catch (e) {
throw new TemplateContentFrontMatterError(
`Having trouble reading front matter from template ${this.inputPath}`,
e
e,
);
}

Expand Down Expand Up @@ -365,7 +367,7 @@ class TemplateContent {
key,
new Promise((resolve) => {
res = resolve;
})
}),
);
}
}
Expand All @@ -390,7 +392,7 @@ class TemplateContent {
debug(`Having trouble compiling template ${this.inputPath}: %O`, str);
throw new TemplateContentCompileError(
`Having trouble compiling template ${this.inputPath}`,
e
e,
);
}
}
Expand Down Expand Up @@ -483,7 +485,7 @@ class TemplateContent {
suffix.push(" (");
if (data.pagination.pages) {
suffix.push(
`${data.pagination.pages.length} page${data.pagination.pages.length !== 1 ? "s" : ""}`
`${data.pagination.pages.length} page${data.pagination.pages.length !== 1 ? "s" : ""}`,
);
} else {
suffix.push("Pagination");
Expand Down Expand Up @@ -512,7 +514,7 @@ class TemplateContent {
// Skip benchmark for each individual pagination entry (very busy output)
let logRenderToOutputBenchmark = "pagination" in data;
let inputPathBenchmark = this.bench.get(
`> Render > ${this.inputPath}${this._getPaginationLogSuffix(data)}`
`> Render > ${this.inputPath}${this._getPaginationLogSuffix(data)}`,
);
let outputPathBenchmark;
if (data.page && data.page.outputPath && logRenderToOutputBenchmark) {
Expand Down Expand Up @@ -547,7 +549,7 @@ class TemplateContent {
debug(`Having trouble rendering ${engine} template ${this.inputPath}: %O`, str);
throw new TemplateContentRenderError(
`Having trouble rendering ${engine} template ${this.inputPath}`,
e
e,
);
}
}
Expand All @@ -571,7 +573,7 @@ class TemplateContent {
"Test dependencies to see if %o is relevant to %o: %o",
this.inputPath,
incrementalFile,
isRelevant
isRelevant,
);

let extensionEntries = this.getExtensionEntries().filter((entry) => !!entry.isIncrementalMatch);
Expand All @@ -585,7 +587,7 @@ class TemplateContent {
isFileRelevantToInputPath: isRelevant,
doesFileHaveDependencies: hasDependencies,
},
incrementalFile
incrementalFile,
)
) {
return true;
Expand Down
26 changes: 23 additions & 3 deletions src/UserConfig.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import chalk from "kleur";
import { DateTime } from "luxon";
import debugUtil from "debug";
import { RetrieveGlobals } from "node-retrieve-globals";

import EventEmitter from "./Util/AsyncEventEmitter.js";
import EleventyCompatibility from "./Util/Compatibility.js";
import EleventyBaseError from "./EleventyBaseError.js";
import BenchmarkManager from "./BenchmarkManager.js";
import merge from "./Util/Merge.js";
import { DeepCopy } from "./Util/Merge.js";

const debug = debugUtil("Eleventy:UserConfig");

Expand Down Expand Up @@ -106,6 +107,24 @@ class UserConfig {
// Defaults in `defaultConfig.js`
this.dataFileSuffixesOverride = false;
this.dataFileDirBaseNameOverride = false;

this.frontMatterParsingOptions = {
engines: {
node: (frontMatterCode, { filePath }) => {
let vm = new RetrieveGlobals(frontMatterCode, {
filePath,
transformEsmImports: true,
});

let data = {}; // extra data
// this is async, but it’s handled in Eleventy upstream.
return vm.getGlobalContext(data, {
reuseGlobal: true,
dynamicImport: true,
});
},
},
};
}

// compatibleRange is optional in 2.0.0-beta.2
Expand Down Expand Up @@ -727,7 +746,7 @@ class UserConfig {
if (override) {
this.serverOptions = options;
} else {
this.serverOptions = merge(this.serverOptions, options);
this.serverOptions = DeepCopy(this.serverOptions, options);
}
}

Expand All @@ -746,8 +765,9 @@ class UserConfig {
this.watchThrottleWaitTime = time;
}

// 3.0 change: this does a top level merge instead of reset.
setFrontMatterParsingOptions(options = {}) {
this.frontMatterParsingOptions = options;
DeepCopy(this.frontMatterParsingOptions, options);
}

setQuietMode(quietMode) {
Expand Down
13 changes: 13 additions & 0 deletions test/JavaScriptFrontMatterTest.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import test from "ava";
import Eleventy from "../src/Eleventy.js";

test("Custom Front Matter Parsing Options (using JavaScript node-retrieve-globals)", async (t) => {
let elev = new Eleventy("./test/stubs/script-frontmatter/test.njk", "./_site");
elev.setIsVerbose(false);

let result = await elev.toJSON();

t.deepEqual(result.length, 1);

t.is(result[0]?.content, `<div>Hi</div><div>Bye</div>`);
});
8 changes: 8 additions & 0 deletions test/stubs/script-frontmatter/test.njk
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---node
import {noopSync} from "@zachleat/noop";
const myString = "Hi";

// export a function
function myFunction() { return "Bye" }
---
<div>{{ noopSync(myString) }}</div><div>{{ myFunction() }}</div>

0 comments on commit 6360c0e

Please sign in to comment.