Skip to content

Commit

Permalink
Move the rewriteLocalLinks behavior to an AST pipeline plugin (github…
Browse files Browse the repository at this point in the history
…#17550)

* Write our plugin

* Include it

* Move the RegEx

* Don't rewriteLocalLinks with cheerio anymore

* Process after HTML ast is generated

* Use the same logic as before, just to see if it'll pass

* Don't require languageCode/version

* Only work on local links

* Needs an href

* Only update href if there's a new one to use

* Check for node.properties

* Some links are just mean
  • Loading branch information
JasonEtco authored Jan 29, 2021
1 parent 8d4f3e6 commit 989006b
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 12 deletions.
10 changes: 0 additions & 10 deletions lib/page.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ const cheerio = require('cheerio')
const patterns = require('./patterns')
const getMapTopicContent = require('./get-map-topic-content')
const rewriteAssetPathsToS3 = require('./rewrite-asset-paths-to-s3')
const rewriteLocalLinks = require('./rewrite-local-links')
const getApplicableVersions = require('./get-applicable-versions')
const encodeBracketedParentheses = require('./encode-bracketed-parentheses')
const generateRedirectsForPermalinks = require('./redirects/permalinks')
Expand Down Expand Up @@ -156,12 +155,6 @@ class Page {
}

this.intro = await renderContent(this.rawIntro, context)

// rewrite local links in the intro to include current language code and GHE version if needed
const introHtml = cheerio.load(this.intro)
rewriteLocalLinks(introHtml, context.currentVersion, context.currentLanguage)
this.intro = introHtml('body').html()

this.introPlainText = await renderContent(this.rawIntro, context, { textOnly: true })
this.title = await renderContent(this.rawTitle, context, { textOnly: true, encodeEntities: true })
this.shortTitle = await renderContent(this.shortTitle, context, { textOnly: true, encodeEntities: true })
Expand Down Expand Up @@ -249,9 +242,6 @@ class Page {
if (englishHeadings) useEnglishHeadings($, englishHeadings)
}

// rewrite local links to include current language code and GHE version if needed
rewriteLocalLinks($, context.currentVersion, context.currentLanguage)

// wrap ordered list images in a container div
$('ol > li img').each((i, el) => {
$(el).wrap('<div class="procedural-image-wrapper" />')
Expand Down
4 changes: 3 additions & 1 deletion lib/render-content/create-processor.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ const html = require('rehype-stringify')
const graphql = require('highlightjs-graphql').definer
const remarkCodeExtra = require('remark-code-extra')
const codeHeader = require('./plugins/code-header')
const rewriteLocalLinks = require('./plugins/rewrite-local-links')

module.exports = function createProcessor () {
module.exports = function createProcessor (context) {
return unified()
.use(markdown)
.use(remarkCodeExtra, { transform: codeHeader })
Expand All @@ -21,5 +22,6 @@ module.exports = function createProcessor () {
.use(autolinkHeadings, { behavior: 'wrap' })
.use(highlight, { languages: { graphql }, subset: false })
.use(raw)
.use(rewriteLocalLinks, { languageCode: context.currentLanguage, version: context.currentVersion })
.use(html)
}
102 changes: 102 additions & 0 deletions lib/render-content/plugins/rewrite-local-links.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
const path = require('path')
const visit = require('unist-util-visit')
const externalRedirects = Object.keys(require('../../redirects/external-sites'))
const { getPathWithoutLanguage, getVersionStringFromPath } = require('../../path-utils')
const { getNewVersionedPath } = require('../../old-versions-utils')
const patterns = require('../../patterns')
const { deprecated, latest } = require('../../enterprise-server-releases')
const nonEnterpriseDefaultVersion = require('../../non-enterprise-default-version')
const allVersions = require('../../all-versions')
const supportedVersions = Object.keys(allVersions)
const supportedPlans = Object.values(allVersions).map(v => v.plan)
const removeFPTFromPath = require('../../remove-fpt-from-path')

// Matches any <a> tags with an href that starts with `/`
const matcher = node => (
node.type === 'element' &&
node.tagName === 'a' &&
node.properties &&
node.properties.href &&
node.properties.href.startsWith('/')
)

// 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 ({ languageCode, version }) {
// There's no languageCode or version passed, so nothing to do
if (!languageCode || !version) return

return ast => {
visit(ast, matcher, node => {
const newHref = getNewHref(node, languageCode, version)
if (newHref) {
node.properties.href = newHref
}
})
}
}

function getNewHref (node, languageCode, version) {
const { href } = node.properties
// Exceptions to link rewriting
if (href.startsWith('/assets')) return
if (href.startsWith('/public')) return
if (externalRedirects.includes(href)) return

let newHref = href
// If the link has a hardcoded plan or version in it, do not update other than adding a language code
// Examples:
// /[email protected]/rest/reference/oauth-authorizations
// /enterprise-server/rest/reference/oauth-authorizations (this redirects to the latest version)
// /enterprise-server@latest/rest/reference/oauth-authorizations (this redirects to the latest version)
const firstLinkSegment = href.split('/')[1]
if ([...supportedPlans, ...supportedVersions, 'enterprise-server@latest'].includes(firstLinkSegment)) {
newHref = path.join('/', languageCode, href)
}

// If the link includes a deprecated version, do not update other than adding a language code
// Example: /enterprise/11.10.340/admin/articles/upgrading-to-the-latest-release
const oldEnterpriseVersionNumber = href.match(patterns.getEnterpriseVersionNumber)
if (oldEnterpriseVersionNumber && deprecated.includes(oldEnterpriseVersionNumber[1])) {
newHref = path.join('/', languageCode, href)
}

if (newHref === href) {
// start clean with no language (TOC pages already include the lang codes via lib/liquid-tags/link.js)
const hrefWithoutLang = getPathWithoutLanguage(href)

// normalize any legacy links so they conform to new link structure
newHref = path.posix.join('/', languageCode, getNewVersionedPath(hrefWithoutLang))

// get the current version from the link
const versionFromHref = getVersionStringFromPath(newHref)

// ------ BEGIN ONE-OFF OVERRIDES ------//
// dotcom-only links always point to dotcom
if (node.properties.className && node.properties.className.includes('dotcom-only')) {
version = nonEnterpriseDefaultVersion
}

// desktop links always point to dotcom
if (patterns.desktop.test(hrefWithoutLang)) {
version = nonEnterpriseDefaultVersion
}

// admin links on dotcom always point to Enterprise
if (patterns.adminProduct.test(hrefWithoutLang) && version === nonEnterpriseDefaultVersion) {
version = `enterprise-server@${latest}`
}

// insights links on dotcom always point to Enterprise
if (patterns.insightsProduct.test(hrefWithoutLang) && version === nonEnterpriseDefaultVersion) {
version = `enterprise-server@${latest}`
}
// ------ END ONE-OFF OVERRIDES ------//

// update the version in the link
newHref = removeFPTFromPath(newHref.replace(versionFromHref, version))
}

newHref = newHref.replace(patterns.trailingSlash, '$1')
return newHref
}
2 changes: 1 addition & 1 deletion lib/render-content/renderContent.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ module.exports = async function renderContent (
// statements so that extra space doesn't mess with list numbering
template = template.replace(/(\r?\n){3}/g, '\n\n')

const processor = createProcessor()
const processor = createProcessor(context)
const vFile = await processor.process(template)
let html = vFile.toString()

Expand Down

0 comments on commit 989006b

Please sign in to comment.