Skip to content

Commit

Permalink
Use themeConfig for switcher configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
jlvandenhout committed Sep 18, 2023
1 parent 8baff0c commit 4e09366
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 7 deletions.
2 changes: 2 additions & 0 deletions docusaurus.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ const path = require('path');
const { create_doc_plugin, globStatic } = require('./src/utils/config');
const common = require('./common/docusaurus.config');
const contentConfigs = require('./contentPlugins');
const switcherConfig = require('./switcherConfig');
const articleRedirectsFile = require('./articleRedirects');

module.exports = async () => {
Expand All @@ -25,6 +26,7 @@ module.exports = async () => {

const themeConfig = {
themeConfig: {
switcher: switcherConfig,
image: 'img/iota-wiki.png',
navbar: {
hideOnScroll: true,
Expand Down
9 changes: 8 additions & 1 deletion src/utils/useSwitcher.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
import useRouteContext from '@docusaurus/useRouteContext';
import useDocusaurusContext from '@docusaurus/useDocusaurusContext';
import { PropSidebarItem } from '@docusaurus/plugin-content-docs';
import {
GlobalPluginData as DocsGlobalPluginData,
useAllDocsData,
} from '@docusaurus/plugin-content-docs/client';
import { useDocsSidebar } from '@docusaurus/theme-common/internal';
import config from '../switcher.config';
import {
Doc,
MenuItem,
Section,
Sidebar,
Item,
Config,
} from '../common/components/Switcher';

export type GlobalPluginData = DocsGlobalPluginData & {
Expand Down Expand Up @@ -78,12 +79,18 @@ function findSidebarItems(
}

export default function useSwitcher(): SwitcherProps {
const {
siteConfig: { themeConfig },
} = useDocusaurusContext();
const plugins = useAllDocsData() as {
[pluginId: string]: GlobalPluginData;
};
const docId = useRouteContext().plugin.id;
const { name: sidebarId, items: sidebarItems } = useDocsSidebar();

const config = themeConfig.switcher as Config;
if (!config) return { main: sidebarItems };

// Convert the sections and docs configuration into a single
// tree structure of sections, subsections, docs, and versions.
const configTree = config.sections.map((section) => {
Expand Down
14 changes: 8 additions & 6 deletions src/switcher.config.ts → switcherConfig.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import type { Config } from './common/components/Switcher';
import { generateSwitcherConfig } from './utils/pluginConfigGenerators';
// @ts-check

const {
generateSwitcherConfig,
} = require('./src/utils/pluginConfigGenerators');
const {
buildPluginsConfig,
maintainPluginsConfig,
} = require('../versionedConfig');
} = require('./versionedConfig');

const config: Config = {
/** @type {import('./src/common/components/Switcher').Config} */
module.exports = {
docs: [
...generateSwitcherConfig(buildPluginsConfig),
...generateSwitcherConfig(maintainPluginsConfig),
Expand Down Expand Up @@ -50,5 +54,3 @@ const config: Config = {
},
],
};

export default config;

0 comments on commit 4e09366

Please sign in to comment.