2021-03-03 10:13:50 -05:00
|
|
|
import fs from 'fs'
|
|
|
|
import path from 'path'
|
|
|
|
import {
|
|
|
|
getNodeFromPath,
|
|
|
|
getPathsFromNavData,
|
|
|
|
} from '@hashicorp/react-docs-page/server'
|
|
|
|
import renderPageMdx from '@hashicorp/react-docs-page/render-page-mdx'
|
2021-03-18 12:24:36 -04:00
|
|
|
import resolveNavData from './utils/resolve-nav-data'
|
2021-03-03 10:13:50 -05:00
|
|
|
|
|
|
|
async function generateStaticPaths(navDataFile, contentDir, options = {}) {
|
|
|
|
const navData = await resolveNavData(navDataFile, contentDir, options)
|
|
|
|
const paths = await getPathsFromNavData(navData)
|
|
|
|
return paths
|
|
|
|
}
|
|
|
|
|
|
|
|
async function generateStaticProps(
|
|
|
|
navDataFile,
|
|
|
|
localContentDir,
|
|
|
|
params,
|
2021-03-18 12:24:36 -04:00
|
|
|
product,
|
|
|
|
{ remotePluginsFile, additionalComponents, mainBranch = 'main' } = {}
|
2021-03-03 10:13:50 -05:00
|
|
|
) {
|
|
|
|
const navData = await resolveNavData(navDataFile, localContentDir, {
|
|
|
|
remotePluginsFile,
|
|
|
|
})
|
|
|
|
const pathToMatch = params.page ? params.page.join('/') : ''
|
|
|
|
const navNode = getNodeFromPath(pathToMatch, navData, localContentDir)
|
|
|
|
const { filePath, remoteFile, pluginTier } = navNode
|
|
|
|
// Fetch the MDX file content
|
2021-03-18 12:24:36 -04:00
|
|
|
const mdxString = remoteFile
|
|
|
|
? remoteFile.fileString
|
|
|
|
: fs.readFileSync(path.join(process.cwd(), filePath), 'utf8')
|
|
|
|
// Construct the githubFileUrl, used for "Edit this page" link
|
|
|
|
// Note: for custom ".docs-artifacts" directories, the "Edit this page"
|
|
|
|
// link will lead to the artifact file rather than the "docs" source file
|
|
|
|
const githubFileUrl = remoteFile
|
|
|
|
? remoteFile.sourceUrl
|
|
|
|
: `https://github.com/hashicorp/${product.slug}/blob/${mainBranch}/website/${filePath}`
|
2021-03-03 10:13:50 -05:00
|
|
|
// For plugin pages, prefix the MDX content with a
|
|
|
|
// label that reflects the plugin tier
|
|
|
|
// (current options are "Official" or "Community")
|
|
|
|
function mdxContentHook(mdxContent) {
|
|
|
|
if (pluginTier) {
|
|
|
|
const tierMdx = `<PluginTierLabel tier="${pluginTier}" />\n\n`
|
|
|
|
mdxContent = tierMdx + mdxContent
|
|
|
|
}
|
|
|
|
return mdxContent
|
|
|
|
}
|
|
|
|
const { mdxSource, frontMatter } = await renderPageMdx(mdxString, {
|
|
|
|
additionalComponents,
|
2021-03-18 12:24:36 -04:00
|
|
|
productName: product.name,
|
2021-03-03 10:13:50 -05:00
|
|
|
mdxContentHook,
|
|
|
|
})
|
|
|
|
// Build the currentPath from page parameters
|
|
|
|
const currentPath = !params.page ? '' : params.page.join('/')
|
2021-03-18 12:24:36 -04:00
|
|
|
|
2021-03-03 10:13:50 -05:00
|
|
|
return {
|
|
|
|
currentPath,
|
|
|
|
frontMatter,
|
|
|
|
mdxSource,
|
|
|
|
mdxString,
|
2021-03-18 12:24:36 -04:00
|
|
|
githubFileUrl,
|
2021-03-03 10:13:50 -05:00
|
|
|
navData,
|
|
|
|
navNode,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default { generateStaticPaths, generateStaticProps }
|
|
|
|
export { generateStaticPaths, generateStaticProps }
|