Skip to content

Commit 45b46e8

Browse files
authored
repo sync
2 parents c1cbd88 + 989006b commit 45b46e8

20 files changed

+122
-36
lines changed

lib/add-frontmatter-to-file.js

Lines changed: 0 additions & 8 deletions
This file was deleted.

lib/page.js

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ const cheerio = require('cheerio')
55
const patterns = require('./patterns')
66
const getMapTopicContent = require('./get-map-topic-content')
77
const rewriteAssetPathsToS3 = require('./rewrite-asset-paths-to-s3')
8-
const rewriteLocalLinks = require('./rewrite-local-links')
98
const getApplicableVersions = require('./get-applicable-versions')
109
const encodeBracketedParentheses = require('./encode-bracketed-parentheses')
1110
const generateRedirectsForPermalinks = require('./redirects/permalinks')
@@ -156,12 +155,6 @@ class Page {
156155
}
157156

158157
this.intro = await renderContent(this.rawIntro, context)
159-
160-
// rewrite local links in the intro to include current language code and GHE version if needed
161-
const introHtml = cheerio.load(this.intro)
162-
rewriteLocalLinks(introHtml, context.currentVersion, context.currentLanguage)
163-
this.intro = introHtml('body').html()
164-
165158
this.introPlainText = await renderContent(this.rawIntro, context, { textOnly: true })
166159
this.title = await renderContent(this.rawTitle, context, { textOnly: true, encodeEntities: true })
167160
this.shortTitle = await renderContent(this.shortTitle, context, { textOnly: true, encodeEntities: true })
@@ -249,9 +242,6 @@ class Page {
249242
if (englishHeadings) useEnglishHeadings($, englishHeadings)
250243
}
251244

