forked from w3c/wcag
-
Notifications
You must be signed in to change notification settings - Fork 0
/
eleventy.config.ts
208 lines (184 loc) · 8.18 KB
/
eleventy.config.ts
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
import compact from "lodash-es/compact";
import { copyFile } from "fs/promises";
import { CustomLiquid } from "11ty/CustomLiquid";
import { actRules, getFlatGuidelines, getPrinciples } from "11ty/guidelines";
import {
getFlatTechniques,
getTechniqueAssociations,
getTechniquesByTechnology,
technologies,
technologyTitles,
} from "11ty/techniques";
import { generateUnderstandingNavMap, getUnderstandingDocs } from "11ty/understanding";
import type { EleventyContext, EleventyData, EleventyEvent } from "11ty/types";
/** Version of WCAG to build */
const version = "22";
const principles = await getPrinciples();
const flatGuidelines = getFlatGuidelines(principles);
const techniques = await getTechniquesByTechnology();
const flatTechniques = getFlatTechniques(techniques);
const techniqueAssociations = await getTechniqueAssociations(flatGuidelines);
const understandingDocs = await getUnderstandingDocs(version);
const understandingNav = await generateUnderstandingNavMap(principles, understandingDocs);
// Declare static global data up-front so we can build typings from it
const globalData = {
version,
versionDecimal: version.split("").join("."),
techniques, // Used for techniques/index.html
technologies, // Used for techniques/index.html
technologyTitles, // Used for techniques/index.html
principles, // Used for understanding/index.html
understandingDocs, // Used for understanding/index.html
};
export type GlobalData = EleventyData &
typeof globalData & {
// Expected data cascade properties from *.11tydata.js
headerLabel?: string; // akin to documentset.name in build.xml
headerUrl?: string;
isTechniques?: boolean;
isUnderstanding?: boolean;
};
const baseUrls = {
guidelines: `https://www.w3.org/TR/WCAG${version}/`,
techniques: "/techniques/",
understanding: "/understanding/",
};
if (process.env.WCAG_MODE === "editors") {
// For pushing to gh-pages
baseUrls.guidelines = "https://w3c.github.io/wcag/guidelines/";
baseUrls.techniques = "https://w3c.github.io/wcag/techniques/";
baseUrls.understanding = "https://w3c.github.io/wcag/understanding/";
} else if (process.env.WCAG_MODE === "publication") {
// For pushing to W3C site
baseUrls.guidelines = `https://www.w3.org/TR/WCAG${version}/`;
baseUrls.techniques = `https://www.w3.org/WAI/WCAG${version}/Techniques/`;
baseUrls.understanding = `https://www.w3.org/WAI/WCAG${version}/Understanding/`;
}
export default function (eleventyConfig: any) {
for (const [name, value] of Object.entries(globalData)) eleventyConfig.addGlobalData(name, value);
// Make baseUrls available to templates
for (const [name, value] of Object.entries(baseUrls))
eleventyConfig.addGlobalData(`${name}Url`, value);
// eleventyComputed data is assigned here rather than in 11tydata files;
// we have access to typings here, and can keep the latter fully static.
eleventyConfig.addGlobalData("eleventyComputed", {
// permalink determines output structure; see https://www.11ty.dev/docs/permalinks/
permalink: ({ page, isUnderstanding }: GlobalData) => {
if (isUnderstanding) {
// understanding-metadata.html exists in 2 places; top-level wins in XSLT process
if (/\/20\/understanding-metadata/.test(page.inputPath)) return false;
// Flatten pages into top-level directory, out of version subdirectories
return page.inputPath.replace(/\/2\d\//, "/");
}
// Preserve existing structure: write to x.html instead of x/index.html
return page.inputPath;
},
nav: ({ page, isUnderstanding }: GlobalData) =>
isUnderstanding ? understandingNav[page.fileSlug] : null,
testRules: ({ page, isTechniques, isUnderstanding }: GlobalData) => {
if (isTechniques)
return actRules.filter(({ wcagTechniques }) => wcagTechniques.includes(page.fileSlug));
if (isUnderstanding)
return actRules.filter(({ successCriteria }) => successCriteria.includes(page.fileSlug));
},
// Data for individual technique pages
technique: ({ page, isTechniques }: GlobalData) =>
isTechniques ? flatTechniques[page.fileSlug] : null,
techniqueAssociations: ({ page, isTechniques }: GlobalData) =>
isTechniques ? techniqueAssociations[page.fileSlug] : null,
// Data for individual understanding pages
guideline: ({ page, isUnderstanding }: GlobalData) =>
isUnderstanding ? flatGuidelines[page.fileSlug] : null,
});
// See https://www.11ty.dev/docs/copy/#emulate-passthrough-copy-during-serve
eleventyConfig.setServerPassthroughCopyBehavior("passthrough");
eleventyConfig.addPassthroughCopy("techniques/*.css");
eleventyConfig.addPassthroughCopy("techniques/*/img/*");
eleventyConfig.addPassthroughCopy({
"css/base.css": "techniques/base.css",
"css/a11y-light.css": "techniques/a11y-light.css",
"script/highlight.min.js": "techniques/highlight.min.js",
});
eleventyConfig.addPassthroughCopy("understanding/*.css");
eleventyConfig.addPassthroughCopy({
"guidelines/relative-luminance.html": "understanding/relative-luminance.html",
"understanding/*/img/*": "understanding/img", // Intentionally flatten
});
eleventyConfig.addPassthroughCopy("working-examples/**");
eleventyConfig.on("eleventy.after", async ({ dir }: EleventyEvent) => {
// addPassthroughCopy can only map each file once,
// but base.css needs to be copied to a 2nd destination
await copyFile(`${dir.input}/css/base.css`, `${dir.output}/understanding/base.css`);
});
eleventyConfig.setLibrary(
"liquid",
new CustomLiquid({
// See https://www.11ty.dev/docs/languages/liquid/#liquid-options
root: ["_includes", "."],
jsTruthy: true,
strictFilters: true,
})
);
// Filter that transforms a technique ID (or list of them) into links to their pages.
eleventyConfig.addFilter(
"linkTechniques",
function (this: EleventyContext, ids: string | string[]) {
const links = (Array.isArray(ids) ? ids : [ids]).map((id) => {
if (typeof id !== "string") throw new Error(`linkTechniques: invalid id ${id}`);
const technique = flatTechniques[id];
if (!technique) {
console.warn(
`linkTechniques in ${this.page.inputPath}: ` +
`skipping unresolvable technique id ${id}`
);
return;
}
// Support relative technique links from other techniques or from techniques/index.html,
// otherwise path-absolute when cross-linked from understanding/*
const urlBase = /^\/techniques\/.*\//.test(this.page.filePathStem)
? "../"
: this.page.filePathStem.startsWith("/techniques")
? ""
: baseUrls.techniques;
const label = `${id}: ${technique.truncatedTitle}`;
return `<a href="${urlBase}${technique.technology}/${id}">${label}</a>`;
});
return compact(links).join("\nand\n");
}
);
// Filter that transforms a guideline or SC shortname (or list of them) into links to their pages.
eleventyConfig.addFilter(
"linkUnderstanding",
function (this: EleventyContext, ids: string | string[]) {
return (Array.isArray(ids) ? ids : [ids])
.map((id) => {
if (typeof id !== "string") throw new Error("linkUnderstanding: invalid id passed");
const guideline = flatGuidelines[id];
if (!guideline) {
console.warn(
`linkUnderstanding in ${this.page.inputPath}: ` +
`skipping unresolvable guideline shortname ${id}`
);
}
const urlBase = this.page.filePathStem.startsWith("/understanding/")
? ""
: baseUrls.understanding;
const label = `${guideline.num}: ${guideline.name}`;
return `<a href="${urlBase}${id}">${label}</a>`;
})
.join("\nand\n");
}
);
// Renders a section box (used for About this Technique and Guideline / SC)
eleventyConfig.addPairedShortcode(
"sectionbox",
(content: string, id: string, title: string) => `
<section id="${id}" class="box">
<h2 class="box-h box-h-icon">${title}</h2>
<div class="box-i">${content}</div>
</section>
`
);
// Suppress default build output that prints every path, to make our own output clearly visible
eleventyConfig.setQuietMode(true);
}