-
Notifications
You must be signed in to change notification settings - Fork 0
/
.eleventy.js
117 lines (101 loc) · 3.15 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
107
108
109
110
111
112
113
114
115
116
117
const beautifyHTML = require("js-beautify").html;
const fs = require("fs");
const hljs = require("highlight.js");
const markdownIt = require("markdown-it");
const markdownItAnchor = require("markdown-it-anchor");
const matter = require("gray-matter");
const mojFilters = require("@ministryofjustice/frontend/moj/filters/all");
const nunjucks = require("nunjucks");
const path = require("path");
const releasePackage = require('./package/package.json');
const { EleventyHtmlBasePlugin } = require("@11ty/eleventy");
module.exports = function (eleventyConfig) {
const nunjucksEnv = nunjucks.configure([
".",
"docs/_includes/",
"node_modules/govuk-frontend/dist/",
"node_modules/@ministryofjustice/frontend/",
"src/",
]);
Object.entries({
...eleventyConfig.nunjucksFilters,
...mojFilters(),
}).forEach(([name, callback]) => {
nunjucksEnv.addFilter(name, callback);
});
const setUpNunjucksFilters = require('./package/dpr/setUpNunjucksFilters').default
setUpNunjucksFilters(nunjucksEnv)
eleventyConfig.setLibrary("njk", nunjucksEnv);
eleventyConfig.setLibrary(
"md",
markdownIt({
html: true,
breaks: false,
highlight: (str, language) =>
language ? hljs.highlight(str, { language }).value : str,
})
.disable("code")
.use(markdownItAnchor, {
level: [1, 2, 3, 4],
})
);
eleventyConfig.addShortcode("example", function (exampleName, height) {
const { data, content: nunjucksCode } = matter(
fs
.readFileSync(
path.join(__dirname, "docs", "examples", exampleName, "index.njk"),
"utf8"
)
.trim()
);
const rawHtmlCode = nunjucksEnv.renderString(nunjucksCode);
const htmlCode = beautifyHTML(rawHtmlCode.trim(), {
indent_size: 2,
end_with_newline: true,
max_preserve_newlines: 1,
unformatted: ["code", "pre", "em", "strong"],
});
let jsCode = "";
try {
jsCode = fs
.readFileSync(
path.join(__dirname, "docs", "examples", exampleName, "script.js"),
"utf8"
)
.trim();
} catch (e) {}
const renderedExample = nunjucksEnv.render("example.njk", {
href: "/examples/" + exampleName,
id: exampleName,
arguments: data.arguments,
jsArguments: data.jsArguments,
title: data.title,
height,
nunjucksCode,
htmlCode,
jsCode,
});
// GovUK tabs now add a bunch of whitespace in front of their content.
// This means Eleventy doesn't spot the markdown tokens unless we remove the whitespace.
return renderedExample.replace(/ +(```|#|\|)/g, '$1')
});
eleventyConfig.addShortcode("version", function () {
return releasePackage.version;
});
eleventyConfig.addShortcode("header", function (level, text) {
let classSuffix = ""
switch (Number(level)) {
case 1:
classSuffix = "l"
break;
case 2:
classSuffix = "m"
break;
case 3:
classSuffix = "s"
break;
}
return `<h${ level } class="govuk-heading-${ classSuffix }">${ text }</h${ level }>`;
});
eleventyConfig.addPlugin(EleventyHtmlBasePlugin);
};