Skip to content

Commit

Permalink
mark DocCardList as safe to swizzle
Browse files Browse the repository at this point in the history
  • Loading branch information
slorber committed Aug 25, 2022
1 parent 4044d00 commit 7aed44e
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 15 deletions.
8 changes: 8 additions & 0 deletions packages/docusaurus-theme-classic/src/getSwizzleConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,14 @@ export default function getSwizzleConfig(): SwizzleConfig {
description:
'The color mode toggle to switch between light and dark mode.',
},
DocCardList: {
actions: {
eject: 'safe',
wrap: 'safe',
},
description:
'The component responsible for rendering a list of sidebar items cards.\nNotable used on the category generated-index pages.',
},
DocSidebar: {
actions: {
eject: 'unsafe', // Too much technical code in sidebar, not very safe atm
Expand Down
20 changes: 6 additions & 14 deletions packages/docusaurus-theme-classic/src/theme/DocCardList/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,21 +7,12 @@

import React from 'react';
import clsx from 'clsx';
import {findFirstCategoryLink} from '@docusaurus/theme-common/internal';
import {useCurrentSidebarCategory} from '@docusaurus/theme-common';
import {
useCurrentSidebarCategory,
filterDocCardListItems,
} from '@docusaurus/theme-common';
import DocCard from '@theme/DocCard';
import type {Props} from '@theme/DocCardList';
import type {PropSidebarItem} from '@docusaurus/plugin-content-docs';

// Filter categories that don't have a link.
function filterItems(items: PropSidebarItem[]): PropSidebarItem[] {
return items.filter((item) => {
if (item.type === 'category') {
return !!findFirstCategoryLink(item);
}
return true;
});
}

function DocCardListForCurrentSidebarCategory({className}: Props) {
const category = useCurrentSidebarCategory();
Expand All @@ -33,9 +24,10 @@ export default function DocCardList(props: Props): JSX.Element {
if (!items) {
return <DocCardListForCurrentSidebarCategory {...props} />;
}
const filteredItems = filterDocCardListItems(items);
return (
<section className={clsx('row', className)}>
{filterItems(items).map((item, index) => (
{filteredItems.map((item, index) => (
<article key={index} className="col col--6 margin-bottom--lg">
<DocCard item={item} />
</article>
Expand Down
5 changes: 4 additions & 1 deletion packages/docusaurus-theme-common/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,10 @@ export {createStorageSlot, listStorageKeys} from './utils/storageUtils';

export {useContextualSearchFilters} from './utils/searchUtils';

export {useCurrentSidebarCategory} from './utils/docsUtils';
export {
useCurrentSidebarCategory,
filterDocCardListItems,
} from './utils/docsUtils';

export {usePluralForm} from './utils/usePluralForm';

Expand Down
15 changes: 15 additions & 0 deletions packages/docusaurus-theme-common/src/utils/docsUtils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -361,3 +361,18 @@ export function useDocRootMetadata({route}: DocRootProps): null | {
sidebarItems,
};
}

/**
* Filter categories that don't have a link.
* @param items
*/
export function filterDocCardListItems(
items: PropSidebarItem[],
): PropSidebarItem[] {
return items.filter((item) => {
if (item.type === 'category') {
return !!findFirstCategoryLink(item);
}
return true;
});
}

0 comments on commit 7aed44e

Please sign in to comment.