-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy patheleventy.config.js
More file actions
415 lines (348 loc) · 12.1 KB
/
eleventy.config.js
File metadata and controls
415 lines (348 loc) · 12.1 KB
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
import path, { dirname, resolve } from "path";
import { fileURLToPath } from "url";
import childProcess from "child_process";
import fs from "fs";
import { EleventyRenderPlugin } from "@11ty/eleventy";
import { load as yamlLoad } from "js-yaml";
import {
CALLOUT_TYPE,
CALLOUT_TYPE_MAP,
HAZARD_TYPE,
HAZARD_TYPE_MAP,
STEP_INFO_ICON,
STEP_RESULT_ICON,
} from "./defs.js";
import markdownIt from "markdown-it";
const __filename = fileURLToPath(import.meta.url);
const __dirname = dirname(__filename);
const md = markdownIt({ html: true, breaks: false });
const isPreview = process.env.PREVIEW_BUILD || process.argv.includes("--serve");
const languageDisplayNames = new Intl.DisplayNames(["en"], {
type: "language",
});
const currentGitSha = childProcess
.execSync(`git log -1 --format=format:%H`)
.toString()
.trim();
export default async function (eleventyConfig) {
eleventyConfig.setInputDirectory("src");
eleventyConfig.setLayoutsDirectory("../_layouts");
eleventyConfig.setIncludesDirectory("../_includes");
eleventyConfig.setDataDirectory("../_data");
eleventyConfig.addGlobalData(
"layout",
isPreview ? "preview.njk" : "base.njk"
);
eleventyConfig.addPlugin(EleventyRenderPlugin);
// Add ids to headings
eleventyConfig.addTransform(
"add-heading-ids",
function (content, outputPath) {
if (outputPath.endsWith(".html")) {
return content.replace(
/<h([1-6])>([^<]+)<\/h[1-6]>/g,
function (match, level, text) {
const id = text
.toLowerCase()
.replace(/[^a-z0-9]+/g, "-")
.replace(/(^-|-$)/g, "");
return `<h${level} id="${id}">${text}</h${level}>`;
}
);
}
return content;
}
);
eleventyConfig.addCollection("zendeskSections", function (collection) {
const data = {};
const sections = collection
.getAll()
.filter((item) => item.data.zendesk && item.data.zendesk.section_id)
.sort((a, b) => a.data.zendesk.position - b.data.zendesk.position);
const articles = collection
.getAll()
.filter((item) => item.data.zendesk && item.data.zendesk.article_id)
.sort((a, b) => a.data.zendesk.position - b.data.zendesk.position);
for (const section of sections) {
const sectionBasePath = section.inputPath
.split("/")
.slice(0, -1)
.join("/");
data[section.data.zendesk.section_id] = {
...section.data.zendesk,
section,
articles: articles.filter((article) =>
article.inputPath.startsWith(sectionBasePath)
),
};
}
return data;
});
eleventyConfig.addFilter("languageDisplayName", function (code) {
return languageDisplayNames.of(code);
});
eleventyConfig.addCollection("zendeskCategories", function (collection) {
const data = {};
const sectionsData = {};
const categories = collection
.getAll()
.filter((item) => item.data.zendesk && item.data.zendesk.category_id)
.sort((a, b) => a.data.zendesk.position - b.data.zendesk.position);
const sections = collection
.getAll()
.filter((item) => item.data.zendesk && item.data.zendesk.section_id)
.sort((a, b) => a.data.zendesk.position - b.data.zendesk.position);
const articles = collection
.getAll()
.filter((item) => item.data.zendesk && item.data.zendesk.article_id)
.sort((a, b) => a.data.zendesk.position - b.data.zendesk.position);
for (const section of sections) {
const sectionBasePath = section.inputPath
.split("/")
.slice(0, -1)
.join("/");
sectionsData[section.data.zendesk.section_id] = {
...section.data.zendesk,
section,
articles: articles.filter((article) =>
article.inputPath.startsWith(sectionBasePath)
),
};
}
for (const category of categories) {
const categoryBasePath = category.inputPath
.split("/")
.slice(0, -1)
.join("/");
data[category.data.zendesk.category_id] = {
...category.data.zendesk,
category,
sections: Object.values(sectionsData).filter((section) =>
section.section.inputPath.startsWith(categoryBasePath)
),
};
}
return data;
});
// Make external links open in a new tab
eleventyConfig.addTransform("external-links", function (content, outputPath) {
if (outputPath.endsWith(".html")) {
return content.replace(
/<a href="(http[s]:\/\/)(?!support\.nabucasa\.com)([^"]+)"/g,
'<a href="$1$2" target="_blank" rel="noreferrer"'
);
}
return content;
});
eleventyConfig.addShortcode("zendeskCategoryId", function (page) {
const [_, __, categoryName] = page.inputPath.split("/");
if (!categoryName) {
return "";
}
const targetPath = resolve(
eleventyConfig.dir.input,
"src",
categoryName,
"_category.md"
);
if (!fs.existsSync(targetPath)) {
return "";
}
const content = fs
.readFileSync(targetPath, { encoding: "utf-8" })
.toString()
.split("---")[1];
return yamlLoad(content, { json: true }).zendesk.category_id;
});
eleventyConfig.addShortcode("zendeskSectionId", function (page) {
const [_, __, categoryName, sectionName] = page.inputPath.split("/");
if (!sectionName) {
return "";
}
const targetPath = resolve(
eleventyConfig.dir.input,
"src",
categoryName,
sectionName,
"_section.md"
);
if (!fs.existsSync(targetPath)) {
return "";
}
const content = fs
.readFileSync(targetPath, { encoding: "utf-8" })
.toString()
.split("---")[1];
return yamlLoad(content, { json: true }).zendesk.section_id;
});
let currentStep = 0;
eleventyConfig.addPairedShortcode("steps", function (content) {
currentStep = 0; // reset step number
let html = `<div class="steps">`; // open steps
html += content;
html += `</div>`; // close steps
return html;
});
eleventyConfig.addPairedShortcode("step", function (content, title, prefix) {
let html = `<div class="step">`; // open step
let stepPrefix = `Step ${currentStep + 1}`;
if (prefix) {
stepPrefix = prefix;
}
html += `<div class="step-header">`; // open header
if (stepPrefix) {
html += `<div class="step-number">${stepPrefix}</div>`; // add step number
}
if (title) {
html += `<div class="step-title">${title}</div>`; // add step title
}
html += `</div>`; // close header
html += `<div class="step-content-wrapper">`; // open content
html += content;
html += `</div>`; // close content
html += `</div>`; // close step
currentStep++; // increment step number
return html;
});
eleventyConfig.addPairedShortcode("prereq", function (content) {
let html = `<div class="step">`; // open prereq
html += `<div class="step-content-wrapper">`; // open content
html += content;
html += `</div>`; // close content
html += `</div>`; // close prereq
return html;
});
eleventyConfig.addPairedShortcode("stepContent", function (content) {
let html = `<div class="step-content">`; // open content
html += content;
html += `</div>`; // close content
return html;
});
eleventyConfig.addShortcode("image", function (src, alt) {
if (!src) {
throw new Error("Image shortcode requires a src parameter");
}
if (!alt) {
throw new Error("Image shortcode requires an alt parameter");
}
return `<img src="${src}" alt="${alt}"/>`;
});
eleventyConfig.addShortcode("abbr", function (abbr) {
const targetPath = resolve(eleventyConfig.dir.input, "_data", "abbr.yml");
const abbrDefinitions = yamlLoad(fs.readFileSync(targetPath, "utf8"));
const abbrValue = abbrDefinitions[abbr];
if (!abbrValue) {
throw new Error(`Abbr '${abbr}' not found in _data/abbr.yml`);
}
return `<abbr title="${abbrValue}">${abbr}</abbr>`;
});
eleventyConfig.addPairedShortcode("hazard", function (content, type) {
// if type not in array of strings
if (!Object.values(HAZARD_TYPE).includes(type)) {
throw new Error(
`Hazard shortcode requires a valid type parameter: ${Object.values(
HAZARD_TYPE
).join(", ")}`
);
}
const hazardKey = Object.keys(HAZARD_TYPE).find(
(key) => HAZARD_TYPE[key] === type
);
const hazardMap = HAZARD_TYPE_MAP[hazardKey];
if (!hazardMap) {
throw new Error(`Hazard type ${type} not found in hazard map`);
}
if (!content) {
throw new Error("Hazard shortcode requires content");
}
return `<div class="hazard ${type}"><div class="hazard-prefix">${hazardMap.icon} ${hazardMap.text}</div><div class="hazard-content">${md.renderInline(content)}</div></div>`;
});
eleventyConfig.addPairedShortcode("callout", function (content, type) {
// if type not in array of strings
if (!Object.values(CALLOUT_TYPE).includes(type)) {
throw new Error(
`Callout shortcode requires a valid type parameter: ${Object.values(
CALLOUT_TYPE
).join(", ")}`
);
}
const calloutKey = Object.keys(CALLOUT_TYPE).find(
(key) => CALLOUT_TYPE[key] === type
);
const calloutMap = CALLOUT_TYPE_MAP[calloutKey];
if (!calloutMap) {
throw new Error(`Callout type ${type} not found in callout map`);
}
if (!content) {
throw new Error("Callout shortcode requires content");
}
return `<div class="callout ${type}"><div class="callout-prefix">${calloutMap.icon} ${calloutMap.text}:</div><div class="callout-content">${md.renderInline(content)}</div></div>`;
});
eleventyConfig.addShortcode("stepInfo", function (content) {
// if type not in array of strings
if (!content) {
throw new Error("Step info shortcode requires content");
}
return `<div class="step-info"><div class="step-info-prefix">${STEP_INFO_ICON} Info:</div><div>${md.renderInline(content)}</div></div>`;
});
eleventyConfig.addShortcode("stepResult", function (content) {
// if type not in array of strings
if (!content) {
throw new Error("Step result shortcode requires content");
}
return `<div class="step-result"><div class="step-result-prefix">${STEP_RESULT_ICON} Result:</div><div>${md.renderInline(content)}</div></div>`;
});
eleventyConfig.addShortcode("zendeskData", function (zendeskFrontmatter) {
return `<!-- ${JSON.stringify({ zendesk: zendeskFrontmatter })} -->`;
});
eleventyConfig.addShortcode("currentGitSha", function () {
return currentGitSha;
});
if (isPreview) {
// Additional changes required for preview build
eleventyConfig.setServerOptions({
messageOnStart: ({ options }) =>
`Server started at http://127.0.0.1:${options.port}/`,
});
eleventyConfig.addGlobalData("permalink", () => {
return (data) => {
if (data?.zendesk?.article_id) {
return `/articles/${data.zendesk.article_id}.${data.page.outputFileExtension}`;
}
if (data?.zendesk?.section_id) {
return `/sections/${data.zendesk.section_id}.${data.page.outputFileExtension}`;
}
if (data?.zendesk?.category_id) {
return `/categories/${data.zendesk.category_id}.${data.page.outputFileExtension}`;
}
return `${data.page.filePathStem}.${data.page.outputFileExtension}`;
};
});
eleventyConfig.addPassthroughCopy("static");
eleventyConfig.addTransform(
"external-links",
function (content, outputPath) {
if (outputPath.endsWith(".html")) {
return content.replace(
/="\/hc\/en-us\/(categories|articles|sections)\/(\w+).*"/g,
'="/$1/$2"'
);
}
return content;
}
);
} else {
eleventyConfig.addTransform(
"replace-static-links",
function (content, outputPath) {
if (outputPath.endsWith(".html")) {
return content.replace(
/(href|src)="\/static/g,
'$1="https://raw.githubusercontent.com/NabuCasa/support/refs/heads/main/static'
);
}
return content;
}
);
}
}