Skip to content

Commit

Permalink
enforce internal links to use docIds
Browse files Browse the repository at this point in the history
  • Loading branch information
amozoss committed Sep 29, 2023
1 parent 196e049 commit 0c1736d
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions src/markdoc/convertDocId.js
Original file line number Diff line number Diff line change
@@ -1,14 +1,45 @@
import { readFileSync, existsSync } from 'fs'
import * as path from 'path'
import Markdoc from '@markdoc/markdoc'
import yaml from 'js-yaml'
import {
nodeBottomNav,
dcsBottomNav,
learnBottomNav,
supportBottomNav,
} from '@/markdoc/navigation.mjs'

function getFrontmatter(filePath) {
const md = readFileSync(filePath, 'utf8')
const ast = Markdoc.parse(md)
const frontmatter = ast.attributes.frontmatter
? yaml.load(ast.attributes.frontmatter)
: {}
return {
docId: frontmatter.docId,
}
}

export function convertDocId(href) {
if (href?.includes('docs.storj.io')) {
const url = new URL(href)
let dir = path.resolve('./app')
const filePath = path.join(dir, url.pathname, 'page.md')
if (
process.env.NODE_ENV !== 'production' ||
(url.host === new URL(process.env.SITE_URL).host && existsSync(filePath))
) {
let { docId } = getFrontmatter(filePath)
throw new Error(
`Internal links should use the docId. replace ${href} with docId:${docId}`
)
}
}

if (!href?.startsWith('docId')) {
return { title: null, href }
}

let parts = href.split(':')
let [docId, fragment] = parts[1].split('#')
let entry = nodeBottomNav.find((o) => o.docId === docId)
Expand Down

0 comments on commit 0c1736d

Please sign in to comment.