From 5d830a9d7a6fcbf6de2424d8b8976ea8601490b2 Mon Sep 17 00:00:00 2001 From: Leah Bush Date: Wed, 3 Sep 2025 16:00:18 -0500 Subject: [PATCH 1/2] fix: prebuild script adds release stage to versions in nav-data files --- scripts/add-version-to-nav-data.mjs | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/scripts/add-version-to-nav-data.mjs b/scripts/add-version-to-nav-data.mjs index 1afafbaccd..c202968758 100644 --- a/scripts/add-version-to-nav-data.mjs +++ b/scripts/add-version-to-nav-data.mjs @@ -34,6 +34,9 @@ export async function addVersionToNavData(filePath, versionMetadata) { */ const [product, version] = relativePath.split('/') + // Remove any release stage in parentheses) + const cleanVersion = version.replace(/\s*\([^)]+\)/, '') + // We are looking at a versionless doc if (PRODUCT_CONFIG[product].versionedDocs === false) { return @@ -70,8 +73,8 @@ export async function addVersionToNavData(filePath, versionMetadata) { (key === 'href' || key === 'path') && typeof obj[key] === 'string' && !obj[key].startsWith('http') && - version !== latestVersion && - !obj[key].includes(version) + cleanVersion !== latestVersion && + !obj[key].includes(cleanVersion) ) { // href allows linking outside of content subpath let basePath = PRODUCT_CONFIG[product].basePaths?.find((basePath) => { @@ -84,9 +87,9 @@ export async function addVersionToNavData(filePath, versionMetadata) { // 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)}` + `${basePath}/${cleanVersion}${obj[key].substring(basePath.length)}` } else if (key === 'path') { - obj[key] = `${version}/${obj[key]}` + obj[key] = `${cleanVersion}/${obj[key]}` } } } From 4678f0ee2f519f2c419e90607579f2046df7ac3a Mon Sep 17 00:00:00 2001 From: Leah Bush Date: Thu, 4 Sep 2025 10:42:47 -0500 Subject: [PATCH 2/2] fix internal links transform --- .../add-version-to-internal-links.mjs | 7 ++- .../add-version-to-internal-links.test.mjs | 63 +++++++++++++++++++ 2 files changed, 68 insertions(+), 2 deletions(-) diff --git a/scripts/mdx-transforms/add-version-to-internal-links/add-version-to-internal-links.mjs b/scripts/mdx-transforms/add-version-to-internal-links/add-version-to-internal-links.mjs index 509087be70..9787cd8229 100644 --- a/scripts/mdx-transforms/add-version-to-internal-links/add-version-to-internal-links.mjs +++ b/scripts/mdx-transforms/add-version-to-internal-links/add-version-to-internal-links.mjs @@ -27,6 +27,9 @@ export const rewriteInternalLinksPlugin = ({ entry, versionMetadata }) => { */ const [product, version] = relativePath.split('/') + // Remove any release stage in parentheses) + const cleanVersion = version.replace(/\s*\([^)]+\)/, '') + if (PRODUCT_CONFIG[product].versionedDocs === false) { return } @@ -42,7 +45,7 @@ export const rewriteInternalLinksPlugin = ({ entry, versionMetadata }) => { * If the version in the filepath is the latest version or * no base paths exist for the product, then skip rewriting internal links */ - if (version === latestVersion || !Object.entries(basePaths).length) { + if (cleanVersion === latestVersion || !Object.entries(basePaths).length) { return } /** @@ -63,7 +66,7 @@ export const rewriteInternalLinksPlugin = ({ entry, versionMetadata }) => { // Check if the node is a link and matches the pattern for links to rewrite if (node.type === 'link' && isLinkToRewritePattern.test(node.url)) { // Replace the matched part of the URL with the versioned path - node.url = node.url.replace(replacePattern, `/$1/${version}$2`) + node.url = node.url.replace(replacePattern, `/$1/${cleanVersion}$2`) } // Return the node (those with and without a versioned path) return [node] diff --git a/scripts/mdx-transforms/add-version-to-internal-links/add-version-to-internal-links.test.mjs b/scripts/mdx-transforms/add-version-to-internal-links/add-version-to-internal-links.test.mjs index d040e1e221..a9c5a7250f 100644 --- a/scripts/mdx-transforms/add-version-to-internal-links/add-version-to-internal-links.test.mjs +++ b/scripts/mdx-transforms/add-version-to-internal-links/add-version-to-internal-links.test.mjs @@ -253,4 +253,67 @@ describe('transformRewriteInternalLinks', () => { ) expect(result).toBe(expectedOutput) }) + + it('should remove release stage in parentheses from version', async () => { + const content = `[Link to plugin/testing](/plugin/testing/some-page)` + const entry = { + filePath: + 'content/terraform-plugin-testing/v1.5.x (alpha)/docs/some-file.mdx', + } + const expectedOutput = + '[Link to plugin/testing](/plugin/testing/v1.5.x/some-page)\n' + const result = await transformRewriteInternalLinks( + content, + entry, + versionMetadata, + ) + expect(result).toBe(expectedOutput) + }) + + it('should not modify version if there is no parentheses', async () => { + const content = `[Link to plugin/testing](/plugin/testing/some-page)` + const entry = { + filePath: 'content/terraform-plugin-testing/v1.5.x/docs/some-file.mdx', + } + const expectedOutput = + '[Link to plugin/testing](/plugin/testing/v1.5.x/some-page)\n' + const result = await transformRewriteInternalLinks( + content, + entry, + versionMetadata, + ) + expect(result).toBe(expectedOutput) + }) + + it('should remove multiple spaces before parentheses', async () => { + const content = `[Link to plugin/testing](/plugin/testing/some-page)` + const entry = { + filePath: + 'content/terraform-plugin-testing/v1.5.x (alpha)/docs/some-file.mdx', + } + const expectedOutput = + '[Link to plugin/testing](/plugin/testing/v1.5.x/some-page)\n' + const result = await transformRewriteInternalLinks( + content, + entry, + versionMetadata, + ) + expect(result).toBe(expectedOutput) + }) + + it('should handle version with parentheses but no space', async () => { + const content = `[Link to plugin/testing](/plugin/testing/some-page)` + const entry = { + filePath: + 'content/terraform-plugin-testing/v1.5.x(alpha)/docs/some-file.mdx', + } + const expectedOutput = + '[Link to plugin/testing](/plugin/testing/v1.5.x/some-page)\n' + const result = await transformRewriteInternalLinks( + content, + entry, + versionMetadata, + ) + expect(result).toBe(expectedOutput) + }) })