-
Couldn't load subscription status.
- Fork 290
docs: explain swizzling DocCard #1207
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
sserrata
merged 1 commit into
PaloAltoNetworks:main
from
sserrata:codex/add-documentation-for-swizzling-doccard
Jul 14, 2025
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
165 changes: 165 additions & 0 deletions
demo/docs/customization/doccard.mdx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,165 @@ | ||
| --- | ||
| id: doccard | ||
| hide_title: true | ||
| sidebar_label: DocCard Markdown | ||
| title: DocCard Markdown | ||
| description: Swizzle the DocCard component to render Markdown in card descriptions. | ||
| --- | ||
|
|
||
| ## Overview | ||
|
|
||
| The default DocCard component used by Docusaurus displays the `description` field | ||
| as plain text. Any Markdown inside the description is escaped rather than | ||
| rendered. To support Markdown, you can swizzle the component and update it to use | ||
| `@theme/Markdown`. | ||
|
|
||
| ## Swizzling the component | ||
|
|
||
| Run the swizzle command from your site directory and choose the **eject** option | ||
| when prompted: | ||
|
|
||
| ```bash | ||
| npx docusaurus swizzle @docusaurus/theme-classic DocCard | ||
| ``` | ||
|
|
||
| This will create `src/theme/DocCard/index.tsx` in your project. Replace its | ||
| contents with the following: | ||
|
|
||
| ```tsx title="src/theme/DocCard/index.tsx" | ||
| import React, { type ReactNode } from "react"; | ||
|
|
||
| import isInternalUrl from "@docusaurus/isInternalUrl"; | ||
| import Link from "@docusaurus/Link"; | ||
| import type { | ||
| PropSidebarItemCategory, | ||
| PropSidebarItemLink, | ||
| } from "@docusaurus/plugin-content-docs"; | ||
| import { | ||
| useDocById, | ||
| findFirstSidebarItemLink, | ||
| } from "@docusaurus/plugin-content-docs/lib/client/docsUtils"; | ||
| import { usePluralForm } from "@docusaurus/theme-common"; | ||
| import { translate } from "@docusaurus/Translate"; | ||
| import type { Props } from "@theme/DocCard"; | ||
| import Heading from "@theme/Heading"; | ||
| import Markdown from "@theme/Markdown"; | ||
| import clsx from "clsx"; | ||
|
|
||
| import styles from "./styles.module.css"; | ||
|
|
||
| function useCategoryItemsPlural() { | ||
| const { selectMessage } = usePluralForm(); | ||
| return (count: number) => | ||
| selectMessage( | ||
| count, | ||
| translate( | ||
| { | ||
| message: "1 item|{count} items", | ||
| id: "theme.docs.DocCard.categoryDescription.plurals", | ||
| description: | ||
| "The default description for a category card in the generated index about how many items this category includes", | ||
| }, | ||
| { count } | ||
| ) | ||
| ); | ||
| } | ||
|
|
||
| function CardContainer({ | ||
| className, | ||
| href, | ||
| children, | ||
| }: { | ||
| className?: string; | ||
| href: string; | ||
| children: ReactNode; | ||
| }): ReactNode { | ||
| return ( | ||
| <Link | ||
| href={href} | ||
| className={clsx("card padding--lg", styles.cardContainer, className)} | ||
| > | ||
| {children} | ||
| </Link> | ||
| ); | ||
| } | ||
|
|
||
| function CardLayout({ | ||
| className, | ||
| href, | ||
| icon, | ||
| title, | ||
| description, | ||
| }: { | ||
| className?: string; | ||
| href: string; | ||
| icon: ReactNode; | ||
| title: string; | ||
| description?: string; | ||
| }): ReactNode { | ||
| return ( | ||
| <CardContainer href={href} className={className}> | ||
| <Heading | ||
| as="h2" | ||
| className={clsx("text--truncate", styles.cardTitle)} | ||
| title={title} | ||
| > | ||
| {icon} {title} | ||
| </Heading> | ||
| {description && ( | ||
| <Markdown | ||
| className={clsx("text--truncate", styles.cardDescription)} | ||
| title={description} | ||
| > | ||
| {description} | ||
| </Markdown> | ||
| )} | ||
| </CardContainer> | ||
| ); | ||
| } | ||
|
|
||
| function CardCategory({ item }: { item: PropSidebarItemCategory }): ReactNode { | ||
| const href = findFirstSidebarItemLink(item); | ||
| const categoryItemsPlural = useCategoryItemsPlural(); | ||
|
|
||
| // Unexpected: categories that don't have a link have been filtered upfront | ||
| if (!href) { | ||
| return null; | ||
| } | ||
|
|
||
| return ( | ||
| <CardLayout | ||
| className={item.className} | ||
| href={href} | ||
| icon="🗃️" | ||
| title={item.label} | ||
| description={item.description ?? categoryItemsPlural(item.items.length)} | ||
| /> | ||
| ); | ||
| } | ||
|
|
||
| function CardLink({ item }: { item: PropSidebarItemLink }): ReactNode { | ||
| const icon = isInternalUrl(item.href) ? "📄️" : "🔗"; | ||
| const doc = useDocById(item.docId ?? undefined); | ||
| return ( | ||
| <CardLayout | ||
| className={item.className} | ||
| href={item.href} | ||
| icon={icon} | ||
| title={item.label} | ||
| description={item.description ?? doc?.description} | ||
| /> | ||
| ); | ||
| } | ||
|
|
||
| export default function DocCard({ item }: Props): ReactNode { | ||
| switch (item.type) { | ||
| case "link": | ||
| return <CardLink item={item} />; | ||
| case "category": | ||
| return <CardCategory item={item} />; | ||
| default: | ||
| throw new Error(`unknown item type ${JSON.stringify(item)}`); | ||
| } | ||
| } | ||
| ``` | ||
|
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.