noelforte / eleventy-plugin-vento

Eleventy plugin that adds support for Vento templates
https://www.npmjs.com/package/eleventy-plugin-vento
MIT License
4 stars 0 forks source link

Integrate Eleventy Universal Filters, Shortcodes, and Paired Shortcodes #9

Open noelforte opened 1 week ago

noelforte commented 1 week ago

With the addition of https://github.com/11ty/eleventy/issues/3310, it should now be possible to fully integrate Eleventy's template features (filters, shortcodes, paired shortcodes) into this plugin.

Only outstanding thing that still needs to be worked out is the appropriate way to add page context (this.page/this.eleventy) correctly. The approach in https://github.com/11ty/eleventy/issues/3081#issuecomment-2171831699 could work by extending the TemplateEngine class, or perhaps by doing some other sort of wheel-reinvention to achieve context availability in shortcodes, filters, etc.

zachleat commented 5 days ago

Sorry, I’m a little confused—isn’t this already implemented with https://github.com/noelforte/eleventy-plugin-vento/blob/6590ff60fcb81ef9ebd60dc5eb26ed87a7c1be5d/VentoPlugin.js#L97-L102 ?

zachleat commented 4 days ago

Ah, I think I see what you’re asking—you just want a less brittle way of doing it—right? I think we can accommodate that

noelforte commented 4 days ago

Yessir, if possible! Or at the very least an "official" recommendation on approach if you have one; even if that's "DIY, you're on your own" 😅

Looks like you do some call()s on the internal Liquid/Nunjucks engines to populate this correctly. If bind()/call() is the preferred recommendation to plugin authors for wiring up custom engines for adding page and eleventy context, that's totally fine for me to support on my end, just say the word and I'll stick with that.

zachleat commented 2 days ago

Related https://github.com/11ty/eleventy/issues/3355 let me know if that could be improved, I’m not super happy with it though it is less brittle than DIY

noelforte commented 21 hours ago

@zachleat first off thank you for offering your time to address this upstream. Here's some observations I've made after several hours of trying, failing, and finally succeeding in using augmentFunctionContext but I'm not sure if my solution was how you intended for the augmentor method to be used:

As you saw, I loop through all filters per template compiled and add context to each, I'm not sure whether there's a more efficient way to do it, but there it is. My first attempt was calling augmentFunctionContext with each filter, and then assigning that to Vento's filters object before compiling the template.

https://github.com/noelforte/eleventy-plugin-vento/blob/05f6f85fc54138b4548f442ecc9ac6c6512b6eaa/VentoPlugin.js#L81-L86

Regardless, that didn't work because this.page and this.eleventy were still left unpopulated; this containing the data cascade and the Vento engine env.

What did end up working for me was supplying data as the source option for the augmentor within the filter loop.

https://github.com/noelforte/eleventy-plugin-vento/blob/29de5e6de59e033df8fa3860a04dde5fbb3aa221/VentoPlugin.js#L81-L84

Again, I have no idea if this was how you imagined this method being used, and it's more than likely I'm missing something critical here. I reviewed the example you provided for Nunjucks and had trouble seeing how I would apply it to my use case. Only if you have the time, I'm curious to hear if where I arrived was what you envisioned.

Given the API differences from engine to engine in core, and that the implementation for shortcodes/filters differs with core vs plugins maybe it is simpler to just have custom engines handle this props DIY. The only downside I see being a scenerio where Eleventy introduces, changes, or deletes props on this for use in filters/shortcodes in the future and then it's up to plugin authors to re-establish feature parity. Not a huge problem since there's only 2 data keys on this for now. Parity and maintenence are why I brought this up to begin with.

Sorry for the lengthy response, hope it helps identify any improvements that could be made!