eleventy-plugin-vento 1.1.3 → 2.0.0-beta.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/VentoPlugin.js CHANGED
@@ -2,20 +2,27 @@
2
2
  * @file Adds support for the Vento templating language to Eleventy.
3
3
  *
4
4
  * @typedef VentoPluginOptions
5
- * @prop {Object<string,(...any) => any} [filters={}] An object containing methods that
6
- * will be loaded as filters into Vento.
7
5
  * @prop {Function[]} [plugins=[]] An array of plugins to load into vento.
8
- * @prop {import('ventojs').Options} [ventoOptions] Vento engine configuration object
9
- * that will be merged with default options.
10
6
  * @prop {string|boolean} [addHelpers=true]
11
7
  * Whether [Javascript Functions](https://www.11ty.dev/docs/languages/javascript/#javascript-template-functions)
12
8
  * should be merged into data provided to templates. If a string, functions will be namespaced under
13
9
  * a property with this name.
10
+ * @prop {boolean|string[]} [trimTags=false] Whether to automatically trim tags from output. Uses Vento's
11
+ * [autoTrim](https://vento.js.org/plugins/auto-trim/) plugin under the hood.
12
+ * @prop {import('ventojs').Options} [ventoOptions] Vento engine configuration object
13
+ * that will be merged with default options.
14
14
  */
15
15
 
16
16
  import path from 'node:path';
17
17
  import VentoJs from 'ventojs';
18
18
 
19
+ // Expose autotrim plugin defaults
20
+ import {
21
+ default as autoTrim,
22
+ defaultTags as ventoDefaultTrimTags,
23
+ } from 'ventojs/plugins/auto_trim.js';
24
+ export { ventoDefaultTrimTags };
25
+
19
26
  /**
20
27
  * @param {import('@11ty/eleventy/src/UserConfig.js').default} eleventyConfig
21
28
  * @param {VentoPluginOptions} options
@@ -28,10 +35,11 @@ export function VentoPlugin(eleventyConfig, options = {}) {
28
35
  }
29
36
 
30
37
  // Pull some bits from eleventyConfig and merge user-options with defaults
31
- const { dir, javascriptFunctions } = eleventyConfig;
38
+ const { dir, javascriptFunctions, liquidFilters, nunjucksFilters, nunjucksAsyncFilters } =
39
+ eleventyConfig;
32
40
  options = {
33
41
  addHelpers: true,
34
- filters: {},
42
+ trimTags: false,
35
43
  plugins: [],
36
44
  ventoOptions: {
37
45
  includes: path.join(dir.input, dir.includes),
@@ -43,37 +51,54 @@ export function VentoPlugin(eleventyConfig, options = {}) {
43
51
  // Init vento
44
52
  const env = VentoJs(options.ventoOptions);
45
53
 
46
- // Load user-defined filters into vento
47
- for (const filter in options.filters) env.filters[filter] = options.filters[filter];
54
+ // Create empty sets to hold user defined functions
55
+ const filters = {};
56
+ const helperFunctions = {};
57
+
58
+ for (const fn in javascriptFunctions) {
59
+ if (liquidFilters[fn] && (nunjucksFilters[fn] || nunjucksAsyncFilters[fn])) {
60
+ filters[fn] = javascriptFunctions[fn];
61
+ } else {
62
+ helperFunctions[fn] = javascriptFunctions[fn];
63
+ }
64
+ }
48
65
 
49
66
  // Load user-defined plugins into vento
50
67
  for (const plugin of options.plugins) env.use(plugin);
51
68
 
69
+ // Add autotrim plugin if enabled
70
+ if (options.trimTags) env.use(autoTrim({ tags: options.trimTags || ventoDefaultTrimTags }));
71
+
72
+ eleventyConfig.on('eleventy.before', () => {
73
+ env.cache.clear();
74
+ });
75
+
52
76
  // Add vto as a template format and create a custom template for it
53
77
  eleventyConfig.addTemplateFormats('vto');
54
78
  eleventyConfig.addExtension('vto', {
55
79
  outputFileExtension: 'html',
56
-
57
80
  async compile(inputContent, inputPath) {
58
- env.cache.clear();
59
81
  return async (data) => {
60
82
  if (options.addHelpers) {
61
83
  // Extract a possible namespace from the addHelpers option
62
84
  const namespace = options.addHelpers;
63
85
 
64
86
  // Rebind functions to page data so this.page, this.eleventy, etc works as intended
65
- const helpers = Object.assign(
66
- ...Object.entries(javascriptFunctions).map(([name, fn]) => ({ [name]: fn.bind(data) }))
67
- );
87
+ for (const helper in helperFunctions) {
88
+ helperFunctions[helper] = helperFunctions[helper].bind(data);
89
+ }
90
+
91
+ // Rebind filters the same way
92
+ for (const filter in filters) {
93
+ env.filters[filter] = filters[filter].bind(data);
94
+ }
68
95
 
69
96
  // Merge helpers into page data
70
- if (typeof namespace === 'string') data = { ...data, [namespace]: helpers };
71
- else data = { ...data, ...helpers };
97
+ if (typeof namespace === 'string') data = { ...data, [namespace]: helperFunctions };
98
+ else data = { ...data, ...helperFunctions };
72
99
  }
73
100
 
74
- const result = await env.runString(inputContent, data, inputPath);
75
-
76
- return result.content;
101
+ return await env.runString(inputContent, data, inputPath).then((result) => result.content);
77
102
  };
78
103
  },
79
104
  });
package/jsconfig.json CHANGED
@@ -1,6 +1,7 @@
1
1
  {
2
2
  "compilerOptions": {
3
3
  "module": "NodeNext",
4
- "moduleResolution": "NodeNext"
4
+ "moduleResolution": "NodeNext",
5
+ "baseUrl": "."
5
6
  }
6
7
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "eleventy-plugin-vento",
3
- "version": "1.1.3",
3
+ "version": "2.0.0-beta.1",
4
4
  "description": "Adds support for the Vento templating language to Eleventy.",
5
5
  "type": "module",
6
6
  "packageManager": "pnpm@9.1.4",
@@ -12,6 +12,9 @@
12
12
  "test-serve": "rm -rf test/output; cd test/input; eleventy --serve"
13
13
  },
14
14
  "main": "./VentoPlugin.js",
15
+ "exports": {
16
+ ".": "./VentoPlugin.js"
17
+ },
15
18
  "repository": "git@github.com:noelforte/eleventy-plugin-vento.git",
16
19
  "bugs": {
17
20
  "url": "https://github.com/noelforte/eleventy-plugin-vento/issues"
package/readme.md CHANGED
@@ -7,6 +7,7 @@ An [Eleventy](https://11ty.dev/) plugin that adds support for [Vento](https://ve
7
7
  [Plugin Options](#plugin-options)<br>
8
8
  [Vento Filters](#vento-filters)<br>
9
9
  [Vento Plugins](#vento-plugins)<br>
10
+ [Auto-Trimming Tags](#auto-trimming-tags)<br>
10
11
  [JavaScript Helpers](#javascript-helpers)<br>
11
12
 
12
13
  ## Installing
@@ -39,17 +40,17 @@ This plugin ships with default options out of the box, but you can [pass an opti
39
40
  import { VentoPlugin } from 'eleventy-plugin-vento';
40
41
 
41
42
  export default function (eleventyConfig) {
42
-
43
43
  eleventyConfig.addPlugin(VentoPlugin, {
44
- // A set of filters to load into Vento
45
- filters: {},
46
-
47
44
  // An array of Vento plugins to load
48
45
  plugins: [],
49
46
 
50
47
  // Define whether Javascript Functions should be added
51
48
  // to page data. See below for usage notes on this.
52
- addHelpers: true
49
+ addHelpers: true,
50
+
51
+ // Define tags that should be trimmed, or set to true
52
+ // to trim the default tags
53
+ trimTags: [],
53
54
 
54
55
  // A Vento configuration object
55
56
  ventoOptions: {
@@ -57,62 +58,74 @@ export default function (eleventyConfig) {
57
58
  autoescape: false,
58
59
  },
59
60
  });
60
-
61
61
  }
62
62
  ```
63
63
 
64
64
  View the [full list of options](https://vento.js.org/configuration/#options/) to pass as a Vento Configuration object (as `ventoOptions`).
65
65
 
66
- ## Vento Filters
66
+ ## Filters
67
67
 
68
- Set the `filters` property to an object with methods, these will be loaded as filters into Vento when Eleventy is run. Note that this feature exists **separately** from the concept of filters in Eleventy ([which are optionally accessible as data in Vento templates]()).
68
+ Filters that are added using Eleventy's `.addFilter()` or `.addAsyncFilter()` methods will be automatically loaded into Vento. Since Vento filters and Eleventy filters follow the same syntax for filters (content as first argument), the implementation is 1:1.
69
+
70
+ [More information about Vento filters](https://vento.js.org/configuration/#filters)
71
+
72
+ [More information about Eleventy filters](https://www.11ty.dev/docs/filters/)
73
+
74
+ ## Vento Plugins
69
75
 
70
- Here's an example of defining an `italicize` filter like the example in the Vento documentation:
76
+ If you'd like to extend the Vento library with any plugins, include them in an array passed to the `plugins` option.
71
77
 
72
78
  ```js
79
+ import { VentoPlugin } from 'eleventy-plugin-vento';
80
+
81
+ function myCustomPlugin() {
82
+ // ...plugin code...
83
+ }
84
+
73
85
  export default function (eleventyConfig) {
74
86
  eleventyConfig.addPlugin(VentoPlugin, {
75
- filters: {
76
- italicize(text) {
77
- return `<em>${text}</em>`;
78
- },
79
- },
87
+ plugins: [myCustomPlugin()],
80
88
  });
81
89
  }
82
90
  ```
83
91
 
84
- And now we can use our filter in a template:
92
+ ## Auto-Trimming Tags
85
93
 
86
- ```hbs
87
- <p>The title of my page is: "{{ title |> italicize }}"</p>
94
+ One exception to Vento plugins is the `autoTrim` plugin which is bundled with Vento, and by extension, this plugin. This plugin provides a convenient way to control whitespace in the output by collapsing spaces left behind when Vento's tags are removed.
95
+
96
+ To trim the default tags, set the `trimTags` plugin option to `true`.
97
+
98
+ ```js
99
+ eleventyConfig.addPlugin(VentoPlugin, {
100
+ trimTags: true,
101
+ });
88
102
  ```
89
103
 
90
- See https://vento.js.org/configuration/#filters for more information about filters and how they work.
104
+ The default set of tags that are trimmed are defined by Vento and are listed in the [usage notes](https://vento.js.org/plugins/auto-trim/#usage) for the auto trim plugin. To set your own tags, set `trimTags` to an array instead.
91
105
 
92
- ## Vento Plugins
106
+ ```js
107
+ eleventyConfig.addPlugin(VentoPlugin, {
108
+ trimTags: ['set', 'if', 'for'],
109
+ });
110
+ ```
93
111
 
94
- If you'd like to extend the Vento library with any plugins, include them in an array passed to the `plugins` option. Here's an example loading the [auto trim](https://vento.js.org/plugins/auto-trim/) plugin:
112
+ If instead, you'd like to extend instead of replace the default tag list, `eleventy-plugin-vento` reexports the `defaultTags` array used by as `ventoDefaultTrimTags`, which can be used like so:
95
113
 
96
114
  ```js
97
- import { VentoPlugin } from 'eleventy-plugin-vento';
98
- import autoTrim, { defaultTags } from 'ventojs/plugins/autotrim_plugin.js';
115
+ import { VentoPlugin, ventoDefaultTrimTags } from 'eleventy-plugin-vento';
99
116
 
100
- export default function (eleventyConfig) {
101
- eleventyConfig.addPlugin(VentoPlugin, {
102
- plugins: [
103
- autoTrim({
104
- tags: ['tag', ...defaultTags],
105
- }),
106
- ],
107
- });
108
- }
117
+ eleventyConfig.addPlugin(VentoPlugin, {
118
+ trimTags: [...ventoDefaultTrimTags, 'tag'],
119
+ });
109
120
  ```
110
121
 
122
+ For more information on the auto-trim plugin, consult the [plugin documentation](https://vento.js.org/plugins/auto-trim/).
123
+
111
124
  ## JavaScript Helpers
112
125
 
113
126
  Vento can [run arbitrary JavaScript](https://vento.js.org/syntax/javascript/) in templates through its `{{> ...}}` operator, which evaluates at the time the template is run. However, you may want to use Eleventy Shortcodes and Filters in your templates as well.
114
127
 
115
- By setting `addHelpers` in the plugin options (defaults to `true`), all JavaScript functions (which includes any Universal Filters and Shortcodes) are exposed as functions in page data. This feature functions very similarly to WebC's ["helper functions"](https://www.11ty.dev/docs/languages/javascript/#javascript-template-functions) feature.
128
+ By setting `addHelpers` in the plugin options (defaults to `true`), all JavaScript functions (which includes any Universal Shortcodes) are exposed as functions in page data. This feature functions very similarly to WebC's ["helper functions"](https://www.11ty.dev/docs/languages/javascript/#javascript-template-functions) feature.
116
129
 
117
130
  As an example:
118
131
 
@@ -155,3 +168,11 @@ addPlugin(VentoPlugin, { addHelpers: 'functions' });
155
168
  <p>This is something {{ functions.italicize('really') }} important.</p>
156
169
  <p>{{ functions.possumPosse() }}</p>
157
170
  ```
171
+
172
+ Finally, if you'd like to disable populating helper functions at all, set `addHelpers` to false:
173
+
174
+ ```js
175
+ eleventyConfig.addPlugin(VentoPlugin, {
176
+ addHelpers: false,
177
+ });
178
+ ```
@@ -1,5 +1,4 @@
1
- import { VentoPlugin } from '../../VentoPlugin.js';
2
- import autoTrim from 'ventojs/plugins/auto_trim.js';
1
+ import { VentoPlugin, ventoDefaultTrimTags } from 'eleventy-plugin-vento';
3
2
 
4
3
  export const config = {
5
4
  dir: {
@@ -10,13 +9,14 @@ export const config = {
10
9
 
11
10
  /** @param {import('@11ty/eleventy/src/UserConfig.js').default} eleventyConfig */
12
11
  export default async function (eleventyConfig) {
12
+ eleventyConfig.addFilter('italicize', (content) => `<em>${content}</em>`);
13
+
14
+ eleventyConfig.addFilter('appendURL', function (content) {
15
+ return `${content} ${this.page.url}`;
16
+ });
17
+
13
18
  eleventyConfig.addPlugin(VentoPlugin, {
14
- filters: {
15
- italicize(content) {
16
- return `<em>${content}</em>`;
17
- },
18
- },
19
- plugins: [autoTrim()],
19
+ trimTags: [...ventoDefaultTrimTags, 'for'],
20
20
  });
21
21
 
22
22
  eleventyConfig.addShortcode('possumPosse', () => 'Release the possums!!!');
@@ -0,0 +1,9 @@
1
+ ---
2
+ title: Page with filters
3
+ ---
4
+
5
+ <h1>{{ title }}</h1>
6
+
7
+ This is a page with a filter. {{ 'Page URL:' |> appendURL }}
8
+
9
+ Built with {{ eleventy.generator |> italicize }}
@@ -0,0 +1,19 @@
1
+ <!DOCTYPE html>
2
+ <html lang="en">
3
+ <head>
4
+ <meta charset="UTF-8">
5
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
6
+ <title>Document</title>
7
+ </head>
8
+ <body>
9
+ {{ set numbers = [1,2,3,4,5,6,7,8,9,10] }}
10
+ <main>
11
+ {{ for num of numbers }}
12
+ <section>
13
+ <h1>{{ num }}</h1>
14
+ <p>This is section {{ num }}</p>
15
+ </section>
16
+ {{ /for }}
17
+ </main>
18
+ </body>
19
+ </html>
@@ -1,7 +0,0 @@
1
- ---
2
- permalink: /test/page/permalink/
3
- title: Test page
4
- layout: page-layout.vto
5
- ---
6
-
7
- This content lives at `/test/page/permalink/`
@@ -1,6 +0,0 @@
1
- ---
2
- permalink: /
3
- layout: page-layout.vto
4
- ---
5
-
6
- This should render on the homepage.