Skip to content

Commit

Permalink
mvp: contextualized filters, take 2
Browse files Browse the repository at this point in the history
  • Loading branch information
noelforte committed Jul 11, 2024
1 parent a055e7b commit 8697ce9
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 26 deletions.
16 changes: 10 additions & 6 deletions VentoPlugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,18 +75,22 @@ export function VentoPlugin(eleventyConfig, userOptions = {}) {
outputFileExtension: 'html',

async compile(inputContent, inputPath) {
return async (data) => {
return async function (data) {
if (options.useEleventyFeatures) {
// Add context to filters
for (const name in eleventyConfig.getFilters()) {
// Retrieve filter
const filter = eleventyConfig.getFilter(name);
const filterWithContext = function (...args) {
const fn = eleventyConfig.augmentFunctionContext(filter, { source: data });

return fn(...args);
// Wrap filter with a function that augments the filter function
// with the data from Vento to extract `page` and `eleventy`
// values, then returns a call to that filter with the original arguments
ventoEnv.filters[name] = function (...filterArguments) {
const filterWithContext = eleventyConfig.augmentFunctionContext(filter, {
source: this.data,
});
return filterWithContext(...filterArguments);
};

ventoEnv.filters[name] = filterWithContext;
}
}

Expand Down
7 changes: 1 addition & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
"ventojs": "0.12.8"
},
"devDependencies": {
"@11ty/eleventy": "3.0.0-alpha.14",
"@11ty/eleventy": "3.0.0-alpha.16",
"conventional-changelog-conventionalcommits": "8.0.0",
"eslint": "9.6.0",
"eslint-config-flat-gitignore": "0.1.7",
Expand All @@ -53,10 +53,5 @@
"singleQuote": true,
"semi": true,
"trailingComma": "es5"
},
"pnpm": {
"overrides": {
"@11ty/eleventy": "github:11ty/eleventy#26d26b7a4fe1e8a7a9431283ff437998a5468cf4"
}
}
}
25 changes: 12 additions & 13 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion tests/filters/filters.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ const results = await runBuild(import.meta.dirname, {
configApi.addFilter('wrapWith', (content, tag) => `<${tag}>${content}</${tag}>`);

configApi.addFilter('pageUrlCompare', function (url) {
console.log(this);
return url === this.page.url;
});
},
Expand Down
2 changes: 2 additions & 0 deletions tests/shortcodes/with-this.vto
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
{{ eleventyVersion }}
{{ page.url }}

0 comments on commit 8697ce9

Please sign in to comment.