|
| 1 | +const path = require('path') |
| 2 | +const visit = require('unist-util-visit') |
| 3 | +const externalRedirects = Object.keys(require('../../redirects/external-sites')) |
| 4 | +const { getPathWithoutLanguage, getVersionStringFromPath } = require('../../path-utils') |
| 5 | +const { getNewVersionedPath } = require('../../old-versions-utils') |
| 6 | +const patterns = require('../../patterns') |
| 7 | +const { deprecated, latest } = require('../../enterprise-server-releases') |
| 8 | +const nonEnterpriseDefaultVersion = require('../../non-enterprise-default-version') |
| 9 | +const allVersions = require('../../all-versions') |
| 10 | +const supportedVersions = Object.keys(allVersions) |
| 11 | +const supportedPlans = Object.values(allVersions).map(v => v.plan) |
| 12 | +const removeFPTFromPath = require('../../remove-fpt-from-path') |
| 13 | + |
| 14 | +// Matches any <a> tags with an href that starts with `/` |
| 15 | +const matcher = node => ( |
| 16 | + node.type === 'element' && |
| 17 | + node.tagName === 'a' && |
| 18 | + node.properties && |
| 19 | + node.properties.href && |
| 20 | + node.properties.href.startsWith('/') |
| 21 | +) |
| 22 | + |
| 23 | +// Content authors write links like `/some/article/path`, but they need to be |
| 24 | +// rewritten on the fly to match the current language and page version |
| 25 | +module.exports = function rewriteLocalLinks ({ languageCode, version }) { |
| 26 | + // There's no languageCode or version passed, so nothing to do |
| 27 | + if (!languageCode || !version) return |
| 28 | + |
| 29 | + return ast => { |
| 30 | + visit(ast, matcher, node => { |
| 31 | + const newHref = getNewHref(node, languageCode, version) |
| 32 | + if (newHref) { |
| 33 | + node.properties.href = newHref |
| 34 | + } |
| 35 | + }) |
| 36 | + } |
| 37 | +} |
| 38 | + |
| 39 | +function getNewHref (node, languageCode, version) { |
| 40 | + const { href } = node.properties |
| 41 | + // Exceptions to link rewriting |
| 42 | + if (href.startsWith('/assets')) return |
| 43 | + if (href.startsWith('/public')) return |
| 44 | + if (externalRedirects.includes(href)) return |
| 45 | + |
| 46 | + let newHref = href |
| 47 | + // If the link has a hardcoded plan or version in it, do not update other than adding a language code |
| 48 | + // Examples: |
| 49 | + // /enterprise-server@2.20/rest/reference/oauth-authorizations |
| 50 | + // /enterprise-server/rest/reference/oauth-authorizations (this redirects to the latest version) |
| 51 | + // /enterprise-server@latest/rest/reference/oauth-authorizations (this redirects to the latest version) |
| 52 | + const firstLinkSegment = href.split('/')[1] |
| 53 | + if ([...supportedPlans, ...supportedVersions, 'enterprise-server@latest'].includes(firstLinkSegment)) { |
| 54 | + newHref = path.join('/', languageCode, href) |
| 55 | + } |
| 56 | + |
| 57 | + // If the link includes a deprecated version, do not update other than adding a language code |
| 58 | + // Example: /enterprise/11.10.340/admin/articles/upgrading-to-the-latest-release |
| 59 | + const oldEnterpriseVersionNumber = href.match(patterns.getEnterpriseVersionNumber) |
| 60 | + if (oldEnterpriseVersionNumber && deprecated.includes(oldEnterpriseVersionNumber[1])) { |
| 61 | + newHref = path.join('/', languageCode, href) |
| 62 | + } |
| 63 | + |
| 64 | + if (newHref === href) { |
| 65 | + // start clean with no language (TOC pages already include the lang codes via lib/liquid-tags/link.js) |
| 66 | + const hrefWithoutLang = getPathWithoutLanguage(href) |
| 67 | + |
| 68 | + // normalize any legacy links so they conform to new link structure |
| 69 | + newHref = path.posix.join('/', languageCode, getNewVersionedPath(hrefWithoutLang)) |
| 70 | + |
| 71 | + // get the current version from the link |
| 72 | + const versionFromHref = getVersionStringFromPath(newHref) |
| 73 | + |
| 74 | + // ------ BEGIN ONE-OFF OVERRIDES ------// |
| 75 | + // dotcom-only links always point to dotcom |
| 76 | + if (node.properties.className && node.properties.className.includes('dotcom-only')) { |
| 77 | + version = nonEnterpriseDefaultVersion |
| 78 | + } |
| 79 | + |
| 80 | + // desktop links always point to dotcom |
| 81 | + if (patterns.desktop.test(hrefWithoutLang)) { |
| 82 | + version = nonEnterpriseDefaultVersion |
| 83 | + } |
| 84 | + |
| 85 | + // admin links on dotcom always point to Enterprise |
| 86 | + if (patterns.adminProduct.test(hrefWithoutLang) && version === nonEnterpriseDefaultVersion) { |
| 87 | + version = `enterprise-server@${latest}` |
| 88 | + } |
| 89 | + |
| 90 | + // insights links on dotcom always point to Enterprise |
| 91 | + if (patterns.insightsProduct.test(hrefWithoutLang) && version === nonEnterpriseDefaultVersion) { |
| 92 | + version = `enterprise-server@${latest}` |
| 93 | + } |
| 94 | + // ------ END ONE-OFF OVERRIDES ------// |
| 95 | + |
| 96 | + // update the version in the link |
| 97 | + newHref = removeFPTFromPath(newHref.replace(versionFromHref, version)) |
| 98 | + } |
| 99 | + |
| 100 | + newHref = newHref.replace(patterns.trailingSlash, '$1') |
| 101 | + return newHref |
| 102 | +} |
0 commit comments