-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
rewrite plugin to implement single and paired shortcodes as Vento tags
- Loading branch information
Showing
5 changed files
with
143 additions
and
33 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,38 @@ | ||
--- | ||
'eleventy-plugin-vento': major | ||
--- | ||
|
||
Adds complete support for Eleventy shortcodes and filters within Vento. Shortcodes are now loaded by default as Vento tags and will no longer be exposed as functions in your page data. | ||
|
||
The implementation of single shortcodes remains largely similar, just replace function-like calls with Vento tags. | ||
|
||
```diff | ||
- {{ nametag('Noel', 'Forte') }} | ||
+ {{ nametag 'Noel', 'Forte' }} | ||
``` | ||
|
||
Of course, the big news is that **paired** shortcodes are now officially supported by this plugin!! Prior to this release, paired shortcodes were exposed just like regular shortcodes, but were plain JS functions. With this release you can now use paired shortcodes just like any other tag. | ||
|
||
```hbs | ||
<!-- Before: everything is jammed into the function call :( --> | ||
{{ blockquote("<p>Call me Ishmael. Some years ago—never mind how long precisely—having little or no money in my purse, and nothing particular to interest me on shore, I thought I would sail about a little and see the watery part of the world.</p>\n<p>It is a way I have of driving off the spleen and regulating the circulation.</p>", "Herman Melville", "1851") }} | ||
<!-- After: opening and closing tags with arguments separated :) --> | ||
{{ blockquote 'Herman Melville', '1851' }} | ||
<p>Call me Ishmael. Some years ago—never mind how long precisely—having little or no money in my purse, and nothing particular to interest me on shore, I thought I would sail about a little and see the watery part of the world.</p> | ||
<p>It is a way I have of driving off the spleen and regulating the circulation.</p> | ||
{{ /blockquote }} | ||
``` | ||
|
||
This functionality became possible with the addition of the `getShortcodes()` and `getPairedShortcodes()` functions that were added in [Eleventy v3.0.0-alpha.15](https:/11ty/eleventy/releases/tag/v3.0.0-alpha.15). Because of dependence on these features, Eleventy 3.0.0-alpha.15 is now the **minimum required version** in order to use this plugin. | ||
|
||
Because these changes removed direct dependence on Eleventy JavaScript functions, the `addHelpers` option has been replaced with 3 new options: `shortcodes`, `pairedShortcodes` and `filters`. **All of them are enabled by default** but can be disabled in your plugin config like so. | ||
|
||
```diff | ||
eleventyConfig.addPlugin(VentoPlugin, { | ||
- addHelpers: false, | ||
+ shortcodes: false, | ||
+ pairedShortcodes: false, | ||
+ filters: false, | ||
}); | ||
``` |
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 was deleted.
Oops, something went wrong.
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,54 @@ | ||
/** | ||
* @file Helper function that creates vento tags from eleventy functions | ||
* | ||
* @param {string} name | ||
* @param {boolean} paired | ||
*/ | ||
|
||
export function createVentoTag(name, paired) { | ||
/** @type {import("ventojs/src/environment.js").Tag} */ | ||
const tag = (env, code, output, tokens) => { | ||
if (!code.startsWith(name)) return; | ||
|
||
// Declare helper object path strings | ||
const fn = `__env._11ty.functions.${name}`; | ||
const ctx = '__env._11ty.ctx'; | ||
let args = [code.replace(name, '').trim()]; | ||
|
||
const varname = output.startsWith('__shortcode_content') | ||
? `${output}_precomp` | ||
: '__shortcode_content'; | ||
|
||
// Create an array to hold compiled template code | ||
const compiled = []; | ||
|
||
if (paired) { | ||
args.unshift(env.compileFilters(tokens, varname, env.options.autoescape)); | ||
compiled.push( | ||
`{ // START paired-${name}`, | ||
`let ${varname} = "";`, | ||
...env.compileTokens(tokens, varname, [`/${name}`]) | ||
); | ||
if (tokens.length > 0 && (tokens[0][0] !== 'tag' || tokens[0][1] !== `/${name}`)) { | ||
throw new Error(`Missing closing tag for ${name} tag: ${code}`); | ||
} | ||
tokens.shift(); | ||
} | ||
|
||
// Compile arguments into a string | ||
args = args.some(Boolean) ? `, ${args.filter(Boolean).join(', ')}` : ''; | ||
|
||
compiled.push( | ||
`{ // START ${name}`, | ||
`const __shortcode_result = await ${fn}.call(${ctx + args});`, | ||
`${output} += ${env.compileFilters(tokens, '__shortcode_result', env.options.autoescape)}`, | ||
`} // END ${name}` | ||
); | ||
|
||
if (paired) compiled.push(`} // END paired-${name}`); | ||
|
||
return compiled.join('\n'); | ||
}; | ||
|
||
return Object.defineProperty(tag, 'name', { value: paired ? `${name}PairedTag` : `${name}Tag` }); | ||
} |