@anydigital/eleventy-bricks 1.0.0-alpha.13

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,152 @@
1
+ import { describe, it } from "node:test";
2
+ import assert from "node:assert/strict";
3
+ import { transformAutoRaw, transformNl2br } 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 =
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
+
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
+
@@ -0,0 +1,17 @@
1
+ /**
2
+ * setAttr filter - Override an attribute and return the object
3
+ *
4
+ * This filter takes an object, a key, and a value, and returns a new object
5
+ * with the specified attribute set to the given value.
6
+ *
7
+ * @param {Object} eleventyConfig - The Eleventy configuration object
8
+ */
9
+ export function setAttrFilter(eleventyConfig) {
10
+ eleventyConfig.addFilter("setAttr", function(obj, key, value) {
11
+ return {
12
+ ...obj,
13
+ [key]: value
14
+ };
15
+ });
16
+ }
17
+
@@ -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
+
@@ -0,0 +1,13 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <meta charset="utf-8" />
5
+ <meta name="viewport" content="width=device-width, initial-scale=1.0" />
6
+ <link rel="dns-prefetch" href="https://unpkg.com">
7
+ <title>Sveltia CMS</title>
8
+ </head>
9
+ <body>
10
+ <!-- Include the script that builds the page and powers Sveltia CMS -->
11
+ <script src="https://unpkg.com/@sveltia/cms/dist/sveltia-cms.js" type="module"></script>
12
+ </body>
13
+ </html>
@@ -0,0 +1,67 @@
1
+ /* CLI */
2
+ import minimist from "minimist";
3
+ /* Plugins */
4
+ import { RenderPlugin } from "@11ty/eleventy";
5
+ import eleventyNavigationPlugin from "@11ty/eleventy-navigation";
6
+ import eleventyBricksPlugin from "@anydigital/eleventy-bricks";
7
+ /* Libraries */
8
+ import markdownIt from "markdown-it";
9
+ import markdownItAnchor from "markdown-it-anchor";
10
+ /* Data */
11
+ import yaml from "js-yaml";
12
+
13
+ /**
14
+ * Eleventy Configuration
15
+ * @param {Object} eleventyConfig - The Eleventy configuration object
16
+ * @returns {Object} The Eleventy configuration object
17
+ */
18
+ export default function(eleventyConfig) {
19
+ /* CLI support */
20
+ const argv = minimist(process.argv.slice(2));
21
+ const inputDir = argv.input || "src";
22
+
23
+ /* Plugins */
24
+ eleventyConfig.addPlugin(RenderPlugin);
25
+ eleventyConfig.addPlugin(eleventyNavigationPlugin);
26
+ eleventyConfig.addPlugin(eleventyBricksPlugin, {
27
+ bricks: true,
28
+ fragments: true,
29
+ mdAutoNl2br: true,
30
+ mdAutoRawTags: true,
31
+ setAttrFilter: true,
32
+ byAttrFilter: true,
33
+ siteData: true
34
+ });
35
+
36
+ /* Libraries */
37
+ eleventyConfig.setLibrary("md", markdownIt({
38
+ html: true,
39
+ breaks: true,
40
+ linkify: true
41
+ }).use(markdownItAnchor, {
42
+ permalink: markdownItAnchor.permalink.headerLink()
43
+ }));
44
+
45
+ /* Data */
46
+ eleventyConfig.addDataExtension("yml", (contents) => yaml.safeLoad(contents));
47
+
48
+ /* Build */
49
+ eleventyConfig.addPassthroughCopy({
50
+ "src/_public": ".",
51
+ ...(inputDir !== "src" && { [`${inputDir}/_public`]: "." })
52
+ }, { expand: true }); // This follows/resolves symbolic links
53
+
54
+ /* Dev tools */
55
+ // Follow symlinks in Chokidar used by 11ty to watch files
56
+ eleventyConfig.setChokidarConfig({ followSymlinks: true });
57
+
58
+ /* Config */
59
+ return {
60
+ templateFormats: ["md", "njk"],
61
+ htmlTemplateEngine: "njk",
62
+ dir: {
63
+ input: inputDir,
64
+ includes: "_template"
65
+ }
66
+ };
67
+ };