Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
export async function renderPageMdx(root, pagePath, components) {
// get the page being requested - figure out if its index page or leaf
// prefer leaf if both are present
const leafPath = path.join(root, `${pagePath}.mdx`)
const indexPath = path.join(root, `${pagePath}/index.mdx`)
let page, filePath
if (existsSync(leafPath)) {
page = fs.readFileSync(leafPath, 'utf8')
filePath = leafPath
} else if (existsSync(indexPath)) {
page = fs.readFileSync(indexPath, 'utf8')
filePath = indexPath
} else {
// NOTE: if we decide to let docs pages render dynamically, we should replace this
// error with a straight 404, at least in production.
throw new Error(
`We went looking for "${leafPath}" and "${indexPath}" but neither one was found.`
)
}
const { data: frontMatter, content } = matter(page)
const mdxSource = await renderToString(content, {
mdxOptions: markdownDefaults({
export async function renderPageMdx(root, pagePath, components) {
// get the page being requested - figure out if its index page or leaf
// prefer leaf if both are present
const leafPath = path.join(root, `${pagePath}.mdx`)
const indexPath = path.join(root, `${pagePath}/index.mdx`)
let page, filePath
if (existsSync(leafPath)) {
page = fs.readFileSync(leafPath, 'utf8')
filePath = leafPath
} else if (existsSync(indexPath)) {
page = fs.readFileSync(indexPath, 'utf8')
filePath = indexPath
} else {
// NOTE: if we decide to let docs pages render dynamically, we should replace this
// error with a straight 404, at least in production.
throw new Error(
`We went looking for "${leafPath}" and "${indexPath}" but neither one was found.`
)
}
const { data: frontMatter, content } = matter(page)
const mdxSource = await renderToString(content, {
mdxOptions: markdownDefaults({
resolveIncludes: path.join(process.cwd(), 'content/partials'),
}),
components,
function partialFinder(markdownBody) {
const regexToFindPartials = /<\w+ ?\/>/g;
const partials = markdownBody.match(regexToFindPartials);
if (partials) {
for (const partial of partials) {
const filename = decamelize(partial, "-").replace("<", "")
.replace("/>", "")
.trim();
const fileExistsTest = exists(`./app/components/${filename}.js`);
if (!fileExistsTest)
markdownBody = markdownBody.replace(partial, "");
else {
const partialFunction = require(path.join(__dirname, "..", `./components/${filename}.js`));
if (filename === "glossary-toc") markdownBody = markdownBody.replace(partial, partialFunction.default);
else markdownBody = markdownBody.replace(partial, `${partialFunction.default()}<div class="page__markup">`);
}
}
}
return ("<div class="\"page__markup\"">" + markdownBody + "</div>").replace(/<div class="page__markup">\s*<\/div>/, "");
}
</div></div>