Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow running builds against a custom local guidelines folder #4182

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions 11ty/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,18 +49,33 @@ but may be useful if you're not seeing what you expect in the output files.

### `WCAG_VERSION`

**Usage context:** for building older versions of techniques and understanding docs
**Usage context:** for building informative docs pinned to a publication version

Indicates WCAG version being built, in `XY` format (i.e. no `.`).
Influences which pages get included, guideline/SC content,
and a few details within pages (e.g. titles/URLs, "New in ..." content).
Also influences base URLs for links to guidelines, techniques, and understanding pages.

Explicitly setting this causes the build to reference guidelines and acknowledgements
published under `w3.org/TR/WCAG{version}`, rather than using the local checkout
(which is effectively the 2.2 Editors' Draft).
(which is effectively the 2.2 Editors' Draft). Note this behavior can be further
altered by `WCAG_FORCE_LOCAL_GUIDELINES`.

Possible values: `22`, `21`

### `WCAG_FORCE_LOCAL_GUIDELINES`

**Usage context:** Only applicable when `WCAG_VERSION` is also set;
should not need to be set manually

When building against a fixed publication version, this overrides the behavior of
loading data from published guidelines, to instead load an alternative local
`guidelines/index.html` (e.g. from a separate git checkout of another branch).
This was implemented to enable preview builds of pull requests targeting the
`WCAG-2.1` branch while reusing the existing build process from `main`.

Possible values: A path relative to the project root, e.g. `../guidelines/index.html`

### `WCAG_MODE`

**Usage context:** should not need to be set manually except in specific testing scenarios
Expand Down
30 changes: 19 additions & 11 deletions 11ty/guidelines.ts
Original file line number Diff line number Diff line change
Expand Up @@ -165,11 +165,11 @@ function processPrinciples($: CheerioAPI) {
}

/**
* Resolves information from guidelines/index.html;
* Resolves information from a local guidelines/index.html source file;
* comparable to the principles section of wcag.xml from the guidelines-xml Ant task.
*/
export const getPrinciples = async () =>
processPrinciples(await flattenDomFromFile("guidelines/index.html"));
export const getPrinciples = async (path = "guidelines/index.html") =>
processPrinciples(await flattenDomFromFile(path));

/**
* Returns a flattened object hash, mapping shortcodes to each principle/guideline/SC.
Expand Down Expand Up @@ -199,14 +199,7 @@ interface Term {
}
export type TermsMap = Record<string, Term>;

/**
* Resolves term definitions from guidelines/index.html organized for lookup by name;
* comparable to the term elements in wcag.xml from the guidelines-xml Ant task.
*/
export async function getTermsMap(version?: WcagVersion) {
const $ = version
? await loadRemoteGuidelines(version)
: await flattenDomFromFile("guidelines/index.html");
function processTermsMap($: CheerioAPI) {
const terms: TermsMap = {};

$("dfn").each((_, el) => {
Expand All @@ -231,6 +224,14 @@ export async function getTermsMap(version?: WcagVersion) {
return terms;
}

/**
* Resolves term definitions from guidelines/index.html (or a specified alternate path)
* organized for lookup by name;
* comparable to the term elements in wcag.xml from the guidelines-xml Ant task.
*/
export const getTermsMap = async (path = "guidelines/index.html") =>
processTermsMap(await flattenDomFromFile(path));

// Version-specific APIs

const remoteGuidelines$: Partial<Record<WcagVersion, CheerioAPI>> = {};
Expand Down Expand Up @@ -299,3 +300,10 @@ export const getAcknowledgementsForVersion = async (version: WcagVersion) => {
*/
export const getPrinciplesForVersion = async (version: WcagVersion) =>
processPrinciples(await loadRemoteGuidelines(version));

/**
* Resolves term definitions from a WCAG 2.x publication,
* organized for lookup by name.
*/
export const getTermsMapForVersion = async (version: WcagVersion) =>
processTermsMap(await loadRemoteGuidelines(version));
22 changes: 18 additions & 4 deletions eleventy.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
getPrinciples,
getPrinciplesForVersion,
getTermsMap,
getTermsMapForVersion,
scSlugOverrides,
type FlatGuidelinesMap,
type Guideline,
Expand Down Expand Up @@ -56,10 +57,16 @@ const allPrinciples = await getPrinciples();
/** Flattened Principles/Guidelines/SC across all versions (including later than selected) */
const allFlatGuidelines = getFlatGuidelines(allPrinciples);

async function resolveRelevantPrinciples() {
if (!process.env.WCAG_VERSION) return allPrinciples;
if (process.env.WCAG_FORCE_LOCAL_GUIDELINES)
return await getPrinciples(process.env.WCAG_FORCE_LOCAL_GUIDELINES);
assertIsWcagVersion(version);
return await getPrinciplesForVersion(version);
}

/** Tree of Principles/Guidelines/SC relevant to selected version */
const principles = process.env.WCAG_VERSION
? await getPrinciplesForVersion(version)
: allPrinciples;
const principles = await resolveRelevantPrinciples();
/** Flattened Principles/Guidelines/SC relevant to selected version */
const flatGuidelines = getFlatGuidelines(principles);
/** Flattened Principles/Guidelines/SC that only exist in later versions (to filter techniques) */
Expand Down Expand Up @@ -104,7 +111,14 @@ for (const [technology, list] of Object.entries(techniques)) {
const understandingDocs = await getUnderstandingDocs(version);
const understandingNav = await generateUnderstandingNavMap(principles, understandingDocs);

const termsMap = process.env.WCAG_VERSION ? await getTermsMap(version) : await getTermsMap();
function resolveRelevantTermsMap() {
if (!process.env.WCAG_VERSION) return getTermsMap();
if (process.env.WCAG_FORCE_LOCAL_GUIDELINES)
return getTermsMap(process.env.WCAG_FORCE_LOCAL_GUIDELINES);
assertIsWcagVersion(version);
return getTermsMapForVersion(version);
}
const termsMap = await resolveRelevantTermsMap();

// Declare static global data up-front so we can build typings from it
const globalData = {
Expand Down