1
1
import { getCollection , getEntry , type CollectionEntry , type CollectionKey } from "astro:content" ;
2
2
import capitalize from "lodash-es/capitalize" ;
3
+ import difference from "lodash-es/difference" ;
4
+
5
+ import { isDevOrPreview } from "./constants" ;
3
6
4
7
/**
5
- * Returns a filtered list of requirement/assertion slugs under the given group and guideline,
6
- * excluding any marked as needing additional research if WCAG_SKIP_WIP is set.
8
+ * Returns a list of requirement/assertion slugs under the given group and guideline that would be
9
+ * skipped for a major publication (i.e. exploratory/placeholder or needs additional research).
10
+ * Note this returns an empty array when building for situations that do not allow for skipping
11
+ * either at build time or client-side (e.g. Editor's Drafts).
7
12
*/
8
- async function getFilteredRequirements ( groupId : string , guidelineSlug : string ) {
13
+ async function determineSkippableRequirements ( groupId : string , guidelineSlug : string ) {
14
+ // Only populate when running a build that needs to skip at build time or client-side
15
+ if ( ! import . meta. env . WCAG_SKIP_WIP && ! isDevOrPreview ) return [ ] ;
16
+
9
17
const guideline = await getEntry ( "guidelines" , `${ groupId } /${ guidelineSlug } ` ) ;
10
18
if ( ! guideline ) throw new Error ( `Unresolvable guideline ID: ${ guidelineSlug } ` ) ;
11
19
12
- const filteredRequirements : string [ ] = [ ] ;
20
+ const skippableRequirements : string [ ] = [ ] ;
13
21
for ( const requirementSlug of guideline . data . children ) {
14
22
const requirement = await getEntry (
15
23
"requirements" ,
16
24
`${ groupId } /${ guidelineSlug } /${ requirementSlug } `
17
25
) ;
18
26
if ( ! requirement ) throw new Error ( `Unresolvable requirement ID: ${ requirementSlug } ` ) ;
19
27
if (
20
- import . meta. env . WCAG_SKIP_WIP &&
21
- ( requirement . data . needsAdditionalResearch ||
22
- requirement . data . status === "placeholder" ||
23
- requirement . data . status === "exploratory" )
24
- )
25
- continue ;
26
- filteredRequirements . push ( requirementSlug ) ;
28
+ requirement . data . needsAdditionalResearch ||
29
+ requirement . data . status === "placeholder" ||
30
+ requirement . data . status === "exploratory"
31
+ ) {
32
+ skippableRequirements . push ( requirementSlug ) ;
33
+ }
27
34
}
28
- return filteredRequirements ;
35
+ return skippableRequirements ;
36
+ }
37
+
38
+ /** Extension of Requirement to facilitate client-side skipping in previews */
39
+ interface SkippableRequirement extends CollectionEntry < "requirements" > {
40
+ skippable ?: boolean ;
29
41
}
30
42
31
43
let groupIds = ( await getCollection ( "groupOrder" ) ) . map ( ( { id } ) => id ) ;
32
44
let groups : Record < string , CollectionEntry < "groups" > > = { } ;
33
45
let guidelines : Record < string , CollectionEntry < "guidelines" > > = { } ;
34
- let requirements : Record < string , CollectionEntry < "requirements" > > = { } ;
46
+ let requirements : Record < string , SkippableRequirement > = { } ;
35
47
36
48
export async function buildGuidelinesHierarchy ( ) {
37
49
// Cache collated collection data for subsequent calls
@@ -44,16 +56,22 @@ export async function buildGuidelinesHierarchy() {
44
56
for ( const guidelineSlug of group . data . children ) {
45
57
const guideline = await getEntry ( "guidelines" , `${ groupId } /${ guidelineSlug } ` ) ;
46
58
if ( ! guideline ) throw new Error ( `Unresolvable guideline ID: ${ guidelineSlug } ` ) ;
47
- guideline . data . children = await getFilteredRequirements ( groupId , guidelineSlug ) ;
48
59
guidelines [ guideline . id ] = guideline ;
49
60
61
+ const skippableChildren = await determineSkippableRequirements ( groupId , guidelineSlug ) ;
62
+ if ( import . meta. env . WCAG_SKIP_WIP )
63
+ // Filter children directly in case of build-time skip
64
+ guideline . data . children = difference ( guideline . data . children , skippableChildren ) ;
65
+
50
66
for ( const requirementSlug of guideline . data . children ) {
51
67
const requirement = await getEntry (
52
68
"requirements" ,
53
69
`${ groupId } /${ guidelineSlug } /${ requirementSlug } `
54
70
) ;
55
71
if ( ! requirement ) throw new Error ( `Unresolvable requirement ID: ${ requirementSlug } ` ) ;
56
- requirements [ requirement . id ] = requirement ;
72
+ requirements [ requirement . id ] = skippableChildren . includes ( requirementSlug )
73
+ ? { ...requirement , skippable : true }
74
+ : requirement ;
57
75
}
58
76
}
59
77
}
0 commit comments