forked from github/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrewrite-asset-paths-to-s3.js
29 lines (24 loc) · 1.33 KB
/
rewrite-asset-paths-to-s3.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
const latestEnterpriseVersion = require('./enterprise-server-releases').latest
const nonEnterpriseDefaultVersion = require('./non-enterprise-default-version')
const { getOldVersionFromNewVersion } = require('./old-versions-utils')
const s3BasePath = 'https://github-images.s3.amazonaws.com'
// 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 ($, version, relativePath) {
// if the current version is non-enterprise, do not rewrite
if (version === nonEnterpriseDefaultVersion) return
// get 2.22 from [email protected]
const oldVersion = getOldVersionFromNewVersion(version)
// 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 version, do not rewrite
if (product === 'admin' && oldVersion === latestEnterpriseVersion) return
$('img').each((i, el) => {
const src = $(el).attr('src')
if (!src.startsWith('/assets/images')) return
const newSrc = `${s3BasePath}/enterprise/${oldVersion}${src}`
if (src !== newSrc) $(el).attr('src', newSrc)
})
}