Skip to content

Commit

Permalink
separate out the partials validation to give better warning messages
Browse files Browse the repository at this point in the history
  • Loading branch information
NWylynko committed Mar 10, 2025
1 parent fbb46ec commit 18ecb6b
Showing 1 changed file with 45 additions and 7 deletions.
52 changes: 45 additions & 7 deletions scripts/build-docs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ const readPartialsMarkdown = (config: BuildConfig) => async (paths: string[]) =>
partialNode = tree
})
.process({
path: markdownPath,
path: `docs/_partials/${markdownPath}`,
value: content,
})

Expand Down Expand Up @@ -533,14 +533,14 @@ const parseInMarkdownFile =
})
// Validate the <Include />
.use(() => (tree, vfile) => {
return mdastMap(tree, (node) => {
return mdastVisit(tree, (node) => {
const partialSrc = extractComponentPropValueFromNode(node, vfile, 'Include', 'src')

if (partialSrc === undefined) return node
if (partialSrc === undefined) return

if (partialSrc.startsWith('_partials/') === false) {
vfile.message(`<Include /> prop "src" must start with "_partials/"`, node.position)
return node
return
}

const partial = partials.find(
Expand All @@ -549,10 +549,10 @@ const parseInMarkdownFile =

if (partial === undefined) {
vfile.message(`Partial /docs/${removeMdxSuffix(partialSrc)}.mdx not found`, node.position)
return node
return
}

return Object.assign(node, partial.node)
return
})
})
// extract out the headings to check hashes in links
Expand Down Expand Up @@ -737,6 +737,44 @@ export const build = async (config: BuildConfig) => {

const flatSDKScopedManifest = flattenTree(sdkScopedManifest)

const partialsVFiles = await Promise.all(
partials.map(async (partial) => {
return await markdownProcessor()
// validate links in partials to docs are valid
.use(() => (tree, vfile) => {
return mdastVisit(tree, (node) => {
if (node.type !== 'link') return
if (!('url' in node)) return
if (typeof node.url !== 'string') return
if (!node.url.startsWith('/docs/')) return
if (!('children' in node)) return

const [url, hash] = removeMdxSuffix(node.url).split('#')

const ignore = config.ignorePaths.some((ignoreItem) => url.startsWith(ignoreItem))
if (ignore === true) return

const doc = docsMap.get(url)

if (doc === undefined) {
vfile.message(`Doc ${url} not found`, node.position)
return
}

if (hash !== undefined) {
const hasHash = doc.headingsHashs.includes(hash)

if (hasHash === false) {
vfile.message(`Hash "${hash}" not found in ${url}`, node.position)
}
}
})
})
.process(partial.vfile)
})
)
console.info(`✔️ Validated all partials`)

const coreVFiles = await Promise.all(
docsArray.map(async (doc) => {
const vfile = await markdownProcessor()
Expand Down Expand Up @@ -833,7 +871,7 @@ export const build = async (config: BuildConfig) => {

console.info(`✔️ Validated all docs`)

return reporter(coreVFiles, { quiet: true })
return reporter([...coreVFiles, ...partialsVFiles], { quiet: true })
}

type BuildConfigOptions = {
Expand Down

0 comments on commit 18ecb6b

Please sign in to comment.