Skip to content

Commit

Permalink
Swizzle version banner
Browse files Browse the repository at this point in the history
  • Loading branch information
Dr-Electron authored May 31, 2024
1 parent 80fc695 commit a5ac39a
Show file tree
Hide file tree
Showing 8 changed files with 247 additions and 176 deletions.
11 changes: 0 additions & 11 deletions src/plugins/docs/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,25 +1,14 @@
import {
Options as DocsOptions,
PluginOptions as DocsPluginOptions,
LoadedContent as DocsLoadedContent,
DocMetadata as DocsDocMetadata,
} from '@docusaurus/plugin-content-docs';
import {
OptionValidationContext as DocsOptionValidationContext,
Validate,
} from '@docusaurus/types';

export type DocMetadata = DocsDocMetadata & {
bannerContent?: string;
};

export type LoadedContent = DocsLoadedContent & {
bannerContent?: string;
};

export type PluginOptions = DocsPluginOptions & {
globalSidebars: string[];
bannerPath?: string;
};

export type Options = Partial<PluginOptions>;
Expand Down
106 changes: 45 additions & 61 deletions src/plugins/docs/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,57 +43,21 @@ var __importStar =
__setModuleDefault(result, mod);
return result;
};
var __importDefault =
(this && this.__importDefault) ||
function (mod) {
return mod && mod.__esModule ? mod : { default: mod };
};
Object.defineProperty(exports, '__esModule', { value: true });
exports.validateOptions = void 0;
const plugin_content_docs_1 = __importStar(
require('@docusaurus/plugin-content-docs'),
);
const promises_1 = __importDefault(require('fs/promises'));
const path_1 = __importDefault(require('path'));
async function pluginDocs(context, options) {
// Destructure to separate the Docusaurus docs plugin options
// and initialize the Docusaurus docs plugin to wrap.
const { bannerPath, globalSidebars, ...docsOptions } = options;
const { globalSidebars, ...docsOptions } = options;
const plugin = await (0, plugin_content_docs_1.default)(context, docsOptions);
return {
...plugin,
getPathsToWatch: () => {
const pathsToWatch = plugin.getPathsToWatch();
if (bannerPath)
pathsToWatch.push(path_1.default.resolve(context.siteDir, bannerPath));
return pathsToWatch;
},
loadContent: async () => {
const docsLoadedContent = await plugin.loadContent();
return {
...docsLoadedContent,
// Load banner content from file
bannerContent: bannerPath
? await promises_1.default.readFile(bannerPath, {
encoding: 'utf-8',
})
: undefined,
};
},
translateContent: ({ content, ...args }) => {
// Propagate banner content
const { bannerContent, ...docsContent } = content;
const docsContentLoaded = plugin.translateContent({
content: docsContent,
...args,
});
return {
...docsContentLoaded,
bannerContent,
};
},
// Override the `contentLoaded` function to add sidebars to the
// global data exposed by the Docusaurus docs plugin.
contentLoaded: async ({ actions, content, ...args }) => {
const { bannerContent, ...docsContent } = content;
const globalSidebarEntries = [];
const createData = async (name, data) => {
// Hook into the `createData` call to extract the sidebars we need.
Expand All @@ -105,18 +69,7 @@ async function pluginDocs(context, options) {
.filter(([sidebarId]) => globalSidebars.includes(sidebarId))
.forEach((entry) => globalSidebarEntries.push(entry));
}
return await actions.createData(
name,
// Expose banner content to be used by the DocBanner theme component
JSON.stringify(
{
...versionMetadata,
bannerContent,
},
null,
2,
),
);
return await actions.createData(name, data);
};
const setGlobalData = (data) => {
actions.setGlobalData({
Expand All @@ -126,7 +79,7 @@ async function pluginDocs(context, options) {
};
await plugin.contentLoaded({
...args,
content: docsContent,
content,
actions: {
...actions,
createData,
Expand All @@ -136,15 +89,46 @@ async function pluginDocs(context, options) {
},
};
}
pluginDocs.validateOptions = ({ validate, options }) => {
const { bannerPath, globalSidebars = [], ...docsOptions } = options;
exports.default = pluginDocs;
function validateOptions({ validate, options }) {
const { versions = {}, globalSidebars = [], ...docsOptions } = options;
const versionEntries = Object.entries(versions);
if (versionEntries.length > 1)
throw 'Multiple Docusuaurus doc versions not allowed in the Wiki';
// Handle version banner.
const versionBannerMap = {};
const docsVersionEntries = versionEntries.map(
([versionLabel, versionOptions]) => {
// TODO: validate banner
const { banner, ...docsVersionOptions } = versionOptions;
versionBannerMap[versionLabel] = banner;
return [versionLabel, docsVersionOptions];
},
);
const validatedDocsOptions = (0, plugin_content_docs_1.validateOptions)({
validate,
options: {
...docsOptions,
versions: Object.fromEntries(docsVersionEntries),
},
});
// Re-add banner.
validatedDocsOptions.versions = Object.fromEntries(
Object.entries(validatedDocsOptions.versions).map(
([versionLabel, versionOptions]) => {
return [
versionLabel,
{
...versionOptions,
banner: versionBannerMap[versionLabel],
},
];
},
),
);
return {
...(0, plugin_content_docs_1.validateOptions)({
validate,
options: docsOptions,
}),
...validatedDocsOptions,
globalSidebars,
bannerPath,
};
};
module.exports = pluginDocs;
}
exports.validateOptions = validateOptions;
17 changes: 0 additions & 17 deletions src/theme/DocBanner/index.tsx

This file was deleted.

7 changes: 0 additions & 7 deletions src/theme/DocBanner/styles.css

This file was deleted.

69 changes: 0 additions & 69 deletions src/theme/DocItem/Layout/index.tsx

This file was deleted.

10 changes: 0 additions & 10 deletions src/theme/DocItem/Layout/styles.module.css

This file was deleted.

Loading

0 comments on commit a5ac39a

Please sign in to comment.