Skip to content

Commit 0df965a

Browse files
authored
Generate guidelines during Netlify PR preview builds (#4141)
This effectively replaces what the pr-preview bot currently does (and what #2220 sought to do within the repo). The problem with the pr-preview bot is its previews don't fully work (e.g. as seen in #4123). ## Changes - Remove `.pr-preview.json` to stop pr-preview bot from running - Add logic to `eleventy.config.ts` to copy `guidelines` folder assets and run through spec-generator (the same thing we use for GH Pages builds; also the same thing used by the pr-preview bot, IIUC) when running a standard build through netlify - Conditionally add guidelines list item to top-level `index.html` (which is only used for local dev and PR builds) - Tangentially, rename our github-pages push workflow to be more distinguishable in the Actions tab
1 parent b99ada8 commit 0df965a

File tree

5 files changed

+46
-7
lines changed

5 files changed

+46
-7
lines changed

.github/workflows/11ty-publish.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
name: CI
1+
name: Push to gh-pages branch
22

33
# Reference documentation: https://docs.github.com/en/actions/reference
44

.pr-preview.json

-4
This file was deleted.

eleventy.config.ts

+29-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,10 @@
1+
import axios from "axios";
12
import compact from "lodash-es/compact";
3+
import { mkdirp } from "mkdirp";
24
import { rimraf } from "rimraf";
35

4-
import { copyFile } from "fs/promises";
6+
import { copyFile, writeFile } from "fs/promises";
7+
import { join } from "path";
58

69
import { CustomLiquid } from "11ty/CustomLiquid";
710
import {
@@ -184,7 +187,31 @@ export default function (eleventyConfig: any) {
184187
eleventyConfig.on("eleventy.after", async ({ dir }: EleventyEvent) => {
185188
// addPassthroughCopy can only map each file once,
186189
// but base.css needs to be copied to a 2nd destination
187-
await copyFile(`${dir.input}/css/base.css`, `${dir.output}/understanding/base.css`);
190+
await copyFile(
191+
join(dir.input, "css", "base.css"),
192+
join(dir.output, "understanding", "base.css")
193+
);
194+
195+
// Output guidelines/index.html and dependencies for PR runs (not for GH Pages or W3C site)
196+
const sha = process.env.COMMIT_REF; // Read environment variable exposed by Netlify
197+
if (sha && !process.env.WCAG_MODE) {
198+
await mkdirp(join(dir.output, "guidelines"));
199+
await copyFile(
200+
join(dir.input, "guidelines", "guidelines.css"),
201+
join(dir.output, "guidelines", "guidelines.css")
202+
);
203+
await copyFile(
204+
join(dir.input, "guidelines", "relative-luminance.html"),
205+
join(dir.output, "guidelines", "relative-luminance.html")
206+
);
207+
208+
const url = `https://raw.githack.com/${GH_ORG}/${GH_REPO}/${sha}/guidelines/index.html?isPreview=true`;
209+
const { data: processedGuidelines } = await axios.get(
210+
`https://labs.w3.org/spec-generator/?type=respec&url=${encodeURIComponent(url)}`,
211+
{ responseType: "text" }
212+
);
213+
await writeFile(`${dir.output}/guidelines/index.html`, processedGuidelines);
214+
}
188215
});
189216

190217
eleventyConfig.setLibrary(

index.html

+15
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1+
---js
2+
// Same condition used for copying guidelines assets in eleventy.config.ts
3+
const includeGuidelines = process.env.COMMIT_REF && !process.env.WCAG_MODE;
4+
const repoUrl = process.env.REPOSITORY_URL;
5+
const prNumber = process.env.REVIEW_ID;
6+
---
17
<!DOCTYPE html>
28
<html lang="en">
39
<head>
@@ -6,7 +12,16 @@
612
</head>
713
<body>
814
<h1>Web Content Accessibility Guidelines {{ versionDecimal }}</h1>
15+
{% if repoUrl and prNumber -%}
16+
<p>
17+
<strong>Note:</strong> This is a <strong>preview</strong> for
18+
<a href="{{ repoUrl }}/pull/{{ prNumber }}">PR #{{ prNumber }}</a>.
19+
</p>
20+
{%- endif %}
921
<ul>
22+
{% if includeGuidelines -%}
23+
<li><a href="guidelines/">Guidelines</a></li>
24+
{%- endif %}
1025
<li><a href="techniques/">Techniques</a></li>
1126
<li><a href="understanding/">Understanding Docs</a></li>
1227
</ul>

package.json

+1
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
"license": "W3C",
1616
"dependencies": {
1717
"@11ty/eleventy": "^3.0.0",
18+
"axios": "^1.7.7",
1819
"cheerio": "^1.0.0",
1920
"glob": "^10.3.16",
2021
"gray-matter": "^4.0.3",

0 commit comments

Comments
 (0)