Skip to content

Commit 89a6cc2

Browse files
fix: prebuild script adds release stage to versions in nav-data files and internal links (#901)
* fix: prebuild script adds release stage to versions in nav-data files * fix internal links transform
1 parent f2aed6b commit 89a6cc2

File tree

3 files changed

+75
-6
lines changed

3 files changed

+75
-6
lines changed

scripts/add-version-to-nav-data.mjs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ export async function addVersionToNavData(filePath, versionMetadata) {
3434
*/
3535
const [product, version] = relativePath.split('/')
3636

37+
// Remove any release stage in parentheses)
38+
const cleanVersion = version.replace(/\s*\([^)]+\)/, '')
39+
3740
// We are looking at a versionless doc
3841
if (PRODUCT_CONFIG[product].versionedDocs === false) {
3942
return
@@ -70,8 +73,8 @@ export async function addVersionToNavData(filePath, versionMetadata) {
7073
(key === 'href' || key === 'path') &&
7174
typeof obj[key] === 'string' &&
7275
!obj[key].startsWith('http') &&
73-
version !== latestVersion &&
74-
!obj[key].includes(version)
76+
cleanVersion !== latestVersion &&
77+
!obj[key].includes(cleanVersion)
7578
) {
7679
// href allows linking outside of content subpath
7780
let basePath = PRODUCT_CONFIG[product].basePaths?.find((basePath) => {
@@ -84,9 +87,9 @@ export async function addVersionToNavData(filePath, versionMetadata) {
8487
// if the href starts with a basepath, e.g. "/cli", add version after the basepath
8588
if (basePath && basePath.length) {
8689
obj[key] =
87-
`${basePath}/${version}${obj[key].substring(basePath.length)}`
90+
`${basePath}/${cleanVersion}${obj[key].substring(basePath.length)}`
8891
} else if (key === 'path') {
89-
obj[key] = `${version}/${obj[key]}`
92+
obj[key] = `${cleanVersion}/${obj[key]}`
9093
}
9194
}
9295
}

scripts/mdx-transforms/add-version-to-internal-links/add-version-to-internal-links.mjs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@ export const rewriteInternalLinksPlugin = ({ entry, versionMetadata }) => {
2727
*/
2828
const [product, version] = relativePath.split('/')
2929

30+
// Remove any release stage in parentheses)
31+
const cleanVersion = version.replace(/\s*\([^)]+\)/, '')
32+
3033
if (PRODUCT_CONFIG[product].versionedDocs === false) {
3134
return
3235
}
@@ -42,7 +45,7 @@ export const rewriteInternalLinksPlugin = ({ entry, versionMetadata }) => {
4245
* If the version in the filepath is the latest version or
4346
* no base paths exist for the product, then skip rewriting internal links
4447
*/
45-
if (version === latestVersion || !Object.entries(basePaths).length) {
48+
if (cleanVersion === latestVersion || !Object.entries(basePaths).length) {
4649
return
4750
}
4851
/**
@@ -63,7 +66,7 @@ export const rewriteInternalLinksPlugin = ({ entry, versionMetadata }) => {
6366
// Check if the node is a link and matches the pattern for links to rewrite
6467
if (node.type === 'link' && isLinkToRewritePattern.test(node.url)) {
6568
// Replace the matched part of the URL with the versioned path
66-
node.url = node.url.replace(replacePattern, `/$1/${version}$2`)
69+
node.url = node.url.replace(replacePattern, `/$1/${cleanVersion}$2`)
6770
}
6871
// Return the node (those with and without a versioned path)
6972
return [node]

scripts/mdx-transforms/add-version-to-internal-links/add-version-to-internal-links.test.mjs

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -253,4 +253,67 @@ describe('transformRewriteInternalLinks', () => {
253253
)
254254
expect(result).toBe(expectedOutput)
255255
})
256+
257+
it('should remove release stage in parentheses from version', async () => {
258+
const content = `[Link to plugin/testing](/plugin/testing/some-page)`
259+
const entry = {
260+
filePath:
261+
'content/terraform-plugin-testing/v1.5.x (alpha)/docs/some-file.mdx',
262+
}
263+
const expectedOutput =
264+
'[Link to plugin/testing](/plugin/testing/v1.5.x/some-page)\n'
265+
const result = await transformRewriteInternalLinks(
266+
content,
267+
entry,
268+
versionMetadata,
269+
)
270+
expect(result).toBe(expectedOutput)
271+
})
272+
273+
it('should not modify version if there is no parentheses', async () => {
274+
const content = `[Link to plugin/testing](/plugin/testing/some-page)`
275+
const entry = {
276+
filePath: 'content/terraform-plugin-testing/v1.5.x/docs/some-file.mdx',
277+
}
278+
const expectedOutput =
279+
'[Link to plugin/testing](/plugin/testing/v1.5.x/some-page)\n'
280+
const result = await transformRewriteInternalLinks(
281+
content,
282+
entry,
283+
versionMetadata,
284+
)
285+
expect(result).toBe(expectedOutput)
286+
})
287+
288+
it('should remove multiple spaces before parentheses', async () => {
289+
const content = `[Link to plugin/testing](/plugin/testing/some-page)`
290+
const entry = {
291+
filePath:
292+
'content/terraform-plugin-testing/v1.5.x (alpha)/docs/some-file.mdx',
293+
}
294+
const expectedOutput =
295+
'[Link to plugin/testing](/plugin/testing/v1.5.x/some-page)\n'
296+
const result = await transformRewriteInternalLinks(
297+
content,
298+
entry,
299+
versionMetadata,
300+
)
301+
expect(result).toBe(expectedOutput)
302+
})
303+
304+
it('should handle version with parentheses but no space', async () => {
305+
const content = `[Link to plugin/testing](/plugin/testing/some-page)`
306+
const entry = {
307+
filePath:
308+
'content/terraform-plugin-testing/v1.5.x(alpha)/docs/some-file.mdx',
309+
}
310+
const expectedOutput =
311+
'[Link to plugin/testing](/plugin/testing/v1.5.x/some-page)\n'
312+
const result = await transformRewriteInternalLinks(
313+
content,
314+
entry,
315+
versionMetadata,
316+
)
317+
expect(result).toBe(expectedOutput)
318+
})
256319
})

0 commit comments

Comments
 (0)