8000 fix: update internal links transform to run for all products (#994) · hashicorp/web-unified-docs@92b7046 · GitHub
[go: up one dir, main page]

Skip to content

Commit 92b7046

Browse files
fix: update internal links transform to run for all products (#994)
* fix: update internal links transform to run for all products * add tests for versions
1 parent ad47a82 commit 92b7046

File tree

6 files changed

+67
-2
lines changed

6 files changed

+67
-2
lines changed

productConfig.mjs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,7 @@ export const PRODUCT_CONFIG = {
202202
* See note at top of this document on `pages` directories for details.
203203
*/
204204
assetDir: 'public/images',
205+
basePaths: ['sentinel'],
205206
/**
206207
* TODO: consider implications of Sentinel's `contentDir`.
207208
*
@@ -444,6 +445,7 @@ export const PRODUCT_CONFIG = {
444445
* See note at top of this document on `pages` directories for details.
445446
*/
446447
assetDir: 'public/img',
448+
basePaths: ['api-docs', 'docs'],
447449
contentDir: 'content',
448450
dataDir: 'data',
449451
productSlug: 'vault',

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

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,14 @@ import { PRODUCT_CONFIG } from '#productConfig.mjs'
2020
* @returns {Function} A transformer function that rewrites internal links in the document tree.
2121
*/
2222
export const rewriteInternalLinksPlugin = ({ entry, versionMetadata }) => {
23+
/** This REGEX is used to parse a product version from a URL */
24+
const VERSION_IN_PATH_REGEX = /^v\d+\.\d+\.(\d+|\w+)/i
25+
26+
/** This REGEX is used to parse a product version that does not include 'v' at the beginning */
27+
const NO_V_VERSION_IN_PATH_REGEX = /^\d+\.\d+\.(\d+|\w+)/i
28+
29+
/** This REGEX is used to parse a Terraform Enterprise version from a URL */
30+
const TFE_VERSION_IN_PATH_REGEXP = /^v[0-9]{6}-\d+/i
2331
const relativePath = entry.filePath.split('content/')[1]
2432
/**
2533
* product and version variables, which are assigned based on the
@@ -65,8 +73,18 @@ export const rewriteInternalLinksPlugin = ({ entry, versionMetadata }) => {
6573
return flatMap(tree, (node) => {
6674
// Check if the node is a link and matches the pattern for links to rewrite
6775
if (node.type === 'link' && isLinkToRewritePattern.test(node.url)) {
68-
// Replace the matched part of the URL with the versioned path
69-
node.url = node.url.replace(replacePattern, `/$1/${cleanVersion}$2`)
76+
const splitUrl = node.url.split('/')
77+
const hasVersionInPath = splitUrl.find((el) => {
78+
return (
79+
TFE_VERSION_IN_PATH_REGEXP.test(el) ||
80+
VERSION_IN_PATH_REGEX.test(el) ||
81+
NO_V_VERSION_IN_PATH_REGEX.test(el)
82+
)
83+
})
84+
// Replace the matched part of the URL with the versioned path if no version is present
85+
if (!hasVersionInPath) {
86+
node.url = node.url.replace(replacePattern, `/$1/${cleanVersion}$2`)
87+
}
7088
}
7189
// Return the node (those with and without a versioned path)
7290
return [node]

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

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,4 +316,49 @@ describe('transformRewriteInternalLinks', () => {
316316
)
317317
expect(result).toBe(expectedOutput)
318318
})
319+
320+
it('should not rewrite links if they already contain a version that starts with v', async () => {
321+
const content = `[Link to versioned path](/terraform/language/v1.7.x/some-page)`
322+
const entry = {
323+
filePath: 'content/terraform/v1.8.x/docs/language/some-file.mdx',
324+
}
325+
const expectedOutput =
326+
'[Link to versioned path](/terraform/language/v1.7.x/some-page)\n'
327+
const result = await transformRewriteInternalLinks(
328+
content,
329+
entry,
330+
versionMetadata,
331+
)
332+
expect(result).toBe(expectedOutput)
333+
})
334+
335+
it('should not rewrite links if they already contain a version that does not start with v', async () => {
336+
const content = `[Link to versioned path](/terraform/language/1.7.x/some-page)`
337+
const entry = {
338+
filePath: 'content/terraform/1.8.x/docs/language/some-file.mdx',
339+
}
340+
const expectedOutput =
341+
'[Link to versioned path](/terraform/language/1.7.x/some-page)\n'
342+
const result = await transformRewriteInternalLinks(
343+
content,
344+
entry,
345+
versionMetadata,
346+
)
347+
expect(result).toBe(expectedOutput)
348+
})
349+
350+
it('should not rewrite links if they already contain a special terraform version', async () => {
351+
const content = `[Link to versioned path](/terraform/language/v202509-01/some-page)`
352+
const entry = {
353+
filePath: 'content/terraform/v202509-02/docs/language/some-file.mdx',
354+
}
355+
const expectedOutput =
356+
'[Link to versioned path](/terraform/language/v202509-01/some-page)\n'
357+
const result = await transformRewriteInternalLinks(
358+
content,
359+
entry,
360+
versionMetadata,
361+
)
362+
expect(result).toBe(expectedOutput)
363+
})
319364
})
799 Bytes
Binary file not shown.
1.74 KB
Binary file not shown.
1.21 KB
Binary file not shown.

0 commit comments

Comments
 (0)
0