forked from github/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpermalink.js
70 lines (60 loc) · 2.44 KB
/
permalink.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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import assert from 'assert'
import path from 'path'
import patterns from './patterns.js'
import { allVersions } from './all-versions.js'
import removeFPTFromPath from './remove-fpt-from-path.js'
/*
This class creates the "permalinks" that power a page's different versions,
where the source for the versions is a content file's frontmatter. The
page.permalinks is an array of objects that looks like this:
[
{
"languageCode": "en",
"pageVersion": "free-pro-team@latest",
"relativePath": "billing/managing-billing-for-your-github-account/index.md",
"title": "Managing billing for your GitHub account",
"hrefWithoutLanguage": "/billing/managing-billing-for-your-github-account",
"href": "/en/billing/managing-billing-for-your-github-account",
"pageVersionTitle": "Free, Pro, & Team"
},
{
"languageCode": "en",
"pageVersion": "enterprise-cloud@latest",
"relativePath": "billing/managing-billing-for-your-github-account/index.md",
"title": "Managing billing for your GitHub account",
"hrefWithoutLanguage": "/enterprise-cloud@latest/billing/managing-billing-for-your-github-account",
"href": "/en/enterprise-cloud@latest/billing/managing-billing-for-your-github-account",
"pageVersionTitle": "Enterprise Cloud"
}
... and so on for each of the content file's supported versions.
]
*/
class Permalink {
constructor(languageCode, pageVersion, relativePath, title) {
this.languageCode = languageCode
this.pageVersion = pageVersion
this.relativePath = relativePath
this.title = title
const permalinkSuffix = this.constructor.relativePathToSuffix(relativePath)
this.hrefWithoutLanguage = removeFPTFromPath(
path.posix.join('/', pageVersion, permalinkSuffix)
).replace(patterns.trailingSlash, '$1')
this.href = `/${languageCode}${
this.hrefWithoutLanguage === '/' ? '' : this.hrefWithoutLanguage
}`
this.pageVersionTitle = allVersions[pageVersion].versionTitle
return this
}
static derive(languageCode, relativePath, title, applicableVersions) {
assert(relativePath, 'relativePath is required')
assert(languageCode, 'languageCode is required')
const permalinks = applicableVersions.map((pageVersion) => {
return new Permalink(languageCode, pageVersion, relativePath, title)
})
return permalinks
}
static relativePathToSuffix(relativePath) {
return '/' + relativePath.replace('index.md', '').replace('.md', '')
}
}
export default Permalink