1
1
import { defineConfig } from "astro/config" ;
2
2
import node from "@astrojs/node" ;
3
- // TODO: dynamically import cheerio only when needed,
4
- // when https://github.com/withastro/astro/issues/12689 is resolved
3
+ import { parseFrontmatter } from "@astrojs/markdown-remark" ;
5
4
import { load } from "cheerio" ;
6
5
import fg from "fast-glob" ;
7
6
import { remarkDefinitionList , defListHastHandlers } from "remark-definition-list" ;
8
7
import remarkDirective from "remark-directive" ;
9
8
10
9
import { readFile , writeFile } from "fs/promises" ;
11
- import { join } from "path" ;
10
+ import { basename , dirname , join } from "path" ;
12
11
import { fileURLToPath } from "url" ;
13
12
14
13
import { guidelinesRehypePlugins , guidelinesRemarkPlugins } from "./src/lib/markdown/guidelines" ;
@@ -28,14 +27,63 @@ export default defineConfig({
28
27
rehypePlugins : [ ...guidelinesRehypePlugins ] ,
29
28
remarkRehype : {
30
29
// https://github.com/wataru-chocola/remark-definition-list/issues/50#issuecomment-1445130314
31
- handlers : { ...defListHastHandlers }
32
- }
30
+ handlers : { ...defListHastHandlers } ,
31
+ } ,
33
32
} ,
34
33
experimental : {
35
34
contentIntellisense : true ,
36
35
preserveScriptOrder : true ,
37
36
} ,
38
37
integrations : [
38
+ {
39
+ /** Checks for mismatched children array vs. subdirectory contents */
40
+ name : "children-check" ,
41
+ hooks : {
42
+ "astro:build:start" : async ( ) => {
43
+ const groupsPath = join ( "guidelines" , "groups" ) ;
44
+ const groupFilenames = await fg . glob ( join ( groupsPath , "*.json" ) , {
45
+ ignore : [ join ( groupsPath , "index.json" ) ] ,
46
+ } ) ;
47
+
48
+ // Check at group level (index.json -> *.json)
49
+ const listedCount = JSON . parse (
50
+ await readFile ( join ( groupsPath , "index.json" ) , "utf8" )
51
+ ) . length ;
52
+ const actualCount = groupFilenames . length ;
53
+ if ( listedCount !== actualCount ) {
54
+ throw new Error (
55
+ `Group index.json lists ${ listedCount } children but there are ${ actualCount } files`
56
+ ) ;
57
+ }
58
+
59
+ // Check at group->guideline level (*.json -> */*.md)
60
+ for ( const filename of groupFilenames ) {
61
+ const id = basename ( filename , ".json" ) ;
62
+ const data = JSON . parse ( await readFile ( filename , "utf8" ) ) ;
63
+
64
+ const actualCount = ( await fg . glob ( join ( groupsPath , id , "*.md" ) ) ) . length ;
65
+ if ( data . children . length !== actualCount ) {
66
+ throw new Error (
67
+ `Group ${ id } lists ${ data . children . length } children but has ${ actualCount } files`
68
+ ) ;
69
+ }
70
+ }
71
+
72
+ // Check at guideline level (*/*.md -> */*/*.md)
73
+ for ( const filename of await fg . glob ( join ( groupsPath , "*" , "*.md" ) ) ) {
74
+ const id = join ( basename ( dirname ( filename ) ) , basename ( filename , ".md" ) ) ;
75
+ const data = parseFrontmatter ( await readFile ( filename , "utf8" ) ) . frontmatter ;
76
+
77
+ const actualCount = ( await fg . glob ( join ( groupsPath , id , "*.md" ) ) ) . length ;
78
+ if ( data . children . length !== actualCount ) {
79
+ throw new Error (
80
+ `Group ${ id } lists ${ data . children . length } children but has ${ actualCount } files`
81
+ ) ;
82
+ }
83
+ }
84
+ } ,
85
+ } ,
86
+ } ,
39
87
{
40
88
/** Filters output to reduce diff noise, esp. due to script/style/dependency updates */
41
89
name : "diffable-html" ,
0 commit comments