MCPcopy Create free account
hub / github.com/Automattic/mongoose / renderFile

Function renderFile

scripts/website.js:469–599  ·  view source on GitHub ↗

* Render a given file with the given options * @param {String} filename The documentation file path to render * @param {import("../docs/source/index").DocsOptions} options The options to use to render the file (api may be overwritten at reload) * @param {Boolean} isReload Indicate this is a reloa

(filename, options, isReload = false)

Source from the content-addressed store, hash-verified

467 * @returns
468 */
469async function renderFile(filename, options, isReload = false) {
470 /** Path for the output file */
471 let newfile = undefined;
472 options = options || {};
473 options.package = pkg;
474 let markdownSource = null;
475
476 const _editLink = 'https://github.com/Automattic/mongoose/edit/master' +
477 filename.replace(cwd, '');
478 options.editLink = options.editLink || _editLink;
479
480 /** Set which path to read, also pug uses this to resolve relative includes from */
481 let inputFile = filename;
482
483 if (options.api) {
484 // only re-parse the api file when in a reload, because it is done once at file load
485 if (isReload) {
486 apiReq.parseFile(options.file);
487 // overwrite original options because of reload
488 options = { ...options, ...apiReq.docs.get(options.file) };
489 }
490 inputFile = path.resolve(cwd, 'docs/api_split.pug');
491 }
492
493 if (options.apiMarkdown) {
494 newfile = path.resolve(cwd, filename);
495 const str = options.markdownSource;
496 if (typeof str !== 'string') {
497 throw new Error(`No markdown source found for API page "${filename}"`);
498 }
499
500 if (versionObj.versionedDeploy) {
501 const versionedMarkdownPath = path.resolve(cwd, path.join('.', versionObj.versionedPath), path.relative(cwd, filename));
502 await fs.promises.mkdir(path.dirname(versionedMarkdownPath), { recursive: true });
503 await fs.promises.writeFile(versionedMarkdownPath, str);
504 console.log('%s : rendered %s', (new Date()).toISOString(), versionedMarkdownPath);
505 }
506
507 await fs.promises.mkdir(path.dirname(newfile), { recursive: true });
508 await fs.promises.writeFile(newfile, str).catch((err) => {
509 console.error('could not write', err.stack);
510 }).then(() => {
511 console.log('%s : rendered %s', (new Date()).toISOString(), newfile);
512 });
513
514 return;
515 }
516
517 let contents = fs.readFileSync(path.resolve(cwd, inputFile)).toString();
518 const originalContents = contents;
519
520 if (options.acquit) {
521 const tests = getTests();
522 contents = contents.replace(/```javascript acquit:([^\n]+)\n[\s\S]*?```/g, (match, pattern) => {
523 const code = findTest(pattern, tests);
524 if (!code) {
525 throw new Error(`No test found for acquit pattern "${pattern}" in ${filename}`);
526 }

Callers 2

startWatchFunction · 0.85
renderAllFilesFunction · 0.85

Calls 5

getTestsFunction · 0.85
wrapMarkdownFunction · 0.85
mapURLsFunction · 0.85
getMethod · 0.80
toStringMethod · 0.45

Tested by

no test coverage detected