forked from github/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remove cheerio from Page#render (github#17566)
* 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 * Move use-english-headings to be a plugin * Bail if no englishHeadings were passed * Install rehype-wrap * Wrap ol > li img in div.procedural-image-wrapper * Test for platform without cheerio * Use a plugin for rewriteAssetPathsToS3 * Remove cheerio from page.render * Fix require paths * SImplify * Fix some 🐛s * Use our own rehype-wrap * Move rewriteAssetPathsToS3 after HTML AST * Remove some console logs * Fix check for includesPlatformSpecificContent * Rename ast => tree
- Loading branch information
Showing
8 changed files
with
186 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
const visit = require('unist-util-visit') | ||
const latestEnterpriseRelease = require('../../enterprise-server-releases').latest | ||
const nonEnterpriseDefaultVersion = require('../../non-enterprise-default-version') | ||
const { getS3BucketPathFromVersion } = require('../../s3-bucket-path-utils') | ||
const allVersions = require('../../all-versions') | ||
const s3BasePath = 'https://github-images.s3.amazonaws.com' | ||
|
||
const matcher = node => ( | ||
node.type === 'element' && | ||
node.tagName === 'img' && | ||
node.properties.src && | ||
node.properties.src.startsWith('/assets/images') | ||
) | ||
|
||
// This module rewrites asset paths on Enterprise versions to S3 paths. | ||
// Source example: /assets/images/foo.png | ||
// Rewritten: https://github-images.s3.amazonaws.com/enterprise/2.20/assets/images/foo.png | ||
// The one exception is Admin pages on the latest GHES release. | ||
module.exports = function rewriteAssetPathsToS3 ({ currentVersion, relativePath }) { | ||
// Bail if we don't have a relativePath in this context | ||
if (!relativePath) return | ||
|
||
// skip if this is the homepage | ||
if (relativePath === 'index.md') return | ||
|
||
// if the current version is non-enterprise, do not rewrite | ||
if (currentVersion === nonEnterpriseDefaultVersion) return | ||
|
||
// the relativePath starts with the product, like /admin/foo or /github/foo | ||
const product = relativePath.split('/')[0] | ||
|
||
// if this is an Admin page on the latest GHES release, do not rewrite | ||
if (product === 'admin' && allVersions[currentVersion].currentRelease === latestEnterpriseRelease) return | ||
|
||
// if the version is enterprise-server@2.22, use `enterprise/2.22` as the bucket path | ||
// otherwise, use the plan name, e.g., `github-ae` | ||
const bucketPath = getS3BucketPathFromVersion(currentVersion) | ||
|
||
return tree => { | ||
visit(tree, matcher, node => { | ||
// Rewrite the node's src | ||
node.properties.src = `${s3BasePath}/${bucketPath}${node.properties.src}` | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
const GithubSlugger = require('github-slugger') | ||
const Entities = require('html-entities').XmlEntities | ||
const visit = require('unist-util-visit') | ||
const slugger = new GithubSlugger() | ||
const entities = new Entities() | ||
|
||
const matcher = node => ( | ||
node.type === 'element' && | ||
['h2', 'h3', 'h4'].includes(node.tagName) | ||
) | ||
|
||
// replace translated IDs and links in headings with English | ||
module.exports = function useEnglishHeadings ({ englishHeadings }) { | ||
if (!englishHeadings) return | ||
return tree => { | ||
visit(tree, matcher, node => { | ||
const text = node.children.find(n => n.type === 'text').value | ||
// find English heading in the collection | ||
const englishHeading = englishHeadings[entities.encode(text)] | ||
// get English slug | ||
const englishSlug = slugger.slug(englishHeading) | ||
// use English slug for heading ID and link | ||
node.properties.id = englishSlug | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
const visit = require('unist-util-visit') | ||
const { selectAll } = require('hast-util-select') | ||
const parseSelector = require('hast-util-parse-selector') | ||
|
||
/* | ||
* Attacher | ||
*/ | ||
module.exports = options => { | ||
options = options || {} | ||
const selector = options.selector || options.select || 'body' | ||
const wrapper = options.wrapper || options.wrap | ||
|
||
/* | ||
* Transformer | ||
*/ | ||
return tree => { | ||
if (typeof wrapper !== 'string') { | ||
throw new TypeError('Expected a `string` as wrapper') | ||
} | ||
|
||
if (typeof selector !== 'string') { | ||
throw new TypeError('Expected a `string` as selector') | ||
} | ||
|
||
for (const match of selectAll(options.selector, tree)) { | ||
visit(tree, match, (node, i, parent) => { | ||
const wrapper = parseSelector('div') | ||
wrapper.children = [node] | ||
parent.children[i] = wrapper | ||
}) | ||
} | ||
} | ||
} |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters