@anydigital/11ty-bricks 1.0.0-alpha.7 → 1.0.0-alpha.8

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/README.md CHANGED
@@ -22,7 +22,7 @@ import eleventyBricks from "@anydigital/11ty-bricks";
22
22
 
23
23
  export default function(eleventyConfig) {
24
24
  eleventyConfig.addPlugin(eleventyBricks, {
25
- autoRawPreprocessor: true // Enable autoRaw preprocessor (default: false)
25
+ mdAutoRawTags: true // Enable mdAutoRawTags preprocessor (default: false)
26
26
  });
27
27
 
28
28
  // Your other configuration...
@@ -35,7 +35,7 @@ const eleventyBricks = require("@anydigital/11ty-bricks");
35
35
 
36
36
  module.exports = function(eleventyConfig) {
37
37
  eleventyConfig.addPlugin(eleventyBricks, {
38
- autoRawPreprocessor: true // Enable autoRaw preprocessor (default: false)
38
+ mdAutoRawTags: true // Enable mdAutoRawTags preprocessor (default: false)
39
39
  });
40
40
 
41
41
  // Your other configuration...
@@ -48,11 +48,11 @@ Import only the specific helpers you need without using the plugin:
48
48
 
49
49
  **ES Modules:**
50
50
  ```javascript
51
- import { bricksRegistry, autoRaw } from "@anydigital/11ty-bricks";
51
+ import { bricks, mdAutoRawTags } from "@anydigital/11ty-bricks";
52
52
 
53
53
  export default function(eleventyConfig) {
54
- bricksRegistry(eleventyConfig);
55
- autoRaw(eleventyConfig);
54
+ bricks(eleventyConfig);
55
+ mdAutoRawTags(eleventyConfig);
56
56
 
57
57
  // Your other configuration...
58
58
  }
@@ -60,11 +60,11 @@ export default function(eleventyConfig) {
60
60
 
61
61
  **CommonJS:**
62
62
  ```javascript
63
- const { bricksRegistry, autoRaw } = require("@anydigital/11ty-bricks");
63
+ const { bricks, mdAutoRawTags } = require("@anydigital/11ty-bricks");
64
64
 
65
65
  module.exports = function(eleventyConfig) {
66
- bricksRegistry(eleventyConfig);
67
- autoRaw(eleventyConfig);
66
+ bricks(eleventyConfig);
67
+ mdAutoRawTags(eleventyConfig);
68
68
 
69
69
  // Your other configuration...
70
70
  };
@@ -77,7 +77,8 @@ When using the plugin (Option 1), you can configure which helpers to enable:
77
77
  | Option | Type | Default | Description |
78
78
  |--------|------|---------|-------------|
79
79
  | `bricks` | boolean | `false` | Enable the bricks system for dependency management |
80
- | `autoRawPreprocessor` | boolean | `false` | Enable the autoRaw preprocessor for Markdown files |
80
+ | `mdAutoRawTags` | boolean | `false` | Enable the mdAutoRawTags preprocessor for Markdown files |
81
+ | `mdAutoNl2br` | boolean | `false` | Enable the mdAutoNl2br preprocessor to convert \n to `<br>` tags |
81
82
  | `fragments` | boolean | `false` | Enable the fragment shortcode for including content from fragments |
82
83
  | `setAttrFilter` | boolean | `false` | Enable the setAttr filter for overriding object attributes |
83
84
  | `byAttrFilter` | boolean | `false` | Enable the byAttr filter for filtering collections by attribute values |
@@ -86,41 +87,41 @@ When using the plugin (Option 1), you can configure which helpers to enable:
86
87
  ```javascript
87
88
  eleventyConfig.addPlugin(eleventyBricks, {
88
89
  bricks: true,
89
- autoRawPreprocessor: true,
90
+ mdAutoRawTags: true,
90
91
  byAttrFilter: true
91
92
  });
92
93
  ```
93
94
 
94
95
  ## Available 11ty Helpers
95
96
 
96
- ### bricksRegistry
97
+ ### bricks
97
98
 
98
99
  A dependency management system for Eleventy that automatically collects and injects CSS and JavaScript dependencies (both external and inline) per page. This allows brick components to declare their dependencies, and the system will inject them in the correct location in your HTML.
99
100
 
100
101
  **Why use this?**
101
102
 
102
- When building reusable components (bricks) in Eleventy, you often need to include CSS and JavaScript dependencies. Instead of manually adding these to every page, `bricksRegistry` automatically:
103
+ When building reusable components (bricks) in Eleventy, you often need to include CSS and JavaScript dependencies. Instead of manually adding these to every page, `bricks` automatically:
103
104
  - Collects dependencies from all bricks used on a page
104
105
  - Categorizes them (external CSS, external JS, inline styles, inline scripts)
105
106
  - Injects them in the correct location in your HTML output
106
107
 
107
108
  **How it works:**
108
109
 
109
- 1. Use the `bricksRegistry` shortcode in your base template to mark where dependencies should be injected
110
+ 1. Use the `bricksDependencies` shortcode in your base template to mark where dependencies should be injected
110
111
  2. Use the `brick` shortcode to register and render brick components that declare their dependencies
111
112
  3. The system automatically collects all dependencies and injects them when the page is built
112
113
 
113
114
  **Usage:**
114
115
 
115
- 1. Enable `bricksRegistry` in your Eleventy config:
116
+ 1. Enable `bricks` in your Eleventy config:
116
117
 
117
118
  ```javascript
118
- import { bricksRegistry } from "@anydigital/11ty-bricks";
119
+ import { bricks } from "@anydigital/11ty-bricks";
119
120
 
120
121
  export default function(eleventyConfig) {
121
- bricksRegistry(eleventyConfig);
122
+ bricks(eleventyConfig);
122
123
  // Or use as plugin:
123
- // eleventyConfig.addPlugin(eleventyBricks, { bricksRegistry: true });
124
+ // eleventyConfig.addPlugin(eleventyBricks, { bricks: true });
124
125
  }
125
126
  ```
126
127
 
@@ -198,22 +199,75 @@ The system will automatically inject all dependencies in the order they were reg
198
199
  - Works with both external URLs and inline code
199
200
  - Clears registry before each build to prevent stale data
200
201
 
201
- ### autoRaw
202
+ ### mdAutoRawTags
202
203
 
203
204
  Prevents Nunjucks syntax from being processed in Markdown files by automatically wrapping `{{`, `}}`, `{%`, and `%}` with `{% raw %}` tags.
204
205
 
205
206
  **Why use this?**
206
207
 
207
- 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.
208
+ When writing documentation or tutorials about templating in Markdown files, you often want to show Nunjucks/Liquid syntax as literal text. This preprocessor automatically escapes these special characters so they display as-is instead of being processed by the template engine.
209
+
210
+ **Usage:**
211
+
212
+ 1. Enable `mdAutoRawTags` in your Eleventy config:
213
+
214
+ ```javascript
215
+ import { mdAutoRawTags } from "@anydigital/11ty-bricks";
216
+
217
+ export default function(eleventyConfig) {
218
+ mdAutoRawTags(eleventyConfig);
219
+ // Or use as plugin:
220
+ // eleventyConfig.addPlugin(eleventyBricks, { mdAutoRawTags: true });
221
+ }
222
+ ```
208
223
 
209
224
  **Example:**
210
225
 
211
- Before `autoRaw`, writing this in Markdown:
226
+ Before `mdAutoRawTags`, writing this in Markdown:
212
227
  ```markdown
213
228
  Use {{ variable }} to output variables.
214
229
  ```
215
230
 
216
- Would try to process `{{ variable }}` as a template variable. With `autoRaw`, it displays exactly as written.
231
+ Would try to process `{{ variable }}` as a template variable. With `mdAutoRawTags`, it displays exactly as written.
232
+
233
+ ### mdAutoNl2br
234
+
235
+ Automatically converts `\n` sequences to `<br>` tags in Markdown content. This is particularly useful for adding line breaks inside Markdown tables where standard newlines don't work.
236
+
237
+ **Why use this?**
238
+
239
+ Markdown tables don't support multi-line content in cells. By using `\n` in your content, this preprocessor will convert it to `<br>` tags, allowing you to display line breaks within table cells and other content.
240
+
241
+ **Usage:**
242
+
243
+ 1. Enable `mdAutoNl2br` in your Eleventy config:
244
+
245
+ ```javascript
246
+ import { mdAutoNl2br } from "@anydigital/11ty-bricks";
247
+
248
+ export default function(eleventyConfig) {
249
+ mdAutoNl2br(eleventyConfig);
250
+ // Or use as plugin:
251
+ // eleventyConfig.addPlugin(eleventyBricks, { mdAutoNl2br: true });
252
+ }
253
+ ```
254
+
255
+ **Example:**
256
+
257
+ In your Markdown file:
258
+ ```markdown
259
+ | Column 1 | Column 2 |
260
+ |----------|----------|
261
+ | Line 1\nLine 2\nLine 3 | Another cell\nWith multiple lines |
262
+ ```
263
+
264
+ Will render as:
265
+ ```html
266
+ <td>Line 1<br>Line 2<br>Line 3</td>
267
+ <td>Another cell<br>With multiple lines</td>
268
+ ```
269
+
270
+ **Note:** This processes literal `\n` sequences (backslash followed by 'n'), not actual newline characters. Type `\n` in your source files where you want line breaks.
217
271
 
218
272
  ### fragment
219
273
 
@@ -446,7 +500,8 @@ Template usage:
446
500
 
447
501
  The plugin also exports the following for advanced usage:
448
502
 
449
- - `transformAutoRaw(content)`: The transform function used by `autoRaw` preprocessor. Can be used programmatically to wrap Nunjucks syntax with raw tags.
503
+ - `transformAutoRaw(content)`: The transform function used by `mdAutoRawTags` preprocessor. Can be used programmatically to wrap Nunjucks syntax with raw tags.
504
+ - `transformNl2br(content)`: The transform function used by `mdAutoNl2br` preprocessor. Can be used programmatically to convert `\n` sequences to `<br>` tags.
450
505
 
451
506
  ## CLI Helper Commands
452
507
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@anydigital/11ty-bricks",
3
- "version": "1.0.0-alpha.7",
3
+ "version": "1.0.0-alpha.8",
4
4
  "description": "A collection of helpful utilities and filters for Eleventy (11ty)",
5
5
  "type": "module",
6
6
  "main": "./src/index.js",
@@ -1,4 +1,4 @@
1
- export function bricksRegistry(eleventyConfig) {
1
+ export function bricks(eleventyConfig) {
2
2
 
3
3
  // Brick Registry System
4
4
  // Global registry to track dependencies per page
@@ -7,7 +7,7 @@
7
7
  *
8
8
  * @param {Object} eleventyConfig - The Eleventy configuration object
9
9
  */
10
- export function byAttr(eleventyConfig) {
10
+ export function byAttrFilter(eleventyConfig) {
11
11
  eleventyConfig.addFilter("byAttr", function(collection, attrName, targetValue) {
12
12
  if (!collection || !Array.isArray(collection)) {
13
13
  return [];
@@ -1,6 +1,6 @@
1
1
  import { describe, it } from 'node:test';
2
2
  import assert from 'node:assert';
3
- import { byAttr } from './byAttr.js';
3
+ import { byAttrFilter } from './byAttrFilter.js';
4
4
 
5
5
  describe('byAttr filter', () => {
6
6
  let filterFn;
@@ -15,7 +15,7 @@ describe('byAttr filter', () => {
15
15
  };
16
16
 
17
17
  // Register the filter
18
- byAttr(mockEleventyConfig);
18
+ byAttrFilter(mockEleventyConfig);
19
19
 
20
20
  it('should filter items by exact attribute match', () => {
21
21
  const collection = [
@@ -9,7 +9,7 @@ import { join } from "path";
9
9
  *
10
10
  * @param {Object} eleventyConfig - The Eleventy configuration object
11
11
  */
12
- export function fragment(eleventyConfig) {
12
+ export function fragments(eleventyConfig) {
13
13
  eleventyConfig.addShortcode("fragment", function(path) {
14
14
  // Get the input directory from Eleventy's context
15
15
  const inputDir = this.page?.inputPath
package/src/index.cjs CHANGED
@@ -9,24 +9,18 @@ module.exports = async function eleventyBricksPlugin(eleventyConfig, options) {
9
9
  return plugin(eleventyConfig, options);
10
10
  };
11
11
 
12
- // Export individual helpers
13
- module.exports.bricksRegistry = async function(eleventyConfig) {
14
- const { bricksRegistry } = await import('./index.js');
15
- return bricksRegistry(eleventyConfig);
16
- };
17
- module.exports.autoRaw = async function(eleventyConfig) {
18
- const { autoRaw } = await import('./index.js');
19
- return autoRaw(eleventyConfig);
20
- };
21
- module.exports.fragment = async function(eleventyConfig) {
22
- const { fragment } = await import('./index.js');
23
- return fragment(eleventyConfig);
24
- };
25
- module.exports.setAttr = async function(eleventyConfig) {
26
- const { setAttr } = await import('./index.js');
27
- return setAttr(eleventyConfig);
28
- };
29
- module.exports.byAttr = async function(eleventyConfig) {
30
- const { byAttr } = await import('./index.js');
31
- return byAttr(eleventyConfig);
32
- };
12
+ // Export individual helpers for granular usage
13
+ ['bricks', 'mdAutoRawTags', 'mdAutoNl2br', 'fragments', 'setAttrFilter', 'byAttrFilter'].forEach(name => {
14
+ module.exports[name] = async (eleventyConfig) => {
15
+ const module = await import('./index.js');
16
+ return module[name](eleventyConfig);
17
+ };
18
+ });
19
+
20
+ // Export transform functions for advanced usage
21
+ ['transformAutoRaw', 'transformNl2br'].forEach(name => {
22
+ module.exports[name] = async (content) => {
23
+ const module = await import('./index.js');
24
+ return module[name](content);
25
+ };
26
+ });
package/src/index.js CHANGED
@@ -1,8 +1,8 @@
1
- import { bricksRegistry } from "./bricksRegistry.js";
2
- import { autoRaw } from "./autoRaw.js";
3
- import { fragment } from "./fragment.js";
4
- import { setAttr } from "./setAttr.js";
5
- import { byAttr } from "./byAttr.js";
1
+ import { bricks } from "./bricks.js";
2
+ import { mdAutoRawTags, mdAutoNl2br, transformAutoRaw, transformNl2br } from "./markdown.js";
3
+ import { fragments } from "./fragments.js";
4
+ import { setAttrFilter } from "./setAttrFilter.js";
5
+ import { byAttrFilter } from "./byAttrFilter.js";
6
6
 
7
7
  /**
8
8
  * 11ty Bricks Plugin
@@ -13,32 +13,19 @@ import { byAttr } from "./byAttr.js";
13
13
  * @param {Object} eleventyConfig - The Eleventy configuration object
14
14
  * @param {Object} options - Plugin options
15
15
  * @param {boolean} options.bricks - Enable bricks system with dependencies injection (default: false)
16
- * @param {boolean} options.autoRawPreprocessor - Enable autoRaw preprocessor (default: false)
16
+ * @param {boolean} options.mdAutoRawTags - Enable mdAutoRawTags preprocessor (default: false)
17
+ * @param {boolean} options.mdAutoNl2br - Enable mdAutoNl2br for \n to <br> conversion (default: false)
17
18
  * @param {boolean} options.fragments - Enable fragment shortcode (default: false)
18
19
  * @param {boolean} options.setAttrFilter - Enable setAttr filter (default: false)
19
20
  * @param {boolean} options.byAttrFilter - Enable byAttr filter (default: false)
20
21
  */
21
22
  export default function eleventyBricksPlugin(eleventyConfig, options = {}) {
22
- if (options.bricks) {
23
- bricksRegistry(eleventyConfig);
24
- }
25
- if (options.autoRawPreprocessor) {
26
- autoRaw(eleventyConfig);
27
- }
28
- if (options.fragments) {
29
- fragment(eleventyConfig);
30
- }
31
- if (options.setAttrFilter) {
32
- setAttr(eleventyConfig);
33
- }
34
- if (options.byAttrFilter) {
35
- byAttr(eleventyConfig);
36
- }
23
+ const plugins = { bricks, mdAutoRawTags, mdAutoNl2br, fragments, setAttrFilter, byAttrFilter };
24
+ Object.entries(options).forEach(([key, enabled]) => enabled && plugins[key]?.(eleventyConfig));
37
25
  }
38
26
 
39
27
  // Export individual helpers for granular usage
40
- export { bricksRegistry };
41
- export { autoRaw };
42
- export { fragment };
43
- export { setAttr };
44
- export { byAttr };
28
+ export { bricks, mdAutoRawTags, mdAutoNl2br, fragments, setAttrFilter, byAttrFilter };
29
+
30
+ // Export transform functions for advanced usage
31
+ export { transformAutoRaw, transformNl2br };
@@ -0,0 +1,58 @@
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
+ * mdAutoRawTags - 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 mdAutoRawTags(eleventyConfig) {
24
+ eleventyConfig.addPreprocessor("mdAutoRawTags", "md", (data, content) => {
25
+ return transformAutoRaw(content);
26
+ });
27
+ }
28
+
29
+ /**
30
+ * Transform \n sequences to <br> tags
31
+ *
32
+ * This function converts literal \n sequences (double backslash + n) to HTML <br> tags.
33
+ * It handles both double \n\n and single \n sequences, processing double ones first.
34
+ *
35
+ * @param {string} content - The content to transform
36
+ * @returns {string} The transformed content with \n converted to <br>
37
+ */
38
+ export function transformNl2br(content) {
39
+ // Replace double \n\n first, then single \n to avoid double conversion
40
+ return content.replace(/\\n\\n/g, '<br>').replace(/\\n/g, '<br>');
41
+ }
42
+
43
+ /**
44
+ * mdAutoNl2br - Auto convert \n to <br> in markdown (especially tables)
45
+ *
46
+ * This function amends the markdown library to automatically convert \n
47
+ * to <br> tags in text content, which is particularly useful for line breaks
48
+ * inside markdown tables where standard newlines don't work.
49
+ *
50
+ * @param {Object} eleventyConfig - The Eleventy configuration object
51
+ */
52
+ export function mdAutoNl2br(eleventyConfig) {
53
+ eleventyConfig.amendLibrary("md", mdLib => {
54
+ mdLib.renderer.rules.text = (tokens, idx) => {
55
+ return transformNl2br(tokens[idx].content);
56
+ };
57
+ });
58
+ }
@@ -1,6 +1,6 @@
1
1
  import { describe, it } from "node:test";
2
2
  import assert from "node:assert/strict";
3
- import { transformAutoRaw } from "./autoRaw.js";
3
+ import { transformAutoRaw, transformNl2br } from "./markdown.js";
4
4
 
5
5
  describe("transformAutoRaw", () => {
6
6
  it("should wrap opening double curly braces with raw tags", () => {
@@ -85,3 +85,68 @@ Some text
85
85
  });
86
86
  });
87
87
 
88
+ describe("transformNl2br", () => {
89
+ it("should convert single \\n to <br>", () => {
90
+ const input = "Line 1\\nLine 2";
91
+ const expected = "Line 1<br>Line 2";
92
+ assert.equal(transformNl2br(input), expected);
93
+ });
94
+
95
+ it("should convert double \\n\\n to <br>", () => {
96
+ const input = "Line 1\\n\\nLine 2";
97
+ const expected = "Line 1<br>Line 2";
98
+ assert.equal(transformNl2br(input), expected);
99
+ });
100
+
101
+ it("should convert multiple \\n sequences", () => {
102
+ const input = "Line 1\\nLine 2\\nLine 3";
103
+ const expected = "Line 1<br>Line 2<br>Line 3";
104
+ assert.equal(transformNl2br(input), expected);
105
+ });
106
+
107
+ it("should handle mixed single and double \\n", () => {
108
+ const input = "Line 1\\n\\nLine 2\\nLine 3";
109
+ const expected = "Line 1<br>Line 2<br>Line 3";
110
+ assert.equal(transformNl2br(input), expected);
111
+ });
112
+
113
+ it("should handle text without \\n", () => {
114
+ const input = "Just plain text";
115
+ assert.equal(transformNl2br(input), input);
116
+ });
117
+
118
+ it("should handle empty content", () => {
119
+ assert.equal(transformNl2br(""), "");
120
+ });
121
+
122
+ it("should handle content with only \\n", () => {
123
+ const input = "\\n\\n\\n";
124
+ const expected = "<br><br>";
125
+ assert.equal(transformNl2br(input), expected);
126
+ });
127
+
128
+ it("should handle markdown table cell content with \\n", () => {
129
+ const input = "Cell 1\\nCell 1 Line 2\\n\\nCell 1 Line 3";
130
+ const expected = "Cell 1<br>Cell 1 Line 2<br>Cell 1 Line 3";
131
+ assert.equal(transformNl2br(input), expected);
132
+ });
133
+
134
+ it("should handle multiple consecutive double \\n\\n", () => {
135
+ const input = "Line 1\\n\\n\\n\\nLine 2";
136
+ const expected = "Line 1<br><br>Line 2";
137
+ assert.equal(transformNl2br(input), expected);
138
+ });
139
+
140
+ it("should preserve actual newlines (not literal \\n)", () => {
141
+ const input = "Line 1\nLine 2";
142
+ const expected = "Line 1\nLine 2";
143
+ assert.equal(transformNl2br(input), expected);
144
+ });
145
+
146
+ it("should only convert literal backslash-n sequences", () => {
147
+ const input = "Text with\\nbackslash-n and\nreal newline";
148
+ const expected = "Text with<br>backslash-n and\nreal newline";
149
+ assert.equal(transformNl2br(input), expected);
150
+ });
151
+ });
152
+
@@ -6,7 +6,7 @@
6
6
  *
7
7
  * @param {Object} eleventyConfig - The Eleventy configuration object
8
8
  */
9
- export function setAttr(eleventyConfig) {
9
+ export function setAttrFilter(eleventyConfig) {
10
10
  eleventyConfig.addFilter("setAttr", function(obj, key, value) {
11
11
  return {
12
12
  ...obj,
package/src/autoRaw.js DELETED
@@ -1,28 +0,0 @@
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
-