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
11 changes: 7 additions & 4 deletions scripts/add-version-to-nav-data.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) => {
Expand All @@ -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]}`
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -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
}
/**
Expand All @@ -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]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -253,4 +253,67 @@ describe('transformRewriteInternalLinks', () => {
)
expect(result).toBe(expectedOutput)
})

it('should remove release stage in parentheses from version', async () => {
Copy link
Contributor

Choose a reason for hiding this comment

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

❤️

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)
})
})
Loading