-
-
Notifications
You must be signed in to change notification settings - Fork 501
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds support for arbitrary JavaScript front matter on top of existing…
… `js` engine. Also works with `javascript` #2819
- Loading branch information
Showing
4 changed files
with
64 additions
and
25 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
import { RetrieveGlobals } from "node-retrieve-globals"; | ||
|
||
// `javascript` Front Matter Type | ||
export default function (frontMatterCode, context = {}) { | ||
let { filePath } = context; | ||
|
||
// context.language would be nice as a guard, but was unreliable | ||
if (frontMatterCode.trimStart().startsWith("{")) { | ||
return context.engines.jsLegacy.parse(frontMatterCode, context); | ||
} | ||
|
||
let vm = new RetrieveGlobals(frontMatterCode, { | ||
filePath, | ||
// ignored if vm.Module is stable (or --experimental-vm-modules) | ||
transformEsmImports: true, | ||
}); | ||
|
||
// Future warning until vm.Module is stable: | ||
// If the frontMatterCode uses `import` this uses the `experimentalModuleApi` | ||
// option in node-retrieve-globals to workaround https://github.com/zachleat/node-retrieve-globals/issues/2 | ||
let data = { | ||
page: { | ||
// Theoretically fileSlug and filePathStem could be added here but require extensionMap | ||
inputPath: filePath, | ||
}, | ||
}; | ||
|
||
// this is async, but it’s handled in Eleventy upstream. | ||
return vm.getGlobalContext(data, { | ||
reuseGlobal: true, | ||
dynamicImport: true, | ||
// addRequire: true, | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
---javascript | ||
{ | ||
myFunction: function() { | ||
return "HELLO!"; | ||
} | ||
} | ||
--- | ||
<div>{{ myFunction() }}</div> |