Skip to content

Commit

Permalink
Refactor breadcrumbs to use plugin-based config instead of theme, hou…
Browse files Browse the repository at this point in the history
…se logic in hook
  • Loading branch information
jodyheavener committed Feb 11, 2022
1 parent b1a76d5 commit bc5287d
Show file tree
Hide file tree
Showing 10 changed files with 40 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,7 @@ exports[`simple website content 5`] = `
Object {
"pluginName": Object {
"pluginId": Object {
"breadcrumbs": "nested",
"path": "/docs",
"versions": Array [
Object {
Expand Down Expand Up @@ -955,6 +956,7 @@ exports[`simple website content: global data 1`] = `
Object {
"pluginName": Object {
"pluginId": Object {
"breadcrumbs": "nested",
"path": "/docs",
"versions": Array [
Object {
Expand Down Expand Up @@ -2411,6 +2413,7 @@ exports[`versioned website (community) content: global data 1`] = `
Object {
"pluginName": Object {
"pluginId": Object {
"breadcrumbs": "nested",
"path": "/community",
"versions": Array [
Object {
Expand Down Expand Up @@ -3450,6 +3453,7 @@ exports[`versioned website content: global data 1`] = `
Object {
"pluginName": Object {
"pluginId": Object {
"breadcrumbs": "nested",
"path": "/docs",
"versions": Array [
Object {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ describe('normalizeDocsPluginOptions', () => {
rehypePlugins: [markdownPluginsFunctionStub],
beforeDefaultRehypePlugins: [],
beforeDefaultRemarkPlugins: [],
breadcrumbs: 'nested',
showLastUpdateTime: true,
showLastUpdateAuthor: true,
admonitions: {},
Expand Down
2 changes: 2 additions & 0 deletions packages/docusaurus-plugin-content-docs/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,7 @@ export default async function pluginContentDocs(
docLayoutComponent,
docItemComponent,
docCategoryGeneratedIndexComponent,
breadcrumbs,
} = options;
const {addRoute, createData, setGlobalData} = actions;

Expand Down Expand Up @@ -295,6 +296,7 @@ export default async function pluginContentDocs(
setGlobalData<GlobalPluginData>({
path: normalizeUrl([baseUrl, options.routeBasePath]),
versions: loadedVersions.map(toGlobalDataVersion),
breadcrumbs,
});
},

Expand Down
5 changes: 5 additions & 0 deletions packages/docusaurus-plugin-content-docs/src/options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ export const DEFAULT_OPTIONS: Omit<PluginOptions, 'id' | 'sidebarPath'> = {
editLocalizedFiles: false,
sidebarCollapsible: true,
sidebarCollapsed: true,
breadcrumbs: 'nested',
};

const VersionOptionsSchema = Joi.object({
Expand Down Expand Up @@ -139,6 +140,10 @@ export const OptionsSchema = Joi.object({
disableVersioning: Joi.bool().default(DEFAULT_OPTIONS.disableVersioning),
lastVersion: Joi.string().optional(),
versions: VersionsOptionsSchema,
breadcrumbs: Joi.alternatives(
Joi.bool(),
Joi.string().valid('nested'),
).default(DEFAULT_OPTIONS.breadcrumbs),
});

export function validateOptions({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ declare module '@docusaurus/plugin-content-docs' {
showLastUpdateTime?: boolean;
showLastUpdateAuthor?: boolean;
numberPrefixParser: NumberPrefixParser;
breadcrumbs: boolean | 'nested';
};

export type PathOptions = {
Expand Down Expand Up @@ -298,6 +299,7 @@ declare module '@docusaurus/plugin-content-docs/client' {
export type GlobalPluginData = {
path: string;
versions: GlobalVersion[];
breadcrumbs: boolean | 'nested';
};
export type DocVersionSuggestions = {
// suggest the latest version
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -701,14 +701,4 @@ describe('themeConfig tableOfContents', () => {
`"\\"tableOfContents.minHeadingLevel\\" must be less than or equal to ref:maxHeadingLevel"`,
);
});

test('should accept nested breadcrumb config', () => {
const breadcrumbConfig = {
breadcrumbs: 'nested',
};
expect(testValidateThemeConfig(breadcrumbConfig)).toEqual({
...DEFAULT_CONFIG,
...breadcrumbConfig,
});
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -6,36 +6,14 @@
*/

import React from 'react';
import {
isSamePath,
ThemeClassNames,
useSidebarBreadcrumbs,
useThemeConfig,
} from '@docusaurus/theme-common';
import {ThemeClassNames, useSidebarBreadcrumbs} from '@docusaurus/theme-common';
import styles from './styles.module.css';
import clsx from 'clsx';
import {useLocation} from '@docusaurus/router';
import type {PropSidebar} from '@docusaurus/plugin-content-docs';

export default function DocBreadcrumbs(): JSX.Element | null {
const {pathname} = useLocation();
const breadcrumbs = useSidebarBreadcrumbs();
const {breadcrumbs: enabled} = useThemeConfig();

function isExact(items: PropSidebar) {
const singleItem = items[0];
return (
items.length === 1 &&
singleItem.type === 'link' &&
isSamePath(singleItem.href, pathname)
);
}

if (
!breadcrumbs.length ||
enabled === false ||
(enabled === 'nested' && isExact(breadcrumbs))
) {
if (!breadcrumbs) {
return null;
}

Expand Down
5 changes: 0 additions & 5 deletions packages/docusaurus-theme-classic/src/validateThemeConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,6 @@ const DEFAULT_CONFIG = {
minHeadingLevel: 2,
maxHeadingLevel: 3,
},
breadcrumbs: 'nested',
};

const NavbarItemPosition = Joi.string().equal('left', 'right').default('left');
Expand Down Expand Up @@ -385,10 +384,6 @@ const ThemeConfigSchema = Joi.object({
.max(6)
.default(DEFAULT_CONFIG.tableOfContents.maxHeadingLevel),
}).default(DEFAULT_CONFIG.tableOfContents),
breadcrumbs: Joi.alternatives(
Joi.bool(),
Joi.string().valid('nested'),
).default(DEFAULT_CONFIG.breadcrumbs),
});

export {DEFAULT_CONFIG, ThemeConfigSchema};
Expand Down
27 changes: 24 additions & 3 deletions packages/docusaurus-theme-common/src/utils/docsUtils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,10 @@
*/

import React, {createContext, type ReactNode, useContext} from 'react';
import {useAllDocsData} from '@docusaurus/plugin-content-docs/client';
import {
useActivePlugin,
useAllDocsData,
} from '@docusaurus/plugin-content-docs/client';
import type {
PropSidebar,
PropSidebarItem,
Expand Down Expand Up @@ -182,13 +185,13 @@ export function isActiveSidebarItem(
return false;
}

export function useSidebarBreadcrumbs(): PropSidebar {
export function useSidebarBreadcrumbs(): PropSidebar | null {
const sidebar = useDocsSidebar();
const {pathname} = useLocation();
const breadcrumbs: PropSidebar = [];
const enabled = useActivePlugin()?.pluginData?.breadcrumbs;

function extract(items: PropSidebar) {
// eslint-disable-next-line no-restricted-syntax
for (const item of items) {
if (item.type === 'category' && extract(item.items)) {
breadcrumbs.push(item);
Expand All @@ -202,9 +205,27 @@ export function useSidebarBreadcrumbs(): PropSidebar {
return false;
}

// Check if the only breadcrumb item
// is a link to the current page
function onlyPageLink() {
return (
breadcrumbs.length === 1 &&
breadcrumbs[0].type === 'link' &&
isSamePath(breadcrumbs[0].href, pathname)
);
}

if (sidebar) {
extract(sidebar);
}

if (
!breadcrumbs.length ||
enabled === false ||
(enabled === 'nested' && onlyPageLink())
) {
return null;
}

return breadcrumbs.reverse();
}
3 changes: 0 additions & 3 deletions packages/docusaurus-theme-common/src/utils/useThemeConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,6 @@ export type TableOfContents = {
maxHeadingLevel: number;
};

export type Breadcrumbs = boolean | 'nested';

// Theme config after validation/normalization
export type ThemeConfig = {
docs: {
Expand All @@ -129,7 +127,6 @@ export type ThemeConfig = {
metadata: Array<Record<string, string>>;
sidebarCollapsible: boolean;
tableOfContents: TableOfContents;
breadcrumbs: Breadcrumbs;
};

// User-provided theme config, unnormalized
Expand Down

0 comments on commit bc5287d

Please sign in to comment.