| | import assert from 'assert' |
| | import path from 'path' |
| | import patterns from './patterns' |
| | import removeFPTFromPath from '@/versions/lib/remove-fpt-from-path' |
| |
|
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | class Permalink { |
| | languageCode: string |
| | pageVersion: string |
| | relativePath: string |
| | title: string |
| | hrefWithoutLanguage: string |
| | href: string |
| |
|
| | constructor(languageCode: string, pageVersion: string, relativePath: string, title: string) { |
| | this.languageCode = languageCode |
| | this.pageVersion = pageVersion |
| | this.relativePath = relativePath |
| | this.title = title |
| |
|
| | const permalinkSuffix = Permalink.relativePathToSuffix(relativePath) |
| |
|
| | this.hrefWithoutLanguage = removeFPTFromPath( |
| | path.posix.join('/', pageVersion, permalinkSuffix), |
| | ).replace(patterns.trailingSlash, '$1') |
| | this.href = `/${languageCode}${ |
| | this.hrefWithoutLanguage === '/' ? '' : this.hrefWithoutLanguage |
| | }` |
| |
|
| | return this |
| | } |
| |
|
| | static derive( |
| | languageCode: string, |
| | relativePath: string, |
| | title: string, |
| | applicableVersions: string[], |
| | ): Permalink[] { |
| | assert(relativePath, 'relativePath is required') |
| | assert(languageCode, 'languageCode is required') |
| |
|
| | const permalinks = applicableVersions.map((pageVersion: string) => { |
| | return new Permalink(languageCode, pageVersion, relativePath, title) |
| | }) |
| |
|
| | return permalinks |
| | } |
| |
|
| | static relativePathToSuffix(relativePath: string): string { |
| | if (relativePath === 'index.md') return '/' |
| | |
| | |
| | |
| | |
| | return `/${relativePath.replace(indexmdSuffixRegex, '').replace(mdSuffixRegex, '')}` |
| | } |
| | } |
| |
|
| | const indexmdSuffixRegex = new RegExp(`${path.sep}index\\.md$`) |
| | const mdSuffixRegex = /\.md$/ |
| |
|
| | export default Permalink |
| |
|