Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 1 addition & 7 deletions __fixtures__/versionMetadata.json
Original file line number Diff line number Diff line change
Expand Up @@ -382,13 +382,7 @@
"isLatest": false
}
],
"terraform-docs-common": [
{
"version": "v0.0.x",
"releaseStage": "stable",
"isLatest": true
}
],
"terraform-docs-common": [],
"terraform-plugin-framework": [
{
"version": "v1.5.x",
Expand Down
51 changes: 24 additions & 27 deletions scripts/add-version-to-nav-data.mjs
Original file line number Diff line number Diff line change
@@ -1,23 +1,6 @@
import fs from 'node:fs/promises'

const terraformBasePaths = [
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Using repo-config rather than hard-coded values

'/cdktf',
'/cli',
'/cloud-docs/agents',
'/cloud-docs',
'/docs',
'/enterprise',
'/internals',
'/intro',
'/language',
'/plugin',
'/plugin/framework',
'/plugin/log',
'/plugin/mux',
'/plugin/sdkv2',
'/plugin/testing',
'/registry',
]
import { PRODUCT_CONFIG } from '../app/utils/productConfig.mjs'
import semver from 'semver'

/**
* Adds version information to navigation data in a JSON file.
Expand All @@ -39,12 +22,27 @@ export async function addVersionToNavData(filePath, versionMetadata) {
}

try {
const relativePath = filePath.split('content/')[1]
/**
* product and version variables, which are assigned based on the
* specific indices those strings are expected to be in the filepath
*/
const [product, version] = relativePath.split('/')

// We are looking at a versionless doc
if (!semver.valid(semver.coerce(version))) {
return
}

if (!versionMetadata[product]) {
throw new Error(`No version metadata found for product: ${product}`)
}

const data = await fs.readFile(filePath, 'utf-8')
if (data === '') {
console.error(`File is empty: ${filePath}`)
return
}

const jsonData = JSON.parse(data)
const versionMatch = filePath.match(/\/content\/[^/]+\/([^/]+)\/data\//)

Expand All @@ -53,10 +51,6 @@ export async function addVersionToNavData(filePath, versionMetadata) {
return
}

const version = versionMatch[1]
let product = filePath.split('/content/')[1]
product = product.split('/')[0]

// Use app/api/versionMetadata.json to get the latest version
const latestVersion = versionMetadata[product].find((version) => {
return version.isLatest
Expand All @@ -75,15 +69,18 @@ export async function addVersionToNavData(filePath, versionMetadata) {
!obj[key].includes(version)
) {
// href allows linking outside of content subpath
const basePath = terraformBasePaths.find((basePath) => {
return obj[key].startsWith(basePath)
let basePath = PRODUCT_CONFIG[product].basePaths?.find((basePath) => {
return obj[key].startsWith(`/${basePath}`)
})

basePath =
typeof basePath === 'undefined' ? undefined : `/${basePath}`

// if the href starts with a basepath, e.g. "/cli", add version after the basepath
if (basePath && basePath.length) {
obj[key] =
`${basePath}/${version}${obj[key].substring(basePath.length)}`
} else {
} else if (key === 'path') {
obj[key] = `${version}/${obj[key]}`
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export const rewriteInternalLinksPlugin = ({ entry, versionMetadata }) => {
const [product, version] = relativePath.split('/')

// We are looking at a versionless doc
if (!semver.valid(semver.coerce(version))) {
if (semver.valid(semver.coerce(version)) === null) {
return
}

Expand All @@ -50,7 +50,6 @@ export const rewriteInternalLinksPlugin = ({ entry, versionMetadata }) => {
const isLinkToRewritePattern = new RegExp(
`^(?!https?:\\/\\/|http:\\/\\/)(((\\.+\\/)*)|\\/|\\/${product}(?:\\/${basePaths.join('|')})?\\/)`,
)

// Creates a regex pattern to match and replace internal links based on the provided base paths.
const replacePattern = new RegExp(`/(${basePaths.join('|')})(/)?`)

Expand Down Expand Up @@ -83,6 +82,5 @@ export const transformRewriteInternalLinks = async (
versionMetadata,
})
.process(content)

return document.contents
}
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ describe('transformRewriteInternalLinks', () => {
it('should not rewrite internal links for a product with no basePaths array', async () => {
const content = `[Link to cloud-docs](/cloud-docs/some-page)`
const entry = {
filePath: 'content/terraform-docs-common/v0.0.x/docs/some-file.mdx',
filePath: 'content/terraform-docs-common/docs/some-file.mdx',
}
const expectedOutput = content + '\n'
const result = await transformRewriteInternalLinks(
Expand Down
15 changes: 9 additions & 6 deletions scripts/mdx-transforms/build-mdx-transforms-file.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,15 @@ import remarkMdx from 'remark-mdx'
import grayMatter from 'gray-matter'

import { paragraphCustomAlertsPlugin } from './paragraph-custom-alert/paragraph-custom-alert.mjs'
// import { rewriteInternalLinksPlugin } from './add-version-to-internal-links/add-version-to-internal-links.mjs'
import { rewriteInternalLinksPlugin } from './add-version-to-internal-links/add-version-to-internal-links.mjs'
import { remarkIncludePartialsPlugin } from './include-partials/remark-include-partials.mjs'
import {
rewriteInternalRedirectsPlugin,
loadRedirects,
} from './rewrite-internal-redirects/rewrite-internal-redirects.mjs'

// const CWD = process.cwd()
// const VERSION_METADATA_FILE = path.join(CWD, 'app/api/versionMetadata.json')
const CWD = process.cwd()
const VERSION_METADATA_FILE = path.join(CWD, 'app/api/versionMetadata.json')

/**
* Given a file path,
Expand Down Expand Up @@ -49,7 +49,9 @@ export async function buildFileMdxTransforms(filePath) {
}

console.log(`🪄 Running MDX transform on ${filePath}...`)
const result = await applyFileMdxTransforms(entry)
const result = await applyFileMdxTransforms(entry, () => {
return fs.readFile(VERSION_METADATA_FILE)
})
if (result.error) {
console.error(`❗ Encountered an error: ${result.error}`)
} else {
Expand All @@ -70,7 +72,7 @@ export async function buildFileMdxTransforms(filePath) {
* @param {string} entry.outPath
* @return {object} { error: string | null }
*/
export async function applyFileMdxTransforms(entry) {
export async function applyFileMdxTransforms(entry, versionMetadata = {}) {
try {
const { filePath, partialsDir, outPath, version, redirectsDir } = entry
const redirects = await loadRedirects(version, redirectsDir)
Expand All @@ -86,14 +88,15 @@ export async function applyFileMdxTransforms(entry) {
.use(rewriteInternalRedirectsPlugin, {
redirects,
})
// .use(rewriteInternalLinksPlugin, { entry, VERSION_METADATA_FILE })
.use(rewriteInternalLinksPlugin, { entry, versionMetadata })
.process(content)

const transformedContent = String(remarkResults)

const transformedFileString = grayMatter.stringify(transformedContent, data)
// Ensure the parent directory for the output file path exists
const outDir = path.dirname(outPath)

if (!fs.existsSync(outDir)) {
fs.mkdirSync(outDir, { recursive: true })
}
Expand Down
3 changes: 2 additions & 1 deletion scripts/mdx-transforms/build-mdx-transforms-file.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
buildFileMdxTransforms,
applyFileMdxTransforms,
} from './build-mdx-transforms-file.mjs'
import versionMetadata from 'scripts/mdx-transforms/include-partials/__fixtures__/basic/versionMetadata.json'
import fs from 'fs'

vi.mock('fs')
Expand Down Expand Up @@ -41,7 +42,7 @@ test('test applyFileMdxTransforms', async () => {
partialsDir: '../../partials',
outPath: transformedOutPath,
}
const result = await applyFileMdxTransforms(entry)
const result = await applyFileMdxTransforms(entry, versionMetadata)
expect(result).toStrictEqual({ error: null })

const transformedContent = fs.readFileSync(entry.outPath, 'utf8')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import * as pathToRegexp from 'path-to-regexp'
* Loads redirects from the file-system and "caches" them in memory.
*/
const cachedRedirects = {}
export const loadRedirects = async (version, redirectsDir) => {
export const loadRedirects = async (version = 'default', redirectsDir) => {
// Return the cached redirects if they are already present
if (cachedRedirects[version]?.length > 0) {
return cachedRedirects[version]
Expand Down Expand Up @@ -125,7 +125,7 @@ export const rewriteInternalRedirectsPlugin = ({ redirects }) => {
*/
export const transformRewriteInternalRedirects = async (
mdxString,
version,
version = 'default',
redirectsDir,
) => {
const redirects = await loadRedirects(version, redirectsDir)
Expand Down
7 changes: 3 additions & 4 deletions scripts/prebuild.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -55,10 +55,9 @@ async function copyNavDataFiles(sourceDir, destDir, versionMetadata = {}) {
fs.mkdirSync(parentDir, { recursive: true })
}
fs.copyFileSync(filePath, destPath)
if (!Object.keys(versionMetadata).length) {
// add version to nav data paths/hrefs
await addVersionToNavData(destPath, versionMetadata)
}

// add version to nav data paths/hrefs
await addVersionToNavData(destPath, versionMetadata)
},
16,
)
Expand Down
Loading