|
| 1 | +import fs from 'fs/promises' |
| 2 | +import path from 'path' |
| 3 | +import frontmatter from '#src/frame/lib/read-frontmatter.js' |
| 4 | +import getApplicableVersions from '#src/versions/lib/get-applicable-versions.js' |
| 5 | +import removeFPTFromPath from '#src/versions/lib/remove-fpt-from-path.js' |
| 6 | +import { ROOT } from '#src/frame/lib/constants.js' |
| 7 | + |
| 8 | +/** |
| 9 | + * Represents a product in the documentation |
| 10 | + */ |
| 11 | +export interface Product { |
| 12 | + /** Unique identifier for the product */ |
| 13 | + id: string |
| 14 | + /** Display name of the product (short title or title) */ |
| 15 | + name: string |
| 16 | + /** URL path to the product's landing page */ |
| 17 | + href: string |
| 18 | + /** Directory path to the product's content */ |
| 19 | + dir: string |
| 20 | + /** Path to the product's table of contents file */ |
| 21 | + toc: string |
| 22 | + /** Whether the product is a work in progress */ |
| 23 | + wip: boolean |
| 24 | + /** Whether the product is hidden from the UI */ |
| 25 | + hidden: boolean |
| 26 | + /** Applicable versions for the product */ |
| 27 | + versions: string[] |
| 28 | + /** Rendered name (added later by middleware) */ |
| 29 | + nameRendered?: string |
| 30 | + /** Whether the product is external */ |
| 31 | + external?: boolean |
| 32 | +} |
| 33 | + |
| 34 | +/** |
| 35 | + * Map of product IDs to their corresponding product data |
| 36 | + */ |
| 37 | +export interface ProductMap { |
| 38 | + [productId: string]: Product |
| 39 | +} |
| 40 | + |
| 41 | +// Both internal and external products are specified in content/index.md |
| 42 | +const homepage = path.posix.join(ROOT, 'content/index.md') |
| 43 | +export const { data } = frontmatter(await fs.readFile(homepage, 'utf8')) |
| 44 | + |
| 45 | +export const productIds: string[] = data?.children || [] |
| 46 | + |
| 47 | +const externalProducts = (data?.externalProducts || {}) as ProductMap |
| 48 | +const internalProducts: ProductMap = {} |
| 49 | + |
| 50 | +for (const productId of productIds) { |
| 51 | + const relPath = productId |
| 52 | + const dir = path.join(ROOT, 'content', relPath) |
| 53 | + |
| 54 | + // Early Access may not exist in the current checkout |
| 55 | + try { |
| 56 | + await fs.readdir(dir) |
| 57 | + } catch { |
| 58 | + continue |
| 59 | + } |
| 60 | + |
| 61 | + const toc = path.posix.join(dir, 'index.md') |
| 62 | + const fileContent = await fs.readFile(toc, 'utf8') |
| 63 | + const { data: tocData } = frontmatter(fileContent) |
| 64 | + if (tocData) { |
| 65 | + const applicableVersions = getApplicableVersions(tocData.versions, toc) |
| 66 | + const href = removeFPTFromPath(path.posix.join('/', applicableVersions[0], productId)) |
| 67 | + |
| 68 | + // Note that a special middleware called `render-product-map.js` later |
| 69 | + // mutates this object by adding a `nameRendered` property to each product. |
| 70 | + // It's the outcome of rendering out possible Liquid from the |
| 71 | + // `shortTitle` or `title` after all the other contextualizers have run. |
| 72 | + internalProducts[productId] = { |
| 73 | + id: productId, |
| 74 | + name: tocData.shortTitle || tocData.title || productId, |
| 75 | + href, |
| 76 | + dir, |
| 77 | + toc, |
| 78 | + wip: tocData.wip || false, |
| 79 | + hidden: tocData.hidden || false, |
| 80 | + versions: applicableVersions, |
| 81 | + } |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +export const productMap: ProductMap = Object.assign({}, internalProducts, externalProducts) |
0 commit comments