@anyblades/eleventy-blades 0.28.0-beta.2

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.
@@ -0,0 +1,207 @@
1
+ import { describe, it } from "node:test";
2
+ import assert from "node:assert/strict";
3
+ import { transformAutoRaw, transformNl2br, transformUncommentAttrs } from "./markdown.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 = "Use {% raw %}{{{% endraw %} variable {% raw %}}}{% endraw %} to output.";
9
+ assert.equal(transformAutoRaw(input), expected);
10
+ });
11
+
12
+ it("should wrap closing double curly braces with raw tags", () => {
13
+ const input = "{{ name }}";
14
+ const expected = "{% raw %}{{{% endraw %} name {% raw %}}}{% endraw %}";
15
+ assert.equal(transformAutoRaw(input), expected);
16
+ });
17
+
18
+ it("should wrap opening template tags with raw tags", () => {
19
+ const input = "{% if condition %}";
20
+ const expected = "{% raw %}{%{% endraw %} if condition {% raw %}%}{% endraw %}";
21
+ assert.equal(transformAutoRaw(input), expected);
22
+ });
23
+
24
+ it("should wrap closing template tags with raw tags", () => {
25
+ const input = "{% endif %}";
26
+ const expected = "{% raw %}{%{% endraw %} endif {% raw %}%}{% endraw %}";
27
+ assert.equal(transformAutoRaw(input), expected);
28
+ });
29
+
30
+ it("should handle multiple Nunjucks patterns in one string", () => {
31
+ const input = "{{ var1 }} and {% if test %} something {% endif %}";
32
+ const expected =
33
+ "{% raw %}{{{% endraw %} var1 {% raw %}}}{% endraw %} and {% raw %}{%{% endraw %} if test {% raw %}%}{% endraw %} something {% raw %}{%{% endraw %} endif {% raw %}%}{% endraw %}";
34
+ assert.equal(transformAutoRaw(input), expected);
35
+ });
36
+
37
+ it("should handle multiline content with Nunjucks syntax", () => {
38
+ const input = `# Title
39
+ {{ variable }}
40
+ Some text
41
+ {% for item in items %}
42
+ {{ item }}
43
+ {% endfor %}`;
44
+ const expected = `# Title
45
+ {% raw %}{{{% endraw %} variable {% raw %}}}{% endraw %}
46
+ Some text
47
+ {% raw %}{%{% endraw %} for item in items {% raw %}%}{% endraw %}
48
+ {% raw %}{{{% endraw %} item {% raw %}}}{% endraw %}
49
+ {% raw %}{%{% endraw %} endfor {% raw %}%}{% endraw %}`;
50
+ assert.equal(transformAutoRaw(input), expected);
51
+ });
52
+
53
+ it("should return unchanged content when no Nunjucks syntax is present", () => {
54
+ const input = "This is just plain text with no templates.";
55
+ assert.equal(transformAutoRaw(input), input);
56
+ });
57
+
58
+ it("should handle empty string", () => {
59
+ assert.equal(transformAutoRaw(""), "");
60
+ });
61
+
62
+ it("should handle content with only Nunjucks syntax", () => {
63
+ const input = "{{}}";
64
+ const expected = "{% raw %}{{{% endraw %}{% raw %}}}{% endraw %}";
65
+ assert.equal(transformAutoRaw(input), expected);
66
+ });
67
+
68
+ it("should handle consecutive Nunjucks patterns", () => {
69
+ const input = "{{{{}}}}";
70
+ const expected = "{% raw %}{{{% endraw %}{% raw %}{{{% endraw %}{% raw %}}}{% endraw %}{% raw %}}}{% endraw %}";
71
+ assert.equal(transformAutoRaw(input), expected);
72
+ });
73
+
74
+ it("should wrap each delimiter individually", () => {
75
+ const input = "Show {{ and }} and {% and %}";
76
+ const expected =
77
+ "Show {% raw %}{{{% endraw %} and {% raw %}}}{% endraw %} and {% raw %}{%{% endraw %} and {% raw %}%}{% endraw %}";
78
+ assert.equal(transformAutoRaw(input), expected);
79
+ });
80
+ });
81
+
82
+ describe("transformNl2br", () => {
83
+ it("should convert single \\n to <br>", () => {
84
+ const input = "Line 1\\nLine 2";
85
+ const expected = "Line 1<br>Line 2";
86
+ assert.equal(transformNl2br(input), expected);
87
+ });
88
+
89
+ it("should convert double \\n\\n to <br>", () => {
90
+ const input = "Line 1\\n\\nLine 2";
91
+ const expected = "Line 1<br>Line 2";
92
+ assert.equal(transformNl2br(input), expected);
93
+ });
94
+
95
+ it("should convert multiple \\n sequences", () => {
96
+ const input = "Line 1\\nLine 2\\nLine 3";
97
+ const expected = "Line 1<br>Line 2<br>Line 3";
98
+ assert.equal(transformNl2br(input), expected);
99
+ });
100
+
101
+ it("should handle mixed single and double \\n", () => {
102
+ const input = "Line 1\\n\\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 text without \\n", () => {
108
+ const input = "Just plain text";
109
+ assert.equal(transformNl2br(input), input);
110
+ });
111
+
112
+ it("should handle empty content", () => {
113
+ assert.equal(transformNl2br(""), "");
114
+ });
115
+
116
+ it("should handle content with only \\n", () => {
117
+ const input = "\\n\\n\\n";
118
+ const expected = "<br><br>";
119
+ assert.equal(transformNl2br(input), expected);
120
+ });
121
+
122
+ it("should handle markdown table cell content with \\n", () => {
123
+ const input = "Cell 1\\nCell 1 Line 2\\n\\nCell 1 Line 3";
124
+ const expected = "Cell 1<br>Cell 1 Line 2<br>Cell 1 Line 3";
125
+ assert.equal(transformNl2br(input), expected);
126
+ });
127
+
128
+ it("should handle multiple consecutive double \\n\\n", () => {
129
+ const input = "Line 1\\n\\n\\n\\nLine 2";
130
+ const expected = "Line 1<br><br>Line 2";
131
+ assert.equal(transformNl2br(input), expected);
132
+ });
133
+
134
+ it("should preserve actual newlines (not literal \\n)", () => {
135
+ const input = "Line 1\nLine 2";
136
+ const expected = "Line 1\nLine 2";
137
+ assert.equal(transformNl2br(input), expected);
138
+ });
139
+
140
+ it("should only convert literal backslash-n sequences", () => {
141
+ const input = "Text with\\nbackslash-n and\nreal newline";
142
+ const expected = "Text with<br>backslash-n and\nreal newline";
143
+ assert.equal(transformNl2br(input), expected);
144
+ });
145
+ });
146
+
147
+ describe("transformUncommentAttrs", () => {
148
+ it("should expand a single <!--{...}--> to {...}", () => {
149
+ const input = "Some text <!--{.class}-->";
150
+ const expected = "Some text {.class}";
151
+ assert.equal(transformUncommentAttrs(input), expected);
152
+ });
153
+
154
+ it("should expand multiple <!--{...}--> occurrences", () => {
155
+ const input = "<!--{.foo}--> text <!--{.bar}-->";
156
+ const expected = "{.foo} text {.bar}";
157
+ assert.equal(transformUncommentAttrs(input), expected);
158
+ });
159
+
160
+ it("should handle attributes with spaces inside braces", () => {
161
+ const input = "Heading <!--{ .class #id }-->";
162
+ const expected = "Heading { .class #id }";
163
+ assert.equal(transformUncommentAttrs(input), expected);
164
+ });
165
+
166
+ it("should handle multiline content", () => {
167
+ const input = `# Title <!--{.heading}-->
168
+ Some paragraph.
169
+ > Blockquote <!--{.note}-->`;
170
+ const expected = `# Title {.heading}
171
+ Some paragraph.
172
+ > Blockquote {.note}`;
173
+ assert.equal(transformUncommentAttrs(input), expected);
174
+ });
175
+
176
+ it("should leave regular HTML comments untouched", () => {
177
+ const input = "<!-- this is a comment -->";
178
+ assert.equal(transformUncommentAttrs(input), input);
179
+ });
180
+
181
+ it("should leave <!--{...}--> intact when the content contains a closing brace inside", () => {
182
+ // The regex [^}]* stops at the first }, so nested } prevents a full match
183
+ const input = "<!--{{nested}}-->";
184
+ assert.equal(transformUncommentAttrs(input), input);
185
+ });
186
+
187
+ it("should not touch plain text with no comments", () => {
188
+ const input = "Just plain text with {.class} already exposed.";
189
+ assert.equal(transformUncommentAttrs(input), input);
190
+ });
191
+
192
+ it("should handle empty string", () => {
193
+ assert.equal(transformUncommentAttrs(""), "");
194
+ });
195
+
196
+ it("should handle back-to-back patterns without separator", () => {
197
+ const input = "<!--{.a}--><!--{.b}-->";
198
+ const expected = "{.a}{.b}";
199
+ assert.equal(transformUncommentAttrs(input), expected);
200
+ });
201
+
202
+ it("should preserve surrounding markdown content", () => {
203
+ const input = "| Cell 1 <!--{.highlight}--> | Cell 2 |";
204
+ const expected = "| Cell 1 {.highlight} | Cell 2 |";
205
+ assert.equal(transformUncommentAttrs(input), expected);
206
+ });
207
+ });
@@ -0,0 +1,25 @@
1
+ // <!--section:code-->```js
2
+ /**
3
+ * Add site.year and site.prod global data
4
+ * - site.prod: Boolean indicating if running in production mode (build) vs development (serve)
5
+ * - site.year: Sets the current year to be available in all templates as {{ site.year }}
6
+ *
7
+ * @param {Object} eleventyConfig - The Eleventy configuration object
8
+ */
9
+ export function siteData(eleventyConfig) {
10
+ eleventyConfig.addGlobalData("site.prod", () => process.env.ELEVENTY_RUN_MODE === "build");
11
+ eleventyConfig.addGlobalData("site.year", () => new Date().getFullYear());
12
+ }
13
+ /*```
14
+
15
+ <!--section:docs-->
16
+ ### Data helpers
17
+
18
+ Adds global `site` data to your Eleventy project, providing commonly needed values that can be accessed in all templates:
19
+
20
+ | Variable | Value |
21
+ | ----------------- | ------------------------------------------------------------------------------------------------------------ |
22
+ | `{{ site.year }}` | The current year as a number (e.g., `2026`) |
23
+ | `{{ site.prod }}` | Boolean indicating if running in production mode (`true` for `eleventy build`, `false` for `eleventy serve`) |
24
+
25
+ <!--section--> */