1
0
mirror of synced 2025-12-21 10:57:10 -05:00
Files
docs/lib/rewrite-local-links.js
Sarah Schneider aa5a62d49d Remove versions feature flag code (#15793)
* remove FEATURE_NEW_VERSIONS from feature-flags.json

* remove process.env.FEATURE_NEW_VERSIONS from include files

* remove process.env.FEATURE_NEW_VERSIONS from lib files

* remove process.env.FEATURE_NEW_VERSIONS from middleware files

* remove process.env.FEATURE_NEW_VERSIONS from script files

* remove process.env.FEATURE_NEW_VERSIONS from test files

* update test fixtures to use new versions as canonical fixtures
2020-09-29 13:36:07 -04:00

35 lines
1.3 KiB
JavaScript

const externalRedirects = Object.keys(require('./redirects/external-sites'))
const pathUtils = require('./path-utils')
const assert = require('assert')
const nonEnterpriseDefaultVersion = require('./non-enterprise-default-version')
// Content authors write links like `/some/article/path`, but they need to be
// rewritten on the fly to match the current language and page version
module.exports = function rewriteLocalLinks ($, version, languageCode) {
assert(languageCode, 'languageCode is required')
$('a[href^="/"]').each((i, el) => {
getNewHref($(el), languageCode, version)
})
}
function getNewHref (link, languageCode, version) {
const href = link.attr('href')
// these links should not be rewritten
if (href.startsWith('/assets')) return
if (href.startsWith('/public')) return
// Leave old redirected URLs intact
// e.g. `/contact` should not be replaced with `/en/contact`
if (externalRedirects.includes(href)) return
// If link is dotcom-only, just get the language code
// Otherwise, get the versioned path with language code
const newHref = link.hasClass('dotcom-only')
? pathUtils.getVersionedPathWithLanguage(href, nonEnterpriseDefaultVersion, languageCode)
: pathUtils.getVersionedPathWithLanguage(href, version, languageCode)
if (href !== newHref) link.attr('href', newHref)
}