|
| 1 | +/*! |
| 2 | + * @license |
| 3 | + * Copyright Google LLC All Rights Reserved. |
| 4 | + * |
| 5 | + * Use of this source code is governed by an MIT-style license that can be |
| 6 | + * found in the LICENSE file at https://angular.dev/license |
| 7 | + */ |
| 8 | + |
| 9 | +import {ALL_ITEMS} from '../../src/app/routing/navigation-entries'; |
| 10 | +import {NavigationItem} from '@angular/docs'; |
| 11 | +import {writeFileSync, readFileSync} from 'fs'; |
| 12 | +import {join, resolve} from 'path'; |
| 13 | + |
| 14 | +const outputFile = 'defined-routes.json'; |
| 15 | +const contentRoot = resolve(process.cwd(), '../../src/content'); |
| 16 | + |
| 17 | +/** |
| 18 | + * The scripts generates a list of all defined routes in the guides section and stores them |
| 19 | + * in a JSON file. This file then used by other bazel targets to know which routes are valid. |
| 20 | + */ |
| 21 | + |
| 22 | +function extractRoutes(items: NavigationItem[]): string[] { |
| 23 | + const routes: string[] = []; |
| 24 | + for (const item of items) { |
| 25 | + if (item.path && !item.path.startsWith('http')) { |
| 26 | + routes.push(item.path); |
| 27 | + if (item.contentPath) { |
| 28 | + const content = readFileSync(join(contentRoot, `${item.contentPath}.md`), { |
| 29 | + encoding: 'utf-8', |
| 30 | + }); |
| 31 | + const headings = extractHeadings(content); |
| 32 | + routes.push( |
| 33 | + ...headings.map( |
| 34 | + (heading) => `${item.path}#${heading.toLowerCase().replace(/\s+/g, '-')}`, |
| 35 | + ), |
| 36 | + ); |
| 37 | + } |
| 38 | + } |
| 39 | + if (item.children) { |
| 40 | + routes.push(...extractRoutes(item.children)); |
| 41 | + } |
| 42 | + } |
| 43 | + return routes; |
| 44 | +} |
| 45 | + |
| 46 | +function extractHeadings(content: string): string[] { |
| 47 | + const headings = content |
| 48 | + .split('\n') |
| 49 | + // Top level heading (H1) are used for the page title only |
| 50 | + // and yes, headings can have leading spaces |
| 51 | + .filter((line) => line.trim().startsWith('##')) |
| 52 | + .map((line) => line.replace(/^#+\s*/, '').trim()); |
| 53 | + |
| 54 | + const stepRegex = /<docs-step[^>]*title="([^"]*)"/g; |
| 55 | + let match; |
| 56 | + while ((match = stepRegex.exec(content)) !== null) { |
| 57 | + headings.push(match[1]); |
| 58 | + } |
| 59 | + |
| 60 | + return headings.map((heading: string) => getIdFromHeading(heading)); |
| 61 | +} |
| 62 | + |
| 63 | +function main() { |
| 64 | + const allRoutes: string[] = []; |
| 65 | + |
| 66 | + allRoutes.push(...extractRoutes(ALL_ITEMS)); |
| 67 | + |
| 68 | + const uniqueRoutes = Array.from(new Set(allRoutes.filter((r) => !!r))); |
| 69 | + |
| 70 | + console.warn('Generated routes:', JSON.stringify(uniqueRoutes, null, 2)); |
| 71 | + writeFileSync(outputFile, JSON.stringify(uniqueRoutes, null, 2)); |
| 72 | +} |
| 73 | + |
| 74 | +main(); |
| 75 | + |
| 76 | +// TODO: refactor so this function is shared with the generation pipeline (adev/shared-docs/pipeline/shared/marked/transformations/heading.mts) |
| 77 | +function getIdFromHeading(heading: string): string { |
| 78 | + return heading |
| 79 | + .toLowerCase() |
| 80 | + .replace(/\s|\//g, '-') // replace spaces and slashes with dashes |
| 81 | + .replace(/[^\p{L}\d\-]/gu, ''); // only keep letters, digits & dashes |
| 82 | +} |
0 commit comments