* experimenting with redirects * cleanup developer.json * wip * clean console.log * progress * some progress * progress * much progress * debugging tests * hacky progress * ditch latest -> number redirects * minor * hacky progress * lots of progress * some small fixes * fix rendering tests * small fixes * progress * undo debugging * better * routing tests OK * more cleaning * unit tests * undoing lineending edit * undoing temporary debugging * don't ever set this.redirects on Page * cope with archived version redirects * adding code comments on the major if statements * address all feedback * update README about redirects * delete invalid test * fix feedback
45 lines
1.3 KiB
JavaScript
45 lines
1.3 KiB
JavaScript
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.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
|