|
| 1 | +/** |
| 2 | + * @license |
| 3 | + * Copyright 2023 Google LLC |
| 4 | + * SPDX-License-Identifier: Apache-2.0 |
| 5 | + */ |
| 6 | + |
| 7 | +import {cp, readFile, writeFile} from 'fs/promises'; |
| 8 | +import {join, relative} from 'path'; |
| 9 | +import tinyGlob from 'tiny-glob'; |
| 10 | + |
| 11 | +/** |
| 12 | + * Recursively copies the images from |
| 13 | + * |
| 14 | + * /docs/components/images |
| 15 | + * to |
| 16 | + * /catalog/site/components/images |
| 17 | + */ |
| 18 | +async function copyImages() { |
| 19 | + await cp( |
| 20 | + join('..', 'docs', 'components', 'images'), |
| 21 | + join('site', 'components', 'images'), {recursive: true}, (err) => { |
| 22 | + if (err) throw err; |
| 23 | + }); |
| 24 | +} |
| 25 | + |
| 26 | +/** |
| 27 | + * Finds and returns all the filepaths of markdown files in |
| 28 | + * /docs/components/ |
| 29 | + * |
| 30 | + * @return A promise of all the markdwon filepaths in /docs/components/ |
| 31 | + */ |
| 32 | +async function getReadmeFiles() { |
| 33 | + const readmeFilesGlob = ['../docs/components/**/*.md']; |
| 34 | + const readmeFiles = readmeFilesGlob.map(async (entry) => tinyGlob(entry)); |
| 35 | + return (await Promise.all(readmeFiles)).flat(); |
| 36 | +} |
| 37 | + |
| 38 | +/** |
| 39 | + * Transforms to apply to the markdown files |
| 40 | + */ |
| 41 | +const transforms = [ |
| 42 | + // catalog-only code comments are removed |
| 43 | + { |
| 44 | + before: /<!-- catalog-only-start -->(\n)*?<!--\s*/gm, |
| 45 | + after: '', |
| 46 | + }, |
| 47 | + { |
| 48 | + before: /\s*-->(\n)*?<!-- catalog-only-end -->/gm, |
| 49 | + after: '', |
| 50 | + }, |
| 51 | + // removes everything in between github-only-start and github-only-end |
| 52 | + // comments |
| 53 | + { |
| 54 | + before: |
| 55 | + /\s*<!-- github-only-start -->(.|\n)*?<!-- github-only-end -->\s*/gm, |
| 56 | + after: '\n\n', |
| 57 | + }, |
| 58 | + // eleventy pages end with `/` so `components/checkbox.md` will turn into the |
| 59 | + // url `/components/checkbox`. Thus we need to transform the relative |
| 60 | + // `./images` links to `../images`. |
| 61 | + { |
| 62 | + before: /images\//gm, |
| 63 | + after: '../images/', |
| 64 | + }, |
| 65 | +]; |
| 66 | + |
| 67 | +/** |
| 68 | + * Applies the transforms to readme files at the given filepaths and outputs the |
| 69 | + * result to /catalog/site/components/<component-name>.md |
| 70 | + * |
| 71 | + * @param {Array<string>} filepaths The readme file paths to transform. |
| 72 | + */ |
| 73 | +async function transformReadmes(filepaths) { |
| 74 | + const readmePromises = filepaths.map(async (entry) => { |
| 75 | + let readme = await readFile(entry, 'utf8'); |
| 76 | + console.log(`Transforming ${entry}`); |
| 77 | + |
| 78 | + transforms.forEach((transform) => { |
| 79 | + readme = readme.replaceAll(transform.before, transform.after); |
| 80 | + }); |
| 81 | + |
| 82 | + // The `components/<component-name>.md` path. |
| 83 | + const localPath = relative(join('..', 'docs'), entry); |
| 84 | + // The output path at |
| 85 | + // /catalog/site/components/<?local path>/<component name>.md |
| 86 | + const outputPath = join('site', localPath); |
| 87 | + console.log(`Writing trasnformed file to: ${outputPath}`); |
| 88 | + return writeFile(outputPath, readme); |
| 89 | + }); |
| 90 | + |
| 91 | + await Promise.all(readmePromises); |
| 92 | +} |
| 93 | + |
| 94 | +const readmeFiles = await getReadmeFiles(); |
| 95 | + |
| 96 | +console.log('Copying images...'); |
| 97 | +await copyImages(); |
| 98 | +console.log('Images copied!'); |
| 99 | +console.log('Transforming readmes...'); |
| 100 | +await transformReadmes(readmeFiles); |
| 101 | +console.log('Transformations complete!'); |
0 commit comments