feat(content-docs): new front matter options to customize pagination (#5705)

This commit is contained in:
Joshua Chen 2021-10-21 18:27:57 +08:00 committed by GitHub
parent 29d13351a4
commit 3127f12654
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
15 changed files with 489 additions and 95 deletions

View file

@ -22,9 +22,8 @@ import {
} from '@docusaurus/utils';
import {LoadContext, Plugin, RouteConfig} from '@docusaurus/types';
import {loadSidebars} from './sidebars';
import {createSidebarsUtils} from './sidebars/utils';
import {CategoryMetadataFilenamePattern} from './sidebars/generator';
import {readVersionDocs, processDocMetadata} from './docs';
import {readVersionDocs, processDocMetadata, handleNavigation} from './docs';
import {getDocsDirPaths, readVersionsMetadata} from './versions';
import {
@ -35,7 +34,6 @@ import {
DocMetadata,
GlobalPluginData,
VersionMetadata,
DocNavLink,
LoadedVersion,
DocFile,
DocsMarkdownOption,
@ -165,10 +163,6 @@ export default function pluginContentDocs(
const docsBase: DocMetadataBase[] = await loadVersionDocsBase(
versionMetadata,
);
const docsBaseById: Record<string, DocMetadataBase> = keyBy(
docsBase,
(doc) => doc.id,
);
const sidebars = await loadSidebars(versionMetadata.sidebarFilePath, {
sidebarItemsGenerator: options.sidebarItemsGenerator,
@ -180,70 +174,14 @@ export default function pluginContentDocs(
sidebarCollapsible: options.sidebarCollapsible,
},
});
const {
checkSidebarsDocIds,
getDocNavigation,
getFirstDocIdOfFirstSidebar,
} = createSidebarsUtils(sidebars);
const validDocIds = Object.keys(docsBaseById);
checkSidebarsDocIds(
validDocIds,
versionMetadata.sidebarFilePath as string,
);
// Add sidebar/next/previous to the docs
function addNavData(doc: DocMetadataBase): DocMetadata {
const {sidebarName, previousId, nextId} = getDocNavigation(doc.id);
const toDocNavLink = (navDocId: string): DocNavLink => {
const {title, permalink, frontMatter} = docsBaseById[navDocId];
return {
title:
frontMatter.pagination_label ??
frontMatter.sidebar_label ??
title,
permalink,
};
};
return {
...doc,
sidebar: sidebarName,
previous: previousId ? toDocNavLink(previousId) : undefined,
next: nextId ? toDocNavLink(nextId) : undefined,
};
}
const docs = docsBase.map(addNavData);
// sort to ensure consistent output for tests
docs.sort((a, b) => a.id.localeCompare(b.id));
// The "main doc" is the "version entry point"
// We browse this doc by clicking on a version:
// - the "home" doc (at '/docs/')
// - the first doc of the first sidebar
// - a random doc (if no docs are in any sidebar... edge case)
function getMainDoc(): DocMetadata {
const versionHomeDoc = docs.find(
(doc) =>
doc.unversionedId === options.homePageId || doc.slug === '/',
);
const firstDocIdOfFirstSidebar = getFirstDocIdOfFirstSidebar();
if (versionHomeDoc) {
return versionHomeDoc;
} else if (firstDocIdOfFirstSidebar) {
return docs.find((doc) => doc.id === firstDocIdOfFirstSidebar)!;
} else {
return docs[0];
}
}
return {
...versionMetadata,
mainDocId: getMainDoc().unversionedId,
...handleNavigation(
docsBase,
sidebars,
versionMetadata.sidebarFilePath as string,
),
sidebars,
docs: docs.map(addNavData),
};
}