-
Notifications
You must be signed in to change notification settings - Fork 79
fix: update internal links transform to run for all products #994
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
bbb1166
e530a4f
2a87205
749df4d
099ae10
a675f3b
46e3e57
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 | ||
|
@@ -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`) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 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. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 There was a problem hiding this comment. Choose a reason for hiding this commentThe 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] | ||
|
Uh oh!
There was an error while loading. Please reload this page.