-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path.eleventy.js
106 lines (93 loc) · 3.16 KB
/
.eleventy.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
// Eleventy Version 3.0 with node.js >18
const nunjucks = require("nunjucks");
// internationalization
const i18n = require("eleventy-plugin-i18n");
// Traductions
const translations = require("./src/_data/translations.js");
module.exports = async function (eleventyConfig) {
const { EleventyI18nPlugin } = await import("@11ty/eleventy");
eleventyConfig.setNunjucksEnvironmentOptions({
throwOnUndefined: true,
autoescape: false, // warning: don’t do this!
});
// ************** plugin **************************
// internationalization
eleventyConfig.addPlugin(EleventyI18nPlugin, {
defaultLanguage: "fr", // Required
errorMode: "allow-fallback", // Opting out of "strict"
});
// translation
eleventyConfig.addPlugin(i18n, {
translations,
fallbackLocales: {
fr: "fr",
},
});
// ***************** Filter ***********************
// Return the localName of the prefixed URI in parameter
eleventyConfig.addFilter("localName", async function (uri) {
return uri.split(":")[1];
});
/**
* Returns the first element in the array (or single item) having @language
* equal to the provided language code,
* or the first item if none matches the language code
**/
eleventyConfig.addFilter("lang", function (arr, locale) {
if (arr) {
// if t
var jsonFilter = [arr].find((f) => f["@language"] === locale);
if (jsonFilter === undefined) {
result = "";
for (let index = 0; index < arr.length; index++) {
const element = arr[index];
if (element["@language"] === locale) {
result = element["@value"];
}
}
const output_Label = result === "" ? arr["@value"][0] : result;
return output_Label;
} else {
return jsonFilter["@value"];
}
}
});
// URL
eleventyConfig.addFilter("getURL", async function (inputContext, concept) {
const inputPrefix = concept.split(":")[0];
const inputConcept = concept.split(":")[1];
const obj = JSON.parse(inputContext);
const uriFound = JSON.stringify(obj, [inputPrefix]);
const objOutput = JSON.parse(uriFound);
return objOutput[inputPrefix] + inputConcept;
});
/**
* https://stackoverflow.com/questions/46426306/how-to-safely-render-json-into-an-inline-script-using-nunjucks
* Returns a JSON stringified version of the value, safe for inclusion in an
* inline <script> tag. The optional argument 'spaces' can be used for
* pretty-printing.
*
* Output is NOT safe for inclusion in HTML! If that's what you need, use the
* built-in 'dump' filter instead.
*/
eleventyConfig.addFilter("json", function (value) {
if (value instanceof nunjucks.runtime.SafeString) {
value = value.toString();
}
const spaces = null;
//const jsonString = JSON.stringify(value, null, spaces).replace(/</g, '\\u003c')
const jsonString = JSON.stringify(value);
return new nunjucks.runtime.markSafe(jsonString);
});
return {
markdownTemplateEngine: "njk",
htmlTemplateEngine: "njk",
dir: {
input: "src",
output: "dist",
includes: "_includes",
layouts: "_layouts",
data: "_data",
},
};
};