Skip to content

Commit 7aad7a7

Browse files
authored
Pin definitions when building an older version (#4155)
This is a follow-up to #4007, in which I had neglected to pin definitions for key terms to the published recommendation when building for 2.1. - Moves `termsMap` definition to the Eleventy config (where all other target-version-dependent assignments are made), to be passed into the `CustomLiquid` constructor - Updates `termsMap` references within `CustomLiquid` to use an instance variable set in the constructor - Updates `loadRemoteGuidelines` to account for pre-processed terms content - Also updates to account for #4124 - Skips entirety of custom `render` processing for pages not being output for 2.1 (to avoid irrelevant error messages as well as save time)
1 parent 6ec98a3 commit 7aad7a7

File tree

4 files changed

+39
-23
lines changed

4 files changed

+39
-23
lines changed

11ty/CustomLiquid.ts

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { Liquid, type Template } from "liquidjs";
2-
import type { RenderOptions } from "liquidjs/dist/liquid-options";
2+
import type { LiquidOptions, RenderOptions } from "liquidjs/dist/liquid-options";
33
import compact from "lodash-es/compact";
44
import uniq from "lodash-es/uniq";
55

@@ -10,7 +10,7 @@ import type { GlobalData } from "eleventy.config";
1010
import { biblioPattern, getBiblio } from "./biblio";
1111
import { flattenDom, load, type CheerioAnyNode } from "./cheerio";
1212
import { generateId } from "./common";
13-
import { getAcknowledgementsForVersion, getTermsMap } from "./guidelines";
13+
import { getAcknowledgementsForVersion, type TermsMap } from "./guidelines";
1414
import { resolveTechniqueIdFromHref, understandingToTechniqueLinkSelector } from "./techniques";
1515
import { techniqueToUnderstandingLinkSelector } from "./understanding";
1616

@@ -22,7 +22,6 @@ const techniquesPattern = /\btechniques\//;
2222
const understandingPattern = /\bunderstanding\//;
2323

2424
const biblio = await getBiblio();
25-
const termsMap = await getTermsMap();
2625
const termLinkSelector = "a:not([href])";
2726

2827
/** Generates {% include "foo.html" %} directives from 1 or more basenames */
@@ -72,6 +71,10 @@ function expandTechniqueLink($el: CheerioAnyNode) {
7271

7372
const stripHtmlComments = (html: string) => html.replace(/<!--[\s\S]*?-->/g, "");
7473

74+
interface CustomLiquidOptions extends LiquidOptions {
75+
termsMap: TermsMap;
76+
}
77+
7578
// Dev note: Eleventy doesn't expose typings for its template engines for us to neatly extend.
7679
// Fortunately, it passes both the content string and the file path through to Liquid#parse:
7780
// https://github.com/11ty/eleventy/blob/9c3a7619/src/Engines/Liquid.js#L253
@@ -84,6 +87,11 @@ const stripHtmlComments = (html: string) => html.replace(/<!--[\s\S]*?-->/g, "")
8487
* - generating/expanding sections with auto-generated content
8588
*/
8689
export class CustomLiquid extends Liquid {
90+
termsMap: TermsMap;
91+
constructor(options: CustomLiquidOptions) {
92+
super(options);
93+
this.termsMap = options.termsMap;
94+
}
8795
public parse(html: string, filepath?: string) {
8896
// Filter out Liquid calls for computed data and includes themselves
8997
if (filepath && !filepath.includes("_includes/") && isHtmlFileContent(html)) {
@@ -300,7 +308,7 @@ export class CustomLiquid extends Liquid {
300308
public async render(templates: Template[], scope: GlobalData, options?: RenderOptions) {
301309
// html contains markup after Liquid tags/includes have been processed
302310
const html = (await super.render(templates, scope, options)).toString();
303-
if (!isHtmlFileContent(html) || !scope) return html;
311+
if (!isHtmlFileContent(html) || !scope || scope.page.url === false) return html;
304312

305313
const $ = load(html);
306314

@@ -414,7 +422,7 @@ export class CustomLiquid extends Liquid {
414422
.toLowerCase()
415423
.trim()
416424
.replace(/\s*\n+\s*/, " ");
417-
const term = termsMap[name];
425+
const term = this.termsMap[name];
418426
if (!term) {
419427
console.warn(`${scope.page.inputPath}: Term not found: ${name}`);
420428
return;
@@ -428,7 +436,7 @@ export class CustomLiquid extends Liquid {
428436
const $el = $(el);
429437
const termName = extractTermName($el);
430438
$el
431-
.attr("href", `${scope.guidelinesUrl}#${termName ? termsMap[termName].trId : ""}`)
439+
.attr("href", `${scope.guidelinesUrl}#${termName ? this.termsMap[termName].trId : ""}`)
432440
.attr("target", "terms");
433441
});
434442
} else if (scope.isUnderstanding) {
@@ -442,7 +450,7 @@ export class CustomLiquid extends Liquid {
442450
// since terms may reference other terms in their own definitions.
443451
// Each iteration may append to termNames.
444452
for (let i = 0; i < termNames.length; i++) {
445-
const term = termsMap[termNames[i]];
453+
const term = this.termsMap[termNames[i]];
446454
if (!term) continue; // This will already warn via extractTermNames
447455

448456
const $definition = load(term.definition);
@@ -459,7 +467,7 @@ export class CustomLiquid extends Liquid {
459467
return 0;
460468
});
461469
for (const name of termNames) {
462-
const term = termsMap[name]; // Already verified existence in the earlier loop
470+
const term = this.termsMap[name]; // Already verified existence in the earlier loop
463471
$termsList.append(
464472
`<dt id="${term.id}">${term.name}</dt>` +
465473
`<dd><definition>${term.definition}</definition></dd>`
@@ -469,7 +477,7 @@ export class CustomLiquid extends Liquid {
469477
// Iterate over non-href links once more in now-expanded document to add hrefs
470478
$(termLinkSelector).each((_, el) => {
471479
const name = extractTermName($(el));
472-
el.attribs.href = `#${name ? termsMap[name].id : ""}`;
480+
el.attribs.href = `#${name ? this.termsMap[name].id : ""}`;
473481
});
474482
} else {
475483
// No terms: remove skeleton that was placed in #parse

11ty/guidelines.ts

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -197,14 +197,17 @@ interface Term {
197197
/** id of dfn in TR, which matches original id in terms file */
198198
trId: string;
199199
}
200+
export type TermsMap = Record<string, Term>;
200201

201202
/**
202203
* Resolves term definitions from guidelines/index.html organized for lookup by name;
203204
* comparable to the term elements in wcag.xml from the guidelines-xml Ant task.
204205
*/
205-
export async function getTermsMap() {
206-
const $ = await flattenDomFromFile("guidelines/index.html");
207-
const terms: Record<string, Term> = {};
206+
export async function getTermsMap(version?: WcagVersion) {
207+
const $ = version
208+
? await loadRemoteGuidelines(version)
209+
: await flattenDomFromFile("guidelines/index.html");
210+
const terms: TermsMap = {};
208211

209212
$("dfn").each((_, el) => {
210213
const $el = $(el);
@@ -240,24 +243,25 @@ const loadRemoteGuidelines = async (version: WcagVersion) => {
240243
);
241244

242245
// Re-collapse definition links and notes, to be processed by this build system
243-
$(".guideline a.internalDFN").removeAttr("class data-link-type id href title");
244-
$(".guideline [role='note'] .marker").remove();
245-
$(".guideline [role='note']").find("> div, > p").addClass("note").unwrap();
246+
$("a.internalDFN").removeAttr("class data-link-type id href title");
247+
$("[role='note'] .marker").remove();
248+
$("[role='note']").find("> div, > p").addClass("note").unwrap();
246249

247-
// Bibliography references are not processed in Understanding SC boxes
248-
$(".guideline cite:has(a.bibref:only-child)").each((_, el) => {
250+
// Un-process bibliography references, to be processed by CustomLiquid
251+
$("cite:has(a.bibref:only-child)").each((_, el) => {
249252
const $el = $(el);
250-
const $parent = $el.parent();
251-
$el.remove();
252-
// Remove surrounding square brackets (which aren't in a dedicated element)
253-
$parent.html($parent.html()!.replace(/ \[\]/g, ""));
253+
$el.replaceWith(`[${$el.find("a.bibref").html()}]`);
254254
});
255255

256+
// Remove generated IDs and markers from examples
257+
$(".example[id]").removeAttr("id");
258+
$(".example .marker:has(.self-link)").remove();
259+
256260
// Remove extra markup from headings so they can be parsed for names
257261
$("bdi").remove();
258262

259263
// Remove abbr elements which exist only in TR, not in informative docs
260-
$("#acknowledgements li abbr").each((_, abbrEl) => {
264+
$("#acknowledgements li abbr, #glossary abbr").each((_, abbrEl) => {
261265
$(abbrEl).replaceWith($(abbrEl).text());
262266
});
263267

11ty/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ interface EleventyPage {
99
outputPath: string;
1010
rawInput: string;
1111
templateSyntax: string;
12-
url: string;
12+
url: string | false; // (false when permalink is set to false for the page)
1313
}
1414

1515
interface EleventyDirectories {

eleventy.config.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import {
1414
getFlatGuidelines,
1515
getPrinciples,
1616
getPrinciplesForVersion,
17+
getTermsMap,
1718
scSlugOverrides,
1819
type FlatGuidelinesMap,
1920
type Guideline,
@@ -103,6 +104,8 @@ for (const [technology, list] of Object.entries(techniques)) {
103104
const understandingDocs = await getUnderstandingDocs(version);
104105
const understandingNav = await generateUnderstandingNavMap(principles, understandingDocs);
105106

107+
const termsMap = process.env.WCAG_VERSION ? await getTermsMap(version) : await getTermsMap();
108+
106109
// Declare static global data up-front so we can build typings from it
107110
const globalData = {
108111
version,
@@ -274,6 +277,7 @@ export default function (eleventyConfig: any) {
274277
root: ["_includes", "."],
275278
jsTruthy: true,
276279
strictFilters: true,
280+
termsMap,
277281
})
278282
);
279283

0 commit comments

Comments
 (0)