Skip to content

Commit 8bd29d0

Browse files
committed
feat: versioned index files
1 parent 3ff3d7c commit 8bd29d0

File tree

1 file changed

+33
-3
lines changed

1 file changed

+33
-3
lines changed

src/mdx.ts

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ import remarkTocHeadings, {TocNode} from './remark-toc-headings';
2929
import remarkVariables from './remark-variables';
3030
import {FrontMatter, Platform, PlatformConfig} from './types';
3131
import {isTruthy} from './utils';
32+
import {VERSION_INDICATOR} from './versioning';
3233

3334
const root = process.cwd();
3435

@@ -93,6 +94,13 @@ async function getDocsFrontMatterUncached(): Promise<FrontMatter[]> {
9394
if (fm.slug.endsWith(trailingIndex)) {
9495
fm.slug = fm.slug.slice(0, fm.slug.length - trailingIndex.length);
9596
}
97+
98+
// versioned index files get appended to the path (e.g. /path/index__v1 becomes /path__v1)
99+
const versionedIndexFileIndicator = `${trailingIndex}${VERSION_INDICATOR}`;
100+
if (fm.slug.includes(versionedIndexFileIndicator)) {
101+
const segments = fm.slug.split(versionedIndexFileIndicator);
102+
fm.slug = `${segments[0]}${VERSION_INDICATOR}${segments[1]}`;
103+
}
96104
});
97105

98106
return frontMatter;
@@ -237,6 +245,22 @@ function getAllFilesFrontMatter() {
237245
return allFrontMatter;
238246
}
239247

248+
/**
249+
* Generate a file path for versioned content, or return an invalid one if the slug is not versioned
250+
*/
251+
export const getVersionedIndexPath = (
252+
pathRoot: string,
253+
slug: string,
254+
fileExtension: string
255+
) => {
256+
let versionedSlug = 'does/not/exist.mdx';
257+
const segments = slug.split(VERSION_INDICATOR);
258+
if (segments.length === 2) {
259+
versionedSlug = `${segments[0]}/index${VERSION_INDICATOR}${segments[1]}${fileExtension}`;
260+
}
261+
return path.join(pathRoot, versionedSlug);
262+
};
263+
240264
export async function getFileBySlug(slug: string) {
241265
const configPath = path.join(root, slug, 'config.yml');
242266

@@ -247,13 +271,15 @@ export async function getFileBySlug(slug: string) {
247271

248272
let mdxPath = path.join(root, `${slug}.mdx`);
249273
let mdxIndexPath = path.join(root, slug, 'index.mdx');
274+
let versionedMdxIndexPath = getVersionedIndexPath(root, slug, '.mdx');
250275
let mdPath = path.join(root, `${slug}.md`);
251276
let mdIndexPath = path.join(root, slug, 'index.md');
252277

253278
if (
254279
slug.indexOf('docs/platforms/') === 0 &&
255-
[mdxPath, mdxIndexPath, mdPath, mdIndexPath].filter(p => fs.existsSync(p)).length ===
256-
0
280+
[mdxPath, mdxIndexPath, mdPath, mdIndexPath, versionedMdxIndexPath].filter(p =>
281+
fs.existsSync(p)
282+
).length === 0
257283
) {
258284
// Try the common folder.
259285
const slugParts = slug.split('/');
@@ -273,10 +299,14 @@ export async function getFileBySlug(slug: string) {
273299
mdxIndexPath = path.join(root, commonFilePath, 'index.mdx');
274300
mdPath = path.join(root, `${commonFilePath}.md`);
275301
mdIndexPath = path.join(root, commonFilePath, 'index.md');
302+
versionedMdxIndexPath = getVersionedIndexPath(root, commonFilePath, '.mdx');
276303
}
277304
}
278305

279-
const sourcePath = [mdxPath, mdxIndexPath, mdPath].find(fs.existsSync) ?? mdIndexPath;
306+
const sourcePath =
307+
[mdxPath, mdxIndexPath, mdPath, versionedMdxIndexPath].find(fs.existsSync) ??
308+
mdIndexPath;
309+
280310
const source = fs.readFileSync(sourcePath, 'utf8');
281311

282312
process.env.ESBUILD_BINARY_PATH = path.join(

0 commit comments

Comments
 (0)