252-
// rewrite local links to include current language code and GHE version if needed
253-
rewriteLocalLinks($, context.currentVersion, context.currentLanguage)
254-
255245
// wrap ordered list images in a container div
256246
$('ol > li img').each((i, el) => {
257247
$(el).wrap('<div class="procedural-image-wrapper" />')

lib/render-content/create-processor.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@ const html = require('rehype-stringify')
1010
const graphql = require('highlightjs-graphql').definer
1111
const remarkCodeExtra = require('remark-code-extra')
1212
const codeHeader = require('./plugins/code-header')
13+
const rewriteLocalLinks = require('./plugins/rewrite-local-links')
1314

14-
module.exports = function createProcessor () {
15+
module.exports = function createProcessor (context) {
1516
return unified()
1617
.use(markdown)
1718
.use(remarkCodeExtra, { transform: codeHeader })
@@ -21,5 +22,6 @@ module.exports = function createProcessor () {
2122
.use(autolinkHeadings, { behavior: 'wrap' })
2223
.use(highlight, { languages: { graphql }, subset: false })
2324
.use(raw)
25+
.use(rewriteLocalLinks, { languageCode: context.currentLanguage, version: context.currentVersion })
2426
.use(html)
2527
}
Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
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+
}

lib/render-content/renderContent.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ module.exports = async function renderContent (
5252
// statements so that extra space doesn't mess with list numbering
5353
template = template.replace(/(\r?\n){3}/g, '\n\n')
5454

55-
const processor = createProcessor()
55+
const processor = createProcessor(context)
5656
const vFile = await processor.process(template)
5757
let html = vFile.toString()
5858

script/check-s3-images.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ const allVersions = require('../lib/all-versions')
99
const nonEnterpriseDefaultVersion = require('../lib/non-enterprise-default-version')
1010
const { getS3BucketPathFromVersion, getVersionFromS3BucketPath } = require('../lib/s3-bucket-path-utils')
1111
const patterns = require('../lib/patterns')
12-
const authenticateToAWS = require('../lib/authenticate-to-aws.js')
12+
const authenticateToAWS = require('./helpers/authenticate-to-aws.js')
1313
const readlineSync = require('readline-sync')
1414
const { execSync } = require('child_process')
1515
const enterpriseServerVersions = Object.keys(allVersions).filter(v => v.startsWith('enterprise-server@'))

script/delete-unused-staging-apps.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ assert(process.env.HEROKU_API_TOKEN)
1414
const { chain } = require('lodash')
1515
const chalk = require('chalk')
1616
const Heroku = require('heroku-client')
17-
const github = require('../lib/github')()
17+
const github = require('./helpers/github')()
1818
const heroku = new Heroku({ token: process.env.HEROKU_API_TOKEN })
1919
const owner = 'github'
2020
const repo = 'docs-internal'

script/graphql/update-files.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ const yaml = require('js-yaml')
77
const { execSync } = require('child_process')
88
const graphqlDataDir = path.join(process.cwd(), 'data/graphql')
99
const graphqlStaticDir = path.join(process.cwd(), 'lib/graphql/static')
10-
const { getContents, listMatchingRefs } = require('../../lib/git-utils')
10+
const { getContents, listMatchingRefs } = require('../helpers/git-utils')
1111
const dataFilenames = require('./utils/data-filenames')
1212
const allVersions = require('../../lib/all-versions')
1313
const processPreviews = require('./utils/process-previews')
File renamed without changes.

lib/find-extraneous-translation-files.js renamed to script/helpers/find-extraneous-translation-files.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
const path = require('path')
22
const { difference } = require('lodash')
33
const walk = require('walk-sync').entries
4-
const languages = require('../lib/languages')
4+
const languages = require('../../lib/languages')
55

66
module.exports = function findExtraneousTranslatedFiles () {
77
const files = []

lib/find-unused-assets.js renamed to script/helpers/find-unused-assets.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,10 @@ const path = require('path')
55
const walk = require('walk-sync')
66
const { execSync } = require('child_process')
77
const assert = require('assert')
8-
const loadSiteData = require('./site-data')
9-
const { loadPages } = require('./pages')
10-
const patterns = require('./patterns')
11-
const getDataReferences = require('./get-liquid-data-references')
8+
const loadSiteData = require('../../lib/site-data')
9+
const { loadPages } = require('../../lib/pages')
10+
const patterns = require('../../lib/patterns')
11+
const getDataReferences = require('../../lib/get-liquid-data-references')
1212
const imagesPath = '/assets/images'
1313

1414
// these paths should remain in the repo even if they are not referenced directly

lib/git-utils.js renamed to script/helpers/git-utils.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const github = require('../lib/github')()
1+
const github = require('./github')()
22

33
// https://docs.github.com/rest/reference/git#get-a-reference
44
async function getCommitSha (owner, repo, ref) {
File renamed without changes.

script/remove-extraneous-translation-files.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#!/usr/bin/env node
22

33
const fs = require('fs')
4-
const findExtraneousFiles = require('../lib/find-extraneous-translation-files')
4+
const findExtraneousFiles = require('./helpers/find-extraneous-translation-files')
55

66
// [start-readme]
77
//

script/remove-unused-assets.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
const fs = require('fs')
44
const path = require('path')
5-
const findUnusedAssets = require('../lib/find-unused-assets')
5+
const findUnusedAssets = require('./helpers/find-unused-assets')
66

77
// [start-readme]
88
//

script/update-enterprise-dates.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/usr/bin/env node
22

3-
const { getContents } = require('../lib/git-utils')
3+
const { getContents } = require('./helpers/git-utils')
44
const fs = require('fs')
55
const path = require('path')
66
const enterpriseDatesFile = path.join(__dirname, '../lib/enterprise-dates.json')

script/update-s3cmd-config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/usr/bin/env node
22

3-
const authenticateToAWS = require('../lib/authenticate-to-aws.js')
3+
const authenticateToAWS = require('./helpers/authenticate-to-aws.js')
44

55
// [start-readme]
66
//

script/upload-images-to-s3.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ const fs = require('fs')
44
const path = require('path')
55
const program = require('commander')
66
const allVersions = require('../lib/all-versions')
7-
const authenticateToAWS = require('../lib/authenticate-to-aws')
7+
const authenticateToAWS = require('./helpers/authenticate-to-aws')
88
const nonEnterpriseDefaultVersion = require('../lib/non-enterprise-default-version')
99
const { getS3BucketPathFromVersion } = require('../lib/s3-bucket-path-utils')
1010
const walk = require('walk-sync')

tests/content/crowdin-config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
const config = require('../../lib/crowdin-config').read()
1+
const config = require('../helpers/crowdin-config').read()
22
const { loadPages } = require('../../lib/pages')
33
const ignoredPagePaths = config.files[0].ignore
44
const ignoredDataPaths = config.files[2].ignore

lib/crowdin-config.js renamed to tests/helpers/crowdin-config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ const path = require('path')
33
const yaml = require('js-yaml')
44

55
const read = function () {
6-
const filename = path.join(__dirname, '../crowdin.yml')
6+
const filename = path.join(__dirname, '../../crowdin.yml')
77
return yaml.load(fs.readFileSync(filename, 'utf8'), { filename })
88
}
99

0 commit comments

Comments
 (0)