@anydigital/11ty-bricks 1.0.0-alpha
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/LICENSE +21 -0
- package/README.md +116 -0
- package/package.json +44 -0
- package/src/autoRaw.js +28 -0
- package/src/autoRaw.test.js +87 -0
- package/src/index.cjs +17 -0
- package/src/index.js +24 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2025 Anton Staroverov
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,116 @@
|
|
|
1
|
+
# 11ty-bricks
|
|
2
|
+
|
|
3
|
+
A collection of helpful utilities and filters for Eleventy (11ty).
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
npm install @anydigital/11ty-bricks
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
You can use this library in two ways:
|
|
14
|
+
|
|
15
|
+
### Option 1: As a Plugin
|
|
16
|
+
|
|
17
|
+
Import and use the entire plugin. You can configure which helpers to enable using the options parameter:
|
|
18
|
+
|
|
19
|
+
**ES Modules:**
|
|
20
|
+
```javascript
|
|
21
|
+
import eleventyBricks from "@anydigital/11ty-bricks";
|
|
22
|
+
|
|
23
|
+
export default function(eleventyConfig) {
|
|
24
|
+
eleventyBricks(eleventyConfig, {
|
|
25
|
+
autoRaw: true // Enable autoRaw preprocessor (default: false)
|
|
26
|
+
});
|
|
27
|
+
|
|
28
|
+
// Your other configuration...
|
|
29
|
+
}
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
**CommonJS:**
|
|
33
|
+
```javascript
|
|
34
|
+
const eleventyBricks = require("@anydigital/11ty-bricks");
|
|
35
|
+
|
|
36
|
+
module.exports = function(eleventyConfig) {
|
|
37
|
+
eleventyBricks(eleventyConfig, {
|
|
38
|
+
autoRaw: true // Enable autoRaw preprocessor (default: false)
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
// Your other configuration...
|
|
42
|
+
};
|
|
43
|
+
```
|
|
44
|
+
|
|
45
|
+
### Option 2: Import Individual Helpers (Recommended)
|
|
46
|
+
|
|
47
|
+
Import only the specific helpers you need without using the plugin:
|
|
48
|
+
|
|
49
|
+
**ES Modules:**
|
|
50
|
+
```javascript
|
|
51
|
+
import { autoRaw } from "@anydigital/11ty-bricks";
|
|
52
|
+
|
|
53
|
+
export default function(eleventyConfig) {
|
|
54
|
+
autoRaw(eleventyConfig);
|
|
55
|
+
|
|
56
|
+
// Your other configuration...
|
|
57
|
+
}
|
|
58
|
+
```
|
|
59
|
+
|
|
60
|
+
**CommonJS:**
|
|
61
|
+
```javascript
|
|
62
|
+
const { autoRaw } = require("@anydigital/11ty-bricks");
|
|
63
|
+
|
|
64
|
+
module.exports = function(eleventyConfig) {
|
|
65
|
+
autoRaw(eleventyConfig);
|
|
66
|
+
|
|
67
|
+
// Your other configuration...
|
|
68
|
+
};
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
## Configuration Options
|
|
72
|
+
|
|
73
|
+
When using the plugin (Option 1), you can configure which helpers to enable:
|
|
74
|
+
|
|
75
|
+
| Option | Type | Default | Description |
|
|
76
|
+
|--------|------|---------|-------------|
|
|
77
|
+
| `autoRaw` | boolean | `false` | Enable the autoRaw preprocessor for Markdown files |
|
|
78
|
+
|
|
79
|
+
**Example:**
|
|
80
|
+
```javascript
|
|
81
|
+
eleventyBricks(eleventyConfig, {
|
|
82
|
+
autoRaw: true
|
|
83
|
+
});
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
## Available Helpers
|
|
87
|
+
|
|
88
|
+
### autoRaw
|
|
89
|
+
|
|
90
|
+
Prevents Nunjucks syntax from being processed in Markdown files by automatically wrapping `{{`, `}}`, `{%`, and `%}` with `{% raw %}` tags.
|
|
91
|
+
|
|
92
|
+
**Why use this?**
|
|
93
|
+
|
|
94
|
+
When writing documentation or tutorials about templating in Markdown files, you often want to show Nunjucks/Liquid syntax as literal text. This helper automatically escapes these special characters so they display as-is instead of being processed by the template engine.
|
|
95
|
+
|
|
96
|
+
**Example:**
|
|
97
|
+
|
|
98
|
+
Before `autoRaw`, writing this in Markdown:
|
|
99
|
+
```markdown
|
|
100
|
+
Use {{ variable }} to output variables.
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
Would try to process `{{ variable }}` as a template variable. With `autoRaw`, it displays exactly as written.
|
|
104
|
+
|
|
105
|
+
## Requirements
|
|
106
|
+
|
|
107
|
+
- Node.js >= 18.0.0
|
|
108
|
+
- Eleventy >= 2.0.0
|
|
109
|
+
|
|
110
|
+
## License
|
|
111
|
+
|
|
112
|
+
MIT
|
|
113
|
+
|
|
114
|
+
## Contributing
|
|
115
|
+
|
|
116
|
+
Contributions are welcome! Please feel free to submit a Pull Request.
|
package/package.json
ADDED
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@anydigital/11ty-bricks",
|
|
3
|
+
"version": "1.0.0-alpha",
|
|
4
|
+
"description": "A collection of helpful utilities and filters for Eleventy (11ty)",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"main": "./src/index.js",
|
|
7
|
+
"exports": {
|
|
8
|
+
".": {
|
|
9
|
+
"import": "./src/index.js",
|
|
10
|
+
"require": "./src/index.cjs"
|
|
11
|
+
}
|
|
12
|
+
},
|
|
13
|
+
"files": [
|
|
14
|
+
"src"
|
|
15
|
+
],
|
|
16
|
+
"scripts": {
|
|
17
|
+
"test": "node --test src/**/*.test.js"
|
|
18
|
+
},
|
|
19
|
+
"keywords": [
|
|
20
|
+
"11ty",
|
|
21
|
+
"eleventy",
|
|
22
|
+
"plugin",
|
|
23
|
+
"helpers",
|
|
24
|
+
"filters",
|
|
25
|
+
"markdown",
|
|
26
|
+
"nunjucks"
|
|
27
|
+
],
|
|
28
|
+
"author": "Anton Staroverov",
|
|
29
|
+
"license": "MIT",
|
|
30
|
+
"repository": {
|
|
31
|
+
"type": "git",
|
|
32
|
+
"url": "git+https://github.com/anydigital/11ty-bricks.git"
|
|
33
|
+
},
|
|
34
|
+
"bugs": {
|
|
35
|
+
"url": "https://github.com/anydigital/11ty-bricks/issues"
|
|
36
|
+
},
|
|
37
|
+
"homepage": "https://github.com/anydigital/11ty-bricks#readme",
|
|
38
|
+
"peerDependencies": {
|
|
39
|
+
"@11ty/eleventy": "^2.0.0 || ^3.0.0"
|
|
40
|
+
},
|
|
41
|
+
"engines": {
|
|
42
|
+
"node": ">=18.0.0"
|
|
43
|
+
}
|
|
44
|
+
}
|
package/src/autoRaw.js
ADDED
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Transform Nunjucks syntax in content by wrapping it with raw tags
|
|
3
|
+
*
|
|
4
|
+
* This function wraps Nunjucks syntax ({{, }}, {%, %}) with {% raw %} tags
|
|
5
|
+
* to prevent them from being processed by the template engine.
|
|
6
|
+
*
|
|
7
|
+
* @param {string} content - The content to transform
|
|
8
|
+
* @returns {string} The transformed content with Nunjucks syntax wrapped
|
|
9
|
+
*/
|
|
10
|
+
export function transformAutoRaw(content) {
|
|
11
|
+
// This regex looks for {{, }}, {%, or %} individually and wraps them
|
|
12
|
+
return content.replace(/({{|}}|{%|%})/g, "{% raw %}$1{% endraw %}");
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* autoRaw - Forbid Nunjucks processing in Markdown files
|
|
17
|
+
*
|
|
18
|
+
* This preprocessor wraps Nunjucks syntax ({{, }}, {%, %}) with {% raw %} tags
|
|
19
|
+
* to prevent them from being processed by the template engine in Markdown files.
|
|
20
|
+
*
|
|
21
|
+
* @param {Object} eleventyConfig - The Eleventy configuration object
|
|
22
|
+
*/
|
|
23
|
+
export function autoRaw(eleventyConfig) {
|
|
24
|
+
eleventyConfig.addPreprocessor("autoRaw", "md", (data, content) => {
|
|
25
|
+
return transformAutoRaw(content);
|
|
26
|
+
});
|
|
27
|
+
}
|
|
28
|
+
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import { describe, it } from "node:test";
|
|
2
|
+
import assert from "node:assert/strict";
|
|
3
|
+
import { transformAutoRaw } from "./autoRaw.js";
|
|
4
|
+
|
|
5
|
+
describe("transformAutoRaw", () => {
|
|
6
|
+
it("should wrap opening double curly braces with raw tags", () => {
|
|
7
|
+
const input = "Use {{ variable }} to output.";
|
|
8
|
+
const expected =
|
|
9
|
+
"Use {% raw %}{{{% endraw %} variable {% raw %}}}{% endraw %} to output.";
|
|
10
|
+
assert.equal(transformAutoRaw(input), expected);
|
|
11
|
+
});
|
|
12
|
+
|
|
13
|
+
it("should wrap closing double curly braces with raw tags", () => {
|
|
14
|
+
const input = "{{ name }}";
|
|
15
|
+
const expected =
|
|
16
|
+
"{% raw %}{{{% endraw %} name {% raw %}}}{% endraw %}";
|
|
17
|
+
assert.equal(transformAutoRaw(input), expected);
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
it("should wrap opening template tags with raw tags", () => {
|
|
21
|
+
const input = "{% if condition %}";
|
|
22
|
+
const expected =
|
|
23
|
+
"{% raw %}{%{% endraw %} if condition {% raw %}%}{% endraw %}";
|
|
24
|
+
assert.equal(transformAutoRaw(input), expected);
|
|
25
|
+
});
|
|
26
|
+
|
|
27
|
+
it("should wrap closing template tags with raw tags", () => {
|
|
28
|
+
const input = "{% endif %}";
|
|
29
|
+
const expected =
|
|
30
|
+
"{% raw %}{%{% endraw %} endif {% raw %}%}{% endraw %}";
|
|
31
|
+
assert.equal(transformAutoRaw(input), expected);
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
it("should handle multiple Nunjucks patterns in one string", () => {
|
|
35
|
+
const input = "{{ var1 }} and {% if test %} something {% endif %}";
|
|
36
|
+
const expected =
|
|
37
|
+
"{% raw %}{{{% endraw %} var1 {% raw %}}}{% endraw %} and {% raw %}{%{% endraw %} if test {% raw %}%}{% endraw %} something {% raw %}{%{% endraw %} endif {% raw %}%}{% endraw %}";
|
|
38
|
+
assert.equal(transformAutoRaw(input), expected);
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
it("should handle multiline content with Nunjucks syntax", () => {
|
|
42
|
+
const input = `# Title
|
|
43
|
+
{{ variable }}
|
|
44
|
+
Some text
|
|
45
|
+
{% for item in items %}
|
|
46
|
+
{{ item }}
|
|
47
|
+
{% endfor %}`;
|
|
48
|
+
const expected = `# Title
|
|
49
|
+
{% raw %}{{{% endraw %} variable {% raw %}}}{% endraw %}
|
|
50
|
+
Some text
|
|
51
|
+
{% raw %}{%{% endraw %} for item in items {% raw %}%}{% endraw %}
|
|
52
|
+
{% raw %}{{{% endraw %} item {% raw %}}}{% endraw %}
|
|
53
|
+
{% raw %}{%{% endraw %} endfor {% raw %}%}{% endraw %}`;
|
|
54
|
+
assert.equal(transformAutoRaw(input), expected);
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
it("should return unchanged content when no Nunjucks syntax is present", () => {
|
|
58
|
+
const input = "This is just plain text with no templates.";
|
|
59
|
+
assert.equal(transformAutoRaw(input), input);
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
it("should handle empty string", () => {
|
|
63
|
+
assert.equal(transformAutoRaw(""), "");
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
it("should handle content with only Nunjucks syntax", () => {
|
|
67
|
+
const input = "{{}}";
|
|
68
|
+
const expected =
|
|
69
|
+
"{% raw %}{{{% endraw %}{% raw %}}}{% endraw %}";
|
|
70
|
+
assert.equal(transformAutoRaw(input), expected);
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
it("should handle consecutive Nunjucks patterns", () => {
|
|
74
|
+
const input = "{{{{}}}}";
|
|
75
|
+
const expected =
|
|
76
|
+
"{% raw %}{{{% endraw %}{% raw %}{{{% endraw %}{% raw %}}}{% endraw %}{% raw %}}}{% endraw %}";
|
|
77
|
+
assert.equal(transformAutoRaw(input), expected);
|
|
78
|
+
});
|
|
79
|
+
|
|
80
|
+
it("should wrap each delimiter individually", () => {
|
|
81
|
+
const input = "Show {{ and }} and {% and %}";
|
|
82
|
+
const expected =
|
|
83
|
+
"Show {% raw %}{{{% endraw %} and {% raw %}}}{% endraw %} and {% raw %}{%{% endraw %} and {% raw %}%}{% endraw %}";
|
|
84
|
+
assert.equal(transformAutoRaw(input), expected);
|
|
85
|
+
});
|
|
86
|
+
});
|
|
87
|
+
|
package/src/index.cjs
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* CommonJS wrapper for 11ty Bricks Plugin
|
|
3
|
+
* Provides compatibility for projects using require()
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
// Dynamic import for ES modules
|
|
7
|
+
module.exports = async function eleventyBricksPlugin(eleventyConfig, options) {
|
|
8
|
+
const { default: plugin } = await import('./index.js');
|
|
9
|
+
return plugin(eleventyConfig, options);
|
|
10
|
+
};
|
|
11
|
+
|
|
12
|
+
// Export individual helpers
|
|
13
|
+
module.exports.autoRaw = async function(eleventyConfig) {
|
|
14
|
+
const { autoRaw } = await import('./index.js');
|
|
15
|
+
return autoRaw(eleventyConfig);
|
|
16
|
+
};
|
|
17
|
+
|
package/src/index.js
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
import { autoRaw } from "./autoRaw.js";
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* 11ty Bricks Plugin
|
|
5
|
+
*
|
|
6
|
+
* A collection of helpful utilities and filters for Eleventy (11ty).
|
|
7
|
+
* Can be used as a plugin or by importing individual helpers.
|
|
8
|
+
*
|
|
9
|
+
* @param {Object} eleventyConfig - The Eleventy configuration object
|
|
10
|
+
* @param {Object} options - Plugin options
|
|
11
|
+
* @param {boolean} options.autoRaw - Enable autoRaw preprocessor (default: false)
|
|
12
|
+
*/
|
|
13
|
+
export default function eleventyBricksPlugin(eleventyConfig, options = {}) {
|
|
14
|
+
const { autoRaw: enableAutoRaw = false } = options;
|
|
15
|
+
|
|
16
|
+
// Register helpers based on options
|
|
17
|
+
if (enableAutoRaw) {
|
|
18
|
+
autoRaw(eleventyConfig);
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
// Export individual helpers for granular usage
|
|
23
|
+
export { autoRaw };
|
|
24
|
+
|