www: add versioning to docs (#1400)

This commit is contained in:
Marvin Hagemeister
2023-07-04 12:33:47 +02:00
committed by GitHub
parent efcaec3658
commit bf3e82c093
10 changed files with 566 additions and 149 deletions
+75 -34
View File
@@ -19,42 +19,83 @@ export interface TableOfContentsCategoryEntry {
href: string;
}
export const TABLE_OF_CONTENTS: Record<string, TableOfContentsEntry> = {};
export const CATEGORIES: TableOfContentsCategory[] = [];
export const TABLE_OF_CONTENTS: Record<
string,
Record<string, TableOfContentsEntry>
> = {};
export const CATEGORIES: Record<string, TableOfContentsCategory[]> = {};
for (const parent in toc) {
const rawEntry = toc[parent];
const href = `/docs/${parent}`;
const file = `docs/${parent}/index.md`;
const entry = {
slug: parent,
title: rawEntry.title,
href,
file,
};
TABLE_OF_CONTENTS[parent] = entry;
const category: TableOfContentsCategory = {
title: rawEntry.title,
href,
entries: [],
};
CATEGORIES.push(category);
const filteredPages = rawEntry.pages?.filter(([title, page]) =>
title != null && page != null
);
if (filteredPages) {
for (const [id, title] of filteredPages) {
const slug = `${parent}/${id}`;
const href = `/docs/${slug}`;
const file = `docs/${slug}.md`;
const entry = { slug, title, category: parent, href, file };
TABLE_OF_CONTENTS[slug] = entry;
category.entries.push({
title,
href,
});
export const VERSIONS = Object.keys(toc);
export const CANARY_VERSION = toc.canary ? "canary" : "";
export const LATEST_VERSION =
VERSIONS.find((version) => version !== "canary") ?? "";
for (const version in toc) {
const RAW_VERSION = toc[version];
const versionSlug = version === LATEST_VERSION ? "" : `/${version}`;
TABLE_OF_CONTENTS[version] = {};
CATEGORIES[version] = [];
for (const parent in RAW_VERSION.content) {
const rawEntry = RAW_VERSION.content[parent];
// Allow versioned documentation to stack on each other. This should
// only be used for canary versions. This avoids having us to copy
// all documentation content and backport changes.
const fileVersion = rawEntry.link ?? version;
const versionFilePath = fileVersion === LATEST_VERSION
? ""
: `/${fileVersion}`;
const href = `/docs${versionSlug}/${parent}`;
const file = `docs${versionFilePath}/${parent}/index.md`;
const entry = {
slug: parent,
title: rawEntry.title,
href,
file,
};
TABLE_OF_CONTENTS[version][parent] = entry;
const category: TableOfContentsCategory = {
title: rawEntry.title,
href,
entries: [],
};
CATEGORIES[version].push(category);
if (rawEntry.pages) {
for (const [id, title, linkedVersion] of rawEntry.pages) {
const slug = `${parent}/${id}`;
// Allow stacked documentation
const pageVersion = linkedVersion
? linkedVersion.slice("link:".length)
: version;
const versionFilePath = !pageVersion || pageVersion === LATEST_VERSION
? ""
: `/${pageVersion}`;
const href = `/docs${versionSlug}/${slug}`;
const file = `docs${versionFilePath}/${slug}.md`;
const entry = { slug, title, category: parent, href, file };
TABLE_OF_CONTENTS[version][slug] = entry;
category.entries.push({
title,
href,
});
}
}
}
}
export const SLUGS = Object.keys(TABLE_OF_CONTENTS);
export function getFirstPageUrl(version: string) {
const group = TABLE_OF_CONTENTS[version];
if (group) {
for (const slug in group) {
return group[slug].href;
}
}
throw new Error(`Could not find version "${version}"`);
}