Skip to content

Commit 6d594a6

Browse files
committed
Update getErrataForVersion for template format and resolve note links
1 parent 485da7a commit 6d594a6

File tree

1 file changed

+66
-49
lines changed

1 file changed

+66
-49
lines changed

11ty/guidelines.ts

Lines changed: 66 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -233,50 +233,52 @@ export async function getTermsMap(version?: WcagVersion) {
233233

234234
// Version-specific APIs
235235

236-
const remoteGuidelines$: Partial<Record<WcagVersion, CheerioAPI>> = {};
236+
const guidelinesCache: Partial<Record<WcagVersion, string>> = {};
237237

238238
/** Loads guidelines from TR space for specific version, caching for future calls. */
239-
const loadRemoteGuidelines = async (version: WcagVersion) => {
240-
if (!remoteGuidelines$[version]) {
241-
const $ = load(
242-
(await axios.get(`https://www.w3.org/TR/WCAG${version}/`, { responseType: "text" })).data
243-
);
244-
245-
// Re-collapse definition links and notes, to be processed by this build system
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();
249-
250-
// Convert data-plurals (present in publications) to data-lt
251-
$("dfn[data-plurals]").each((_, el) => {
252-
el.attribs["data-lt"] = (el.attribs["data-lt"] || "")
253-
.split("|")
254-
.concat(el.attribs["data-plurals"].split("|"))
255-
.join("|");
256-
delete el.attribs["data-plurals"];
257-
});
239+
const loadRemoteGuidelines = async (version: WcagVersion, stripRespec = true) => {
240+
const html =
241+
guidelinesCache[version] ||
242+
(guidelinesCache[version] = (
243+
await axios.get(`https://www.w3.org/TR/WCAG${version}/`, { responseType: "text" })
244+
).data);
245+
246+
const $ = load(html);
247+
if (!stripRespec) return $;
248+
249+
// Re-collapse definition links and notes, to be processed by this build system
250+
$("a.internalDFN").removeAttr("class data-link-type id href title");
251+
$("[role='note'] .marker").remove();
252+
$("[role='note']").find("> div, > p").addClass("note").unwrap();
253+
254+
// Convert data-plurals (present in publications) to data-lt
255+
$("dfn[data-plurals]").each((_, el) => {
256+
el.attribs["data-lt"] = (el.attribs["data-lt"] || "")
257+
.split("|")
258+
.concat(el.attribs["data-plurals"].split("|"))
259+
.join("|");
260+
delete el.attribs["data-plurals"];
261+
});
258262

259-
// Un-process bibliography references, to be processed by CustomLiquid
260-
$("cite:has(a.bibref:only-child)").each((_, el) => {
261-
const $el = $(el);
262-
$el.replaceWith(`[${$el.find("a.bibref").html()}]`);
263-
});
263+
// Un-process bibliography references, to be processed by CustomLiquid
264+
$("cite:has(a.bibref:only-child)").each((_, el) => {
265+
const $el = $(el);
266+
$el.replaceWith(`[${$el.find("a.bibref").html()}]`);
267+
});
264268

265-
// Remove generated IDs and markers from examples
266-
$(".example[id]").removeAttr("id");
267-
$(".example > .marker").remove();
269+
// Remove generated IDs and markers from examples
270+
$(".example[id]").removeAttr("id");
271+
$(".example > .marker").remove();
268272

269-
// Remove extra markup from headings so they can be parsed for names
270-
$("bdi").remove();
273+
// Remove extra markup from headings so they can be parsed for names
274+
$("bdi").remove();
271275

272-
// Remove abbr elements which exist only in TR, not in informative docs
273-
$("#acknowledgements li abbr, #glossary abbr").each((_, abbrEl) => {
274-
$(abbrEl).replaceWith($(abbrEl).text());
275-
});
276+
// Remove abbr elements which exist only in TR, not in informative docs
277+
$("#acknowledgements li abbr, #glossary abbr").each((_, abbrEl) => {
278+
$(abbrEl).replaceWith($(abbrEl).text());
279+
});
276280

277-
remoteGuidelines$[version] = $;
278-
}
279-
return remoteGuidelines$[version]!;
281+
return $;
280282
};
281283

282284
/**
@@ -303,20 +305,35 @@ export const getPrinciplesForVersion = async (version: WcagVersion) =>
303305
/** Parses errata items from the errata document for the specified WCAG version. */
304306
export const getErrataForVersion = async (version: WcagVersion) => {
305307
const $ = await loadFromFile(join("errata", `${version}.html`));
306-
const aSelector = `a[href^='https://www.w3.org/TR/WCAG${version}/#']`;
308+
const $guidelines = await loadRemoteGuidelines(version, false);
309+
const aSelector = `a[href*='#']:first-of-type`;
307310
const errata: Record<string, string[]> = {};
308311

309-
$(`li:has(${aSelector})`).each((_, el) => {
310-
const $el = $(el);
311-
const $aEl = $el.find(aSelector);
312-
const hash = new URL($aEl.attr("href")!).hash.slice(1);
313-
const erratumHtml = $el
314-
.html()!
315-
.replace(/^.*?<\/a>,?\s*/g, "")
316-
.replace(/^(\w)/, (_, p1) => p1.toUpperCase());
317-
if (hash in errata) errata[hash].push(erratumHtml);
318-
else errata[hash] = [erratumHtml];
319-
});
312+
$("main > section[id]")
313+
.last()
314+
.find(`li:has(${aSelector})`)
315+
.each((_, el) => {
316+
const $el = $(el);
317+
const $aEl = $el.find(aSelector);
318+
let hash: string | undefined = $aEl.attr("href")!.replace(/^.*#/, "");
319+
320+
// Check whether hash pertains to a guideline/SC section or term definition;
321+
// if it doesn't, attempt to resolve it to one
322+
const $hashEl = $guidelines(`#${hash}`);
323+
if (!$hashEl.is("section.guideline, #terms dfn")) {
324+
const $closest = $hashEl.closest("#terms dd, section.guideline");
325+
if ($closest.is("#terms dd")) hash = $closest.prev().find("dfn[id]").attr("id");
326+
else hash = $closest.attr("id");
327+
}
328+
if (!hash) return;
329+
330+
const erratumHtml = $el
331+
.html()!
332+
.replace(/^.*?<\/a>,?\s*/g, "")
333+
.replace(/^(\w)/, (_, p1) => p1.toUpperCase());
334+
if (hash in errata) errata[hash].push(erratumHtml);
335+
else errata[hash] = [erratumHtml];
336+
});
320337

321338
return errata;
322339
};

0 commit comments

Comments
 (0)