@anydigital/11ty-bricks 1.0.0-alpha.7 → 1.0.0-alpha.9
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 +159 -26
- package/package.json +1 -1
- package/src/{bricksRegistry.js → bricks.js} +1 -1
- package/src/{byAttr.js → byAttrFilter.js} +1 -1
- package/src/{byAttr.test.js → byAttrFilter.test.js} +2 -2
- package/src/{fragment.js → fragments.js} +1 -1
- package/src/index.cjs +15 -21
- package/src/index.js +15 -26
- package/src/markdown.js +58 -0
- package/src/{autoRaw.test.js → markdown.test.js} +66 -1
- package/src/{setAttr.js → setAttrFilter.js} +1 -1
- package/src/siteData.js +12 -0
- package/src/autoRaw.js +0 -28
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
|
-
|
|
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
|
-
|
|
38
|
+
mdAutoRawTags: true // Enable mdAutoRawTags preprocessor (default: false)
|
|
39
39
|
});
|
|
40
40
|
|
|
41
41
|
// Your other configuration...
|
|
@@ -48,11 +48,12 @@ Import only the specific helpers you need without using the plugin:
|
|
|
48
48
|
|
|
49
49
|
**ES Modules:**
|
|
50
50
|
```javascript
|
|
51
|
-
import {
|
|
51
|
+
import { bricks, mdAutoRawTags, siteData } from "@anydigital/11ty-bricks";
|
|
52
52
|
|
|
53
53
|
export default function(eleventyConfig) {
|
|
54
|
-
|
|
55
|
-
|
|
54
|
+
bricks(eleventyConfig);
|
|
55
|
+
mdAutoRawTags(eleventyConfig);
|
|
56
|
+
siteData(eleventyConfig);
|
|
56
57
|
|
|
57
58
|
// Your other configuration...
|
|
58
59
|
}
|
|
@@ -60,11 +61,12 @@ export default function(eleventyConfig) {
|
|
|
60
61
|
|
|
61
62
|
**CommonJS:**
|
|
62
63
|
```javascript
|
|
63
|
-
const {
|
|
64
|
+
const { bricks, mdAutoRawTags, siteData } = require("@anydigital/11ty-bricks");
|
|
64
65
|
|
|
65
66
|
module.exports = function(eleventyConfig) {
|
|
66
|
-
|
|
67
|
-
|
|
67
|
+
bricks(eleventyConfig);
|
|
68
|
+
mdAutoRawTags(eleventyConfig);
|
|
69
|
+
siteData(eleventyConfig);
|
|
68
70
|
|
|
69
71
|
// Your other configuration...
|
|
70
72
|
};
|
|
@@ -77,50 +79,53 @@ When using the plugin (Option 1), you can configure which helpers to enable:
|
|
|
77
79
|
| Option | Type | Default | Description |
|
|
78
80
|
|--------|------|---------|-------------|
|
|
79
81
|
| `bricks` | boolean | `false` | Enable the bricks system for dependency management |
|
|
80
|
-
| `
|
|
82
|
+
| `mdAutoRawTags` | boolean | `false` | Enable the mdAutoRawTags preprocessor for Markdown files |
|
|
83
|
+
| `mdAutoNl2br` | boolean | `false` | Enable the mdAutoNl2br preprocessor to convert \n to `<br>` tags |
|
|
81
84
|
| `fragments` | boolean | `false` | Enable the fragment shortcode for including content from fragments |
|
|
82
85
|
| `setAttrFilter` | boolean | `false` | Enable the setAttr filter for overriding object attributes |
|
|
83
86
|
| `byAttrFilter` | boolean | `false` | Enable the byAttr filter for filtering collections by attribute values |
|
|
87
|
+
| `siteData` | boolean | `false` | Enable site.year and site.isProd global data |
|
|
84
88
|
|
|
85
89
|
**Example:**
|
|
86
90
|
```javascript
|
|
87
91
|
eleventyConfig.addPlugin(eleventyBricks, {
|
|
88
92
|
bricks: true,
|
|
89
|
-
|
|
90
|
-
byAttrFilter: true
|
|
93
|
+
mdAutoRawTags: true,
|
|
94
|
+
byAttrFilter: true,
|
|
95
|
+
siteData: true
|
|
91
96
|
});
|
|
92
97
|
```
|
|
93
98
|
|
|
94
99
|
## Available 11ty Helpers
|
|
95
100
|
|
|
96
|
-
###
|
|
101
|
+
### bricks
|
|
97
102
|
|
|
98
103
|
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
104
|
|
|
100
105
|
**Why use this?**
|
|
101
106
|
|
|
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, `
|
|
107
|
+
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
108
|
- Collects dependencies from all bricks used on a page
|
|
104
109
|
- Categorizes them (external CSS, external JS, inline styles, inline scripts)
|
|
105
110
|
- Injects them in the correct location in your HTML output
|
|
106
111
|
|
|
107
112
|
**How it works:**
|
|
108
113
|
|
|
109
|
-
1. Use the `
|
|
114
|
+
1. Use the `bricksDependencies` shortcode in your base template to mark where dependencies should be injected
|
|
110
115
|
2. Use the `brick` shortcode to register and render brick components that declare their dependencies
|
|
111
116
|
3. The system automatically collects all dependencies and injects them when the page is built
|
|
112
117
|
|
|
113
118
|
**Usage:**
|
|
114
119
|
|
|
115
|
-
1. Enable `
|
|
120
|
+
1. Enable `bricks` in your Eleventy config:
|
|
116
121
|
|
|
117
122
|
```javascript
|
|
118
|
-
import {
|
|
123
|
+
import { bricks } from "@anydigital/11ty-bricks";
|
|
119
124
|
|
|
120
125
|
export default function(eleventyConfig) {
|
|
121
|
-
|
|
126
|
+
bricks(eleventyConfig);
|
|
122
127
|
// Or use as plugin:
|
|
123
|
-
// eleventyConfig.addPlugin(eleventyBricks, {
|
|
128
|
+
// eleventyConfig.addPlugin(eleventyBricks, { bricks: true });
|
|
124
129
|
}
|
|
125
130
|
```
|
|
126
131
|
|
|
@@ -198,22 +203,75 @@ The system will automatically inject all dependencies in the order they were reg
|
|
|
198
203
|
- Works with both external URLs and inline code
|
|
199
204
|
- Clears registry before each build to prevent stale data
|
|
200
205
|
|
|
201
|
-
###
|
|
206
|
+
### mdAutoRawTags
|
|
202
207
|
|
|
203
208
|
Prevents Nunjucks syntax from being processed in Markdown files by automatically wrapping `{{`, `}}`, `{%`, and `%}` with `{% raw %}` tags.
|
|
204
209
|
|
|
205
210
|
**Why use this?**
|
|
206
211
|
|
|
207
|
-
When writing documentation or tutorials about templating in Markdown files, you often want to show Nunjucks/Liquid syntax as literal text. This
|
|
212
|
+
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.
|
|
213
|
+
|
|
214
|
+
**Usage:**
|
|
215
|
+
|
|
216
|
+
1. Enable `mdAutoRawTags` in your Eleventy config:
|
|
217
|
+
|
|
218
|
+
```javascript
|
|
219
|
+
import { mdAutoRawTags } from "@anydigital/11ty-bricks";
|
|
220
|
+
|
|
221
|
+
export default function(eleventyConfig) {
|
|
222
|
+
mdAutoRawTags(eleventyConfig);
|
|
223
|
+
// Or use as plugin:
|
|
224
|
+
// eleventyConfig.addPlugin(eleventyBricks, { mdAutoRawTags: true });
|
|
225
|
+
}
|
|
226
|
+
```
|
|
208
227
|
|
|
209
228
|
**Example:**
|
|
210
229
|
|
|
211
|
-
Before `
|
|
230
|
+
Before `mdAutoRawTags`, writing this in Markdown:
|
|
212
231
|
```markdown
|
|
213
232
|
Use {{ variable }} to output variables.
|
|
214
233
|
```
|
|
215
234
|
|
|
216
|
-
Would try to process `{{ variable }}` as a template variable. With `
|
|
235
|
+
Would try to process `{{ variable }}` as a template variable. With `mdAutoRawTags`, it displays exactly as written.
|
|
236
|
+
|
|
237
|
+
### mdAutoNl2br
|
|
238
|
+
|
|
239
|
+
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.
|
|
240
|
+
|
|
241
|
+
**Why use this?**
|
|
242
|
+
|
|
243
|
+
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.
|
|
244
|
+
|
|
245
|
+
**Usage:**
|
|
246
|
+
|
|
247
|
+
1. Enable `mdAutoNl2br` in your Eleventy config:
|
|
248
|
+
|
|
249
|
+
```javascript
|
|
250
|
+
import { mdAutoNl2br } from "@anydigital/11ty-bricks";
|
|
251
|
+
|
|
252
|
+
export default function(eleventyConfig) {
|
|
253
|
+
mdAutoNl2br(eleventyConfig);
|
|
254
|
+
// Or use as plugin:
|
|
255
|
+
// eleventyConfig.addPlugin(eleventyBricks, { mdAutoNl2br: true });
|
|
256
|
+
}
|
|
257
|
+
```
|
|
258
|
+
|
|
259
|
+
**Example:**
|
|
260
|
+
|
|
261
|
+
In your Markdown file:
|
|
262
|
+
```markdown
|
|
263
|
+
| Column 1 | Column 2 |
|
|
264
|
+
|----------|----------|
|
|
265
|
+
| Line 1\nLine 2\nLine 3 | Another cell\nWith multiple lines |
|
|
266
|
+
```
|
|
267
|
+
|
|
268
|
+
Will render as:
|
|
269
|
+
```html
|
|
270
|
+
<td>Line 1<br>Line 2<br>Line 3</td>
|
|
271
|
+
<td>Another cell<br>With multiple lines</td>
|
|
272
|
+
```
|
|
273
|
+
|
|
274
|
+
**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
275
|
|
|
218
276
|
### fragment
|
|
219
277
|
|
|
@@ -228,13 +286,13 @@ Fragments allow you to organize reusable content snippets in a dedicated directo
|
|
|
228
286
|
|
|
229
287
|
**Usage:**
|
|
230
288
|
|
|
231
|
-
1. Enable `
|
|
289
|
+
1. Enable `fragments` in your Eleventy config:
|
|
232
290
|
|
|
233
291
|
```javascript
|
|
234
|
-
import {
|
|
292
|
+
import { fragments } from "@anydigital/11ty-bricks";
|
|
235
293
|
|
|
236
294
|
export default function(eleventyConfig) {
|
|
237
|
-
|
|
295
|
+
fragments(eleventyConfig);
|
|
238
296
|
// Or use as plugin:
|
|
239
297
|
// eleventyConfig.addPlugin(eleventyBricks, { fragments: true });
|
|
240
298
|
}
|
|
@@ -442,11 +500,86 @@ Template usage:
|
|
|
442
500
|
{% set recentBlogPosts = collections.all | byAttr('category', 'blog') | reverse | limit(5) %}
|
|
443
501
|
```
|
|
444
502
|
|
|
503
|
+
### siteData
|
|
504
|
+
|
|
505
|
+
Adds global site data to your Eleventy project, providing commonly needed values that can be accessed in all templates.
|
|
506
|
+
|
|
507
|
+
**Why use this?**
|
|
508
|
+
|
|
509
|
+
Many websites need access to the current year (for copyright notices) and environment information (to conditionally enable features based on production vs development). This helper provides these as global `site` data without manually setting them up.
|
|
510
|
+
|
|
511
|
+
**Usage:**
|
|
512
|
+
|
|
513
|
+
1. Enable `siteData` in your Eleventy config:
|
|
514
|
+
|
|
515
|
+
```javascript
|
|
516
|
+
import { siteData } from "@anydigital/11ty-bricks";
|
|
517
|
+
|
|
518
|
+
export default function(eleventyConfig) {
|
|
519
|
+
siteData(eleventyConfig);
|
|
520
|
+
// Or use as plugin:
|
|
521
|
+
// eleventyConfig.addPlugin(eleventyBricks, { siteData: true });
|
|
522
|
+
}
|
|
523
|
+
```
|
|
524
|
+
|
|
525
|
+
2. Use the global data in your templates:
|
|
526
|
+
|
|
527
|
+
**Current Year:**
|
|
528
|
+
```njk
|
|
529
|
+
<footer>
|
|
530
|
+
<p>© {{ site.year }} Your Company Name. All rights reserved.</p>
|
|
531
|
+
</footer>
|
|
532
|
+
```
|
|
533
|
+
|
|
534
|
+
**Environment Check:**
|
|
535
|
+
```njk
|
|
536
|
+
{% if site.isProd %}
|
|
537
|
+
<!-- Production-only features -->
|
|
538
|
+
<script async src="https://www.googletagmanager.com/gtag/js?id=GA_TRACKING_ID"></script>
|
|
539
|
+
{% else %}
|
|
540
|
+
<!-- Development-only features -->
|
|
541
|
+
<div class="dev-toolbar">Development Mode</div>
|
|
542
|
+
{% endif %}
|
|
543
|
+
```
|
|
544
|
+
|
|
545
|
+
**Available Data:**
|
|
546
|
+
|
|
547
|
+
- `site.year`: The current year as a number (e.g., `2026`)
|
|
548
|
+
- `site.isProd`: Boolean indicating if running in production mode (`true` for `eleventy build`, `false` for `eleventy serve`)
|
|
549
|
+
|
|
550
|
+
**Features:**
|
|
551
|
+
|
|
552
|
+
- Automatically updates the year value
|
|
553
|
+
- Detects production vs development mode based on `ELEVENTY_RUN_MODE` environment variable
|
|
554
|
+
- Available globally in all templates without manual setup
|
|
555
|
+
- No configuration required
|
|
556
|
+
|
|
557
|
+
**Examples:**
|
|
558
|
+
|
|
559
|
+
```njk
|
|
560
|
+
{# Copyright notice #}
|
|
561
|
+
<p>Copyright © {{ site.year }} My Site</p>
|
|
562
|
+
|
|
563
|
+
{# Conditional loading of analytics #}
|
|
564
|
+
{% if site.isProd %}
|
|
565
|
+
<script src="/analytics.js"></script>
|
|
566
|
+
{% endif %}
|
|
567
|
+
|
|
568
|
+
{# Different behavior in dev vs prod #}
|
|
569
|
+
{% if site.isProd %}
|
|
570
|
+
<link rel="stylesheet" href="/css/styles.min.css">
|
|
571
|
+
{% else %}
|
|
572
|
+
<link rel="stylesheet" href="/css/styles.css">
|
|
573
|
+
<script src="/live-reload.js"></script>
|
|
574
|
+
{% endif %}
|
|
575
|
+
```
|
|
576
|
+
|
|
445
577
|
### Additional Exports
|
|
446
578
|
|
|
447
579
|
The plugin also exports the following for advanced usage:
|
|
448
580
|
|
|
449
|
-
- `transformAutoRaw(content)`: The transform function used by `
|
|
581
|
+
- `transformAutoRaw(content)`: The transform function used by `mdAutoRawTags` preprocessor. Can be used programmatically to wrap Nunjucks syntax with raw tags.
|
|
582
|
+
- `transformNl2br(content)`: The transform function used by `mdAutoNl2br` preprocessor. Can be used programmatically to convert `\n` sequences to `<br>` tags.
|
|
450
583
|
|
|
451
584
|
## CLI Helper Commands
|
|
452
585
|
|
package/package.json
CHANGED
|
@@ -7,7 +7,7 @@
|
|
|
7
7
|
*
|
|
8
8
|
* @param {Object} eleventyConfig - The Eleventy configuration object
|
|
9
9
|
*/
|
|
10
|
-
export function
|
|
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 {
|
|
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
|
-
|
|
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
|
|
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
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
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', 'siteData'].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,9 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import {
|
|
3
|
-
import {
|
|
4
|
-
import {
|
|
5
|
-
import {
|
|
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
|
+
import { siteData } from "./siteData.js";
|
|
6
7
|
|
|
7
8
|
/**
|
|
8
9
|
* 11ty Bricks Plugin
|
|
@@ -13,32 +14,20 @@ import { byAttr } from "./byAttr.js";
|
|
|
13
14
|
* @param {Object} eleventyConfig - The Eleventy configuration object
|
|
14
15
|
* @param {Object} options - Plugin options
|
|
15
16
|
* @param {boolean} options.bricks - Enable bricks system with dependencies injection (default: false)
|
|
16
|
-
* @param {boolean} options.
|
|
17
|
+
* @param {boolean} options.mdAutoRawTags - Enable mdAutoRawTags preprocessor (default: false)
|
|
18
|
+
* @param {boolean} options.mdAutoNl2br - Enable mdAutoNl2br for \n to <br> conversion (default: false)
|
|
17
19
|
* @param {boolean} options.fragments - Enable fragment shortcode (default: false)
|
|
18
20
|
* @param {boolean} options.setAttrFilter - Enable setAttr filter (default: false)
|
|
19
21
|
* @param {boolean} options.byAttrFilter - Enable byAttr filter (default: false)
|
|
22
|
+
* @param {boolean} options.siteData - Enable site.year global data (default: false)
|
|
20
23
|
*/
|
|
21
24
|
export default function eleventyBricksPlugin(eleventyConfig, options = {}) {
|
|
22
|
-
|
|
23
|
-
|
|
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
|
-
}
|
|
25
|
+
const plugins = { bricks, mdAutoRawTags, mdAutoNl2br, fragments, setAttrFilter, byAttrFilter, siteData };
|
|
26
|
+
Object.entries(options).forEach(([key, enabled]) => enabled && plugins[key]?.(eleventyConfig));
|
|
37
27
|
}
|
|
38
28
|
|
|
39
29
|
// Export individual helpers for granular usage
|
|
40
|
-
export {
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
export {
|
|
44
|
-
export { byAttr };
|
|
30
|
+
export { bricks, mdAutoRawTags, mdAutoNl2br, fragments, setAttrFilter, byAttrFilter, siteData };
|
|
31
|
+
|
|
32
|
+
// Export transform functions for advanced usage
|
|
33
|
+
export { transformAutoRaw, transformNl2br };
|
package/src/markdown.js
ADDED
|
@@ -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 "./
|
|
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
|
+
|
package/src/siteData.js
ADDED
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Add site.year and site.isProd global data
|
|
3
|
+
* - site.isProd: Boolean indicating if running in production mode (build) vs development (serve)
|
|
4
|
+
* - site.year: Sets the current year to be available in all templates as {{ site.year }}
|
|
5
|
+
*
|
|
6
|
+
* @param {Object} eleventyConfig - The Eleventy configuration object
|
|
7
|
+
*/
|
|
8
|
+
export function siteData(eleventyConfig) {
|
|
9
|
+
eleventyConfig.addGlobalData("site.isProd", () => process.env.ELEVENTY_RUN_MODE === "build");
|
|
10
|
+
eleventyConfig.addGlobalData("site.year", () => new Date().getFullYear());
|
|
11
|
+
}
|
|
12
|
+
|
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
|
-
|