-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adapt DocCardList for the Teleport docs site
Partially addresses #29 Swizzle the Docusaurus-native `DocCardList` component and adapt it to suit the Teleport docs site. Since we prefer a more text-oriented approach to components, edit `DocCardList` to return a plain `ul`, rather than tiles. Since the `DocCardList` component is a Docusaurus-native alternative to our `remark-toc` plugin, edit `remark-toc` to return a `DocCardList` instead of querying the filesystem to generate a table of contents. Once we replace all `(!toc!)` expressions with `<DocCardList />` elements, we can remove `remark-toc` entirely. Also make `DocCardList` an MDX component so docs authors don't need to add `import` statements to the top of a docs page.
- Loading branch information
Showing
3 changed files
with
77 additions
and
77 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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,68 @@ | ||
import React from "react"; | ||
import clsx from "clsx"; | ||
import { | ||
useCurrentSidebarCategory, | ||
filterDocCardListItems, | ||
useDocById, | ||
} from "@docusaurus/plugin-content-docs/client"; | ||
import DocCard from "@theme/DocCard"; | ||
import type { Props } from "@theme/DocCardList"; | ||
|
||
function DocCardListForCurrentSidebarCategory({ className }: Props) { | ||
const category = useCurrentSidebarCategory(); | ||
return <DocCardList items={category.items} className={className} />; | ||
} | ||
|
||
// categoryHrefoToDocID returns the Docusaurus page ID that corresponds to the | ||
// given href. Category pages do not have IDs in the items prop, so we generate | ||
// a page ID based on the assumption that category page slugs are the same as | ||
// their containing directory names. | ||
function categoryHrefToDocID(href: string): string { | ||
if (!href) { | ||
return href; | ||
} | ||
const idPrefix = href.replace(new RegExp(`^/ver/[0-9]+\\.x/`), ""); | ||
const slugRE = new RegExp(`/([^/]+)/$`); | ||
const slug = slugRE.exec(href); | ||
if (!slug || slug.length != 2) { | ||
return ""; | ||
} | ||
return idPrefix + slug[1]; | ||
} | ||
|
||
export default function DocCardList(props: Props): JSX.Element { | ||
const { items, className } = props; | ||
if (!items) { | ||
return <DocCardListForCurrentSidebarCategory {...props} />; | ||
} | ||
const filteredItems = filterDocCardListItems(items).map((item) => { | ||
const doc = useDocById(item.docId ?? undefined); | ||
|
||
if (item.type == "link") { | ||
return { | ||
href: item.href, | ||
label: item.label, | ||
description: doc?.description, | ||
}; | ||
} | ||
if (item.type == "category") { | ||
const indexPage = useDocById(categoryHrefToDocID(item.href) ?? undefined); | ||
|
||
return { | ||
href: item.href, | ||
label: item.label + " (section)", | ||
description: indexPage?.description, | ||
}; | ||
} | ||
}); | ||
|
||
return ( | ||
<ul className={clsx("row", className)}> | ||
{filteredItems.map((item, index) => ( | ||
<li key={index}> | ||
<a href={item.href}>{item.label}</a>: {item.description} | ||
</li> | ||
))} | ||
</ul> | ||
); | ||
} |
This file contains 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