(oldPath, languageCode = '')
| 47 | // Given an old path like /en/enterprise/2.21/user/github/category/article, |
| 48 | // return a new path like /en/enterprise-server@2.21/github/category/article. |
| 49 | export function getNewVersionedPath(oldPath, languageCode = '') { |
| 50 | // It's possible a new version has been injected into an old path |
| 51 | // via syntax like: /en/enterprise/{{ currentVersion }}/admin/category/article |
| 52 | // which could resolve to /en/enterprise/private-instances@latest/admin/category/article, |
| 53 | // in which case the new version is the `private-instances@latest` segment. |
| 54 | // Get the second or third segment depending on whether there is a lang code. |
| 55 | const pathParts = oldPath.split('/') |
| 56 | const possibleVersion = languageCode ? pathParts[3] : pathParts[2] |
| 57 | let newVersion = newVersions.includes(possibleVersion) ? possibleVersion : '' |
| 58 | |
| 59 | // If no new version was found, assume path contains an old version, like 2.21 |
| 60 | if (!newVersion) { |
| 61 | const oldVersion = getOldVersionFromOldPath(oldPath, languageCode) |
| 62 | newVersion = getNewVersionFromOldVersion(oldVersion) |
| 63 | } |
| 64 | |
| 65 | // Remove /<lang>?/enterprise?/<version>?/user? if present. |
| 66 | // This leaves only the part of the string that starts with the product. |
| 67 | // Example: /github/category/article |
| 68 | const restOfString = oldPath.replace(patterns.oldEnterprisePath, '') |
| 69 | |
| 70 | // Add the language and new version to the product part of the string |
| 71 | return path.posix.join('/', languageCode, newVersion, restOfString) |
| 72 | } |
| 73 | |
| 74 | export default { |
| 75 | oldVersions, |
no test coverage detected