Skip to content

Commit

Permalink
build(catalog): add catalog-include to catalog build
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 542968626
  • Loading branch information
Elliott Marquez authored and copybara-github committed Jun 23, 2023
1 parent 0ba56b6 commit 0bced28
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 336 deletions.
44 changes: 43 additions & 1 deletion catalog/scripts/copy-readmes.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
*/

import {cp, readFile, writeFile} from 'fs/promises';
import {join, relative} from 'path';
import {dirname, join, relative} from 'path';
import tinyGlob from 'tiny-glob';

/**
Expand Down Expand Up @@ -64,6 +64,46 @@ const transforms = [
},
];

/**
* Executes the `<!-- catalog-include "..." -->` file transform by fetching the
* file in quotes and simply injecting its contents into the Markdown.
*
* @param {string} filepath The filepath of the markdown file to transform. Used
* for determining relative URLs.
* @param {string} fileContents The contents of the markdown filepath to
* transform.
* @return The stringified transformed contents of the markdown file.
*/
async function fileIncludeTransform(filepath, fileContents) {
const catalogIncludeRegex = /<!--\s?catalog-include "(.+)"\s?-->/g;
const matches = [];
let match = catalogIncludeRegex.exec(fileContents);

// Collect all the regex matches
while (match) {
matches.push(match);
match = catalogIncludeRegex.exec(fileContents);
}

const fileDir = dirname(filepath);

// Iterate through the regex matches backward and splice in the file contents.
// Iterating backwards so that injecting won't affect match string indices.
for (let i = matches.length - 1; i >= 0; i--) {
const match = matches[i];
const matchedString = match[0];
const includePath = match[1];

console.log(`Injecting ${includePath} file contents into ${filepath}...`);
const includeContents = await readFile(join(fileDir, includePath), 'utf8');

fileContents = fileContents.slice(0, match.index) + includeContents +
fileContents.slice(match.index + matchedString.length);
}

return fileContents;
}

/**
* Applies the transforms to readme files at the given filepaths and outputs the
* result to /catalog/site/components/<component-name>.md
Expand All @@ -79,6 +119,8 @@ async function transformReadmes(filepaths) {
readme = readme.replaceAll(transform.before, transform.after);
});

readme = await fileIncludeTransform(entry, readme);

// The `components/<component-name>.md` path.
const localPath = relative(join('..', 'docs'), entry);
// The output path at
Expand Down
Loading

0 comments on commit 0bced28

Please sign in to comment.