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
2 changes: 2 additions & 0 deletions productConfig.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,7 @@ export const PRODUCT_CONFIG = {
* See note at top of this document on `pages` directories for details.
*/
assetDir: 'public/images',
basePaths: ['sentinel'],
/**
* TODO: consider implications of Sentinel's `contentDir`.
*
Expand Down Expand Up @@ -444,6 +445,7 @@ export const PRODUCT_CONFIG = {
* See note at top of this document on `pages` directories for details.
*/
assetDir: 'public/img',
basePaths: ['api-docs', 'docs'],
contentDir: 'content',
dataDir: 'data',
productSlug: 'vault',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,14 @@ import { PRODUCT_CONFIG } from '#productConfig.mjs'
* @returns {Function} A transformer function that rewrites internal links in the document tree.
*/
export const rewriteInternalLinksPlugin = ({ entry, versionMetadata }) => {
/** This REGEX is used to parse a product version from a URL */
const VERSION_IN_PATH_REGEX = /^v\d+\.\d+\.(\d+|\w+)/i

/** This REGEX is used to parse a product version that does not include 'v' at the beginning */
const NO_V_VERSION_IN_PATH_REGEX = /^\d+\.\d+\.(\d+|\w+)/i

/** This REGEX is used to parse a Terraform Enterprise version from a URL */
const TFE_VERSION_IN_PATH_REGEXP = /^v[0-9]{6}-\d+/i
const relativePath = entry.filePath.split('content/')[1]
/**
* product and version variables, which are assigned based on the
Expand Down Expand Up @@ -65,8 +73,18 @@ export const rewriteInternalLinksPlugin = ({ entry, versionMetadata }) => {
return flatMap(tree, (node) => {
// 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/${cleanVersion}$2`)
const splitUrl = node.url.split('/')
const hasVersionInPath = splitUrl.find((el) => {
return (
TFE_VERSION_IN_PATH_REGEXP.test(el) ||
VERSION_IN_PATH_REGEX.test(el) ||
NO_V_VERSION_IN_PATH_REGEX.test(el)
)
})
// Replace the matched part of the URL with the versioned path if no version is present
if (!hasVersionInPath) {
node.url = node.url.replace(replacePattern, `/$1/${cleanVersion}$2`)
Copy link
Contributor

Choose a reason for hiding this comment

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

Here be dragons....

We might want to be careful here with this logic as this has caused issues on dev portal URL slugs that contain versions. You can see an example of this here where the links in the dropdown get truncated to https://developer.hashicorp.com/vault/docs/v1.19.x/upgrading/upgrade-to-.

I have a dev portal PR open here. I don't know how likely we are to run into that here, but it just seemed worth mentioning.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yes this is something I made sure to double check with the regexes I've used here. These regexes include ^ at the beginning which means that the string must start with the version. This means strings like upgrade-to-0.9.2 do not pass the regex. Here are a few example of this:
https://unified-docs-frontend-preview-i54wv6l6r-hashicorp.vercel.app/vault/docs/v1.10.x/commands - upgrade guide link at top of page
https://unified-docs-frontend-preview-i54wv6l6r-hashicorp.vercel.app/vault/docs/v1.10.x/deprecation - Filter Mount Replication Deprecation Notice link in the table
https://unified-docs-frontend-preview-i54wv6l6r-hashicorp.vercel.app/vault/docs/v1.10.x/faq/ssct - Upgrade to 1.10 link

Copy link
Contributor

Choose a reason for hiding this comment

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

Ah yeah gotcha. Somehow I didn't notice the caret at first!

}
}
// 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 @@ -316,4 +316,49 @@ describe('transformRewriteInternalLinks', () => {
)
expect(result).toBe(expectedOutput)
})

it('should not rewrite links if they already contain a version that starts with v', async () => {
const content = `[Link to versioned path](/terraform/language/v1.7.x/some-page)`
const entry = {
filePath: 'content/terraform/v1.8.x/docs/language/some-file.mdx',
}
const expectedOutput =
'[Link to versioned path](/terraform/language/v1.7.x/some-page)\n'
const result = await transformRewriteInternalLinks(
content,
entry,
versionMetadata,
)
expect(result).toBe(expectedOutput)
})

it('should not rewrite links if they already contain a version that does not start with v', async () => {
const content = `[Link to versioned path](/terraform/language/1.7.x/some-page)`
const entry = {
filePath: 'content/terraform/1.8.x/docs/language/some-file.mdx',
}
const expectedOutput =
'[Link to versioned path](/terraform/language/1.7.x/some-page)\n'
const result = await transformRewriteInternalLinks(
content,
entry,
versionMetadata,
)
expect(result).toBe(expectedOutput)
})

it('should not rewrite links if they already contain a special terraform version', async () => {
const content = `[Link to versioned path](/terraform/language/v202509-01/some-page)`
const entry = {
filePath: 'content/terraform/v202509-02/docs/language/some-file.mdx',
}
const expectedOutput =
'[Link to versioned path](/terraform/language/v202509-01/some-page)\n'
const result = await transformRewriteInternalLinks(
content,
entry,
versionMetadata,
)
expect(result).toBe(expectedOutput)
})
})
Binary file modified scripts/prebuild/prebuild-arm-linux-binary.gz
Binary file not shown.
Binary file modified scripts/prebuild/prebuild-arm-mac-binary.gz
Binary file not shown.
Binary file modified scripts/prebuild/prebuild-x64-linux-binary.gz
Binary file not shown.
Loading