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

Function renderFile

scripts/website.js:464–593  ·  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

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

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