forked from github/docs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
permalink.js
41 lines (31 loc) · 1.21 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
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'
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.href = removeFPTFromPath(
path.posix.join('/', languageCode, pageVersion, permalinkSuffix)
).replace(patterns.trailingSlash, '$1')
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