1+ import { type NavigationItem } from "@react-md/core/navigation/types" ;
2+ import { alphaNumericSort } from "@react-md/core/utils/alphaNumericSort" ;
13import { log , logComplete } from "docs-generator/utils/log" ;
24import { existsSync } from "node:fs" ;
35import { writeFile } from "node:fs/promises" ;
46import { format } from "prettier" ;
5- import { type GeneratedSassDocWithOrder , generate } from "sassdoc-generator" ;
7+ import {
8+ type GeneratedSassDoc ,
9+ type GeneratedSassDocWithOrder ,
10+ generate ,
11+ } from "sassdoc-generator" ;
612import { type FormattedSassDocItem } from "sassdoc-generator/types" ;
713
14+ import { titleCase } from "../src/utils/strings.js" ;
815import {
916 ALIASED_SASSDOC_FILE ,
17+ ALIASED_SASSDOC_NAV_ITEMS_FILE ,
1018 CORE_SRC ,
1119 GENERATED_FILE_BANNER ,
1220 GENERATED_SASSDOC_FILE ,
21+ GENERATED_SASSDOC_NAV_ITEMS_FILE ,
1322} from "./constants.js" ;
1423import { ensureGeneratedDir } from "./ensureGeneratedDir.js" ;
1524
@@ -50,25 +59,104 @@ export const SASSDOC_VARIABLES: Record<string, FormattedVariableItem | undefined
5059 GENERATED_SASSDOC_FILE ,
5160 await format ( contents , { parser : "typescript" } )
5261 ) ;
62+ logComplete ( `Generated "${ ALIASED_SASSDOC_FILE } "` ) ;
63+ }
64+
65+ async function generateNavItems ( options : GeneratedSassDoc ) : Promise < void > {
66+ const { mixins, functions, variables } = options ;
67+ const groups = new Set < string > ( ) ;
68+ mixins . forEach ( ( item ) => groups . add ( item . group ) ) ;
69+ functions . forEach ( ( item ) => groups . add ( item . group ) ) ;
70+ variables . forEach ( ( item ) => groups . add ( item . group ) ) ;
71+
72+ const components : ( NavigationItem & { children : string } ) [ ] = [ ] ;
73+ const remainingItems : ( NavigationItem & { children : string } ) [ ] = [ ] ;
74+ groups . forEach ( ( group ) => {
75+ if ( group . startsWith ( "core." ) ) {
76+ const withoutCore = group . replace ( "core." , "" ) ;
77+ if ( withoutCore === "form" ) {
78+ components . push ( {
79+ type : "route" ,
80+ href : "/form" ,
81+ children : "Form" ,
82+ } ) ;
83+ } else {
84+ remainingItems . push ( {
85+ type : "route" ,
86+ href : `/${ withoutCore } ` ,
87+ children : titleCase ( withoutCore , "-" ) ,
88+ } ) ;
89+ }
90+ } else {
91+ components . push ( {
92+ type : "route" ,
93+ href : `/${ group } ` ,
94+ children : titleCase ( group , "-" ) ,
95+ } ) ;
96+ }
97+ } ) ;
98+
99+ await writeFile (
100+ GENERATED_SASSDOC_NAV_ITEMS_FILE ,
101+ await format (
102+ `${ GENERATED_FILE_BANNER }
103+ import { type NavigationItem } from "@react-md/core/navigation/types";
104+
105+ export const SASSDOC_NAV_ITEMS: readonly NavigationItem[] = [
106+ {
107+ type: "group",
108+ href: "/sassdoc",
109+ children: "Sass API Docs",
110+ items: [
111+ { type: "subheader", children: "Core" },
112+ ${ alphaNumericSort ( remainingItems , { extractor : ( item ) => item . children } )
113+ . map ( ( item ) => JSON . stringify ( item ) )
114+ . join ( "," ) } ,
115+ { type: "subheader", children: "Components" },
116+ ${ alphaNumericSort ( components , { extractor : ( item ) => item . children } )
117+ . map ( ( item ) => JSON . stringify ( item ) )
118+ . join ( "," ) } ,
119+ ]
120+ },
121+ ]
122+ ` ,
123+ { filepath : GENERATED_SASSDOC_NAV_ITEMS_FILE }
124+ )
125+ ) ;
126+ logComplete ( `Generated "${ ALIASED_SASSDOC_NAV_ITEMS_FILE } "` ) ;
53127}
54128
55129if ( process . argv . includes ( "--touch" ) ) {
130+ const empty : GeneratedSassDocWithOrder = {
131+ mixins : new Map ( ) ,
132+ functions : new Map ( ) ,
133+ variables : new Map ( ) ,
134+ mixinsOrder : new Map ( ) ,
135+ functionsOrder : new Map ( ) ,
136+ variablesOrder : new Map ( ) ,
137+ } ;
138+ const update = "Run `pnpm --filter docs sassdoc` to update." ;
139+
140+ if ( ! existsSync ( GENERATED_SASSDOC_NAV_ITEMS_FILE ) ) {
141+ await log (
142+ generateNavItems ( empty ) ,
143+ "" ,
144+ `Created an empty "${ ALIASED_SASSDOC_NAV_ITEMS_FILE } ". ${ update } `
145+ ) ;
146+ } else {
147+ logComplete (
148+ `Skipped creating "${ ALIASED_SASSDOC_NAV_ITEMS_FILE } " since it already exists. ${ update } `
149+ ) ;
150+ }
56151 if ( ! existsSync ( GENERATED_SASSDOC_FILE ) ) {
57152 await log (
58- createSassDocFile ( {
59- mixins : new Map ( ) ,
60- functions : new Map ( ) ,
61- variables : new Map ( ) ,
62- mixinsOrder : new Map ( ) ,
63- functionsOrder : new Map ( ) ,
64- variablesOrder : new Map ( ) ,
65- } ) ,
153+ createSassDocFile ( empty ) ,
66154 "" ,
67- `Created an empty "${ ALIASED_SASSDOC_FILE } ". Run \`pnpm --filter docs sassdoc\` to update. `
155+ `Created an empty "${ ALIASED_SASSDOC_FILE } ". ${ update } `
68156 ) ;
69157 } else {
70158 logComplete (
71- `Skipped creating "${ ALIASED_SASSDOC_FILE } " since it already exists. Run \`pnpm --filter docs sassdoc\` to update. `
159+ `Skipped creating "${ ALIASED_SASSDOC_FILE } " since it already exists. ${ update } `
72160 ) ;
73161 }
74162
@@ -77,7 +165,11 @@ if (process.argv.includes("--touch")) {
77165
78166async function run ( ) : Promise < void > {
79167 await ensureGeneratedDir ( ) ;
80- await createSassDocFile ( await generate ( { src : CORE_SRC } ) ) ;
168+ const generated = await generate ( { src : CORE_SRC } ) ;
169+ await Promise . all ( [
170+ createSassDocFile ( generated ) ,
171+ generateNavItems ( generated ) ,
172+ ] ) ;
81173}
82174
83- await log ( run ( ) , "" , `Created " ${ ALIASED_SASSDOC_FILE } "` ) ;
175+ await log ( run ( ) , "" , "sassdoc script completed" ) ;
0 commit comments