|
1 | 1 | import { writeFileSync, readFileSync } from 'fs';
|
2 |
| -import { packageCategories } from '../src/data/api-categories.mjs'; |
3 |
| -import { processReferences } from '../src/data/process-typedoc.mjs'; |
| 2 | +import { |
| 3 | + API_CATEGORIES, |
| 4 | + API_SUB_CATEGORIES, |
| 5 | + ROOT_PACKAGE |
| 6 | +} from '../src/data/api-categories.mjs'; |
4 | 7 |
|
5 | 8 | /**
|
6 | 9 | * The purpose of this script is to create generate an object that only contains
|
@@ -28,9 +31,6 @@ const packageName = process.argv[packageIndex + 1];
|
28 | 31 |
|
29 | 32 | const referencesFile = readFileSync(`../${packageName}/docs/reference.json`);
|
30 | 33 |
|
31 |
| -const { API_CATEGORIES, API_SUB_CATEGORIES, ROOT_PACKAGE } = |
32 |
| - packageCategories[packageName]; |
33 |
| - |
34 | 34 | const references = processReferences(JSON.parse(referencesFile), ROOT_PACKAGE);
|
35 | 35 | const cleanReferences = {};
|
36 | 36 | const categoryNodes = [];
|
@@ -155,6 +155,77 @@ function recursivelyPopulateNodes(node) {
|
155 | 155 | }
|
156 | 156 | }
|
157 | 157 |
|
| 158 | +function processReferences(references, rootPackage) { |
| 159 | + // build flat object for easier faster lookups |
| 160 | + const flatReferences = {}; |
| 161 | + |
| 162 | + const recursivelyPopulateFlatObject = (referenceObject) => { |
| 163 | + if (!referenceObject) return; |
| 164 | + if (referenceObject['id']) { |
| 165 | + const copy = recursivelyStripObject(structuredClone(referenceObject)); |
| 166 | + flatReferences[referenceObject['id']] = copy; |
| 167 | + } |
| 168 | + |
| 169 | + for (let key in referenceObject) { |
| 170 | + if (referenceObject.hasOwnProperty(key)) { |
| 171 | + if (Array.isArray(referenceObject[key])) { |
| 172 | + referenceObject[key].forEach((child) => { |
| 173 | + recursivelyPopulateFlatObject(child); |
| 174 | + }); |
| 175 | + } else if ( |
| 176 | + typeof referenceObject[key] === 'object' && |
| 177 | + referenceObject[key] !== null |
| 178 | + ) { |
| 179 | + recursivelyPopulateFlatObject(referenceObject[key]); |
| 180 | + } |
| 181 | + } |
| 182 | + } |
| 183 | + }; |
| 184 | + |
| 185 | + const recursivelyStripObject = (referenceObject) => { |
| 186 | + for (let key in referenceObject) { |
| 187 | + if (referenceObject.hasOwnProperty(key)) { |
| 188 | + if (Array.isArray(referenceObject[key])) { |
| 189 | + referenceObject[key] = referenceObject[key].map((child) => { |
| 190 | + return child.id || child; |
| 191 | + }); |
| 192 | + } else if ( |
| 193 | + typeof referenceObject[key] === 'object' && |
| 194 | + referenceObject[key] !== null |
| 195 | + ) { |
| 196 | + recursivelyStripObject(referenceObject[key]); |
| 197 | + } |
| 198 | + } |
| 199 | + } |
| 200 | + return referenceObject; |
| 201 | + }; |
| 202 | + |
| 203 | + const isFunctionObject = (obj) => { |
| 204 | + return obj.kind === 64 && obj.variant === 'declaration'; |
| 205 | + }; |
| 206 | + recursivelyPopulateFlatObject(references); |
| 207 | + |
| 208 | + const rootId = Object.keys(flatReferences).find( |
| 209 | + (id) => flatReferences[id].name == rootPackage |
| 210 | + ); |
| 211 | + |
| 212 | + flatReferences['categories'] = flatReferences[rootId]?.children?.map( |
| 213 | + (catId) => { |
| 214 | + const cat = structuredClone(flatReferences[catId]); |
| 215 | + if (cat.children && Array.isArray(cat.children)) { |
| 216 | + cat.children = cat.children |
| 217 | + .map((childId) => flatReferences[childId]) |
| 218 | + .filter((child) => { |
| 219 | + return isFunctionObject(child); |
| 220 | + }); |
| 221 | + } |
| 222 | + return cat; |
| 223 | + } |
| 224 | + ); |
| 225 | + |
| 226 | + return flatReferences; |
| 227 | +} |
| 228 | + |
158 | 229 | const categories = Object.values(API_CATEGORIES).concat(
|
159 | 230 | Object.values(API_SUB_CATEGORIES)
|
160 | 231 | );
|
|
0 commit comments