Skip to content

Commit

Permalink
refactor: mark toc in loaded MDX as non-optional
Browse files Browse the repository at this point in the history
  • Loading branch information
Josh-Cena committed May 29, 2022
1 parent 8b1acb5 commit 0ccb493
Show file tree
Hide file tree
Showing 11 changed files with 34 additions and 18 deletions.
2 changes: 1 addition & 1 deletion packages/docusaurus-mdx-loader/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export type LoadedMDXContent<FrontMatter, Metadata, Assets = undefined> = {
/** As provided by the content plugin. */
readonly metadata: Metadata;
/** A list of TOC items (headings). */
readonly toc?: readonly TOCItem[];
readonly toc: readonly TOCItem[];
/** First h1 title before any content. */
readonly contentTitle: string | undefined;
/**
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,19 @@ Content.
"
`;

exports[`toc remark plugin outputs empty array for no TOC 1`] = `
"export const toc = [];
foo
\`bar\`
\`\`\`js
baz
\`\`\`
"
`;

exports[`toc remark plugin works on non text phrasing content 1`] = `
"export const toc = [
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,11 @@ const processFixture = async (name: string) => {
};

describe('toc remark plugin', () => {
it('outputs empty array for no TOC', async () => {
const result = await processFixture('no-heading');
expect(result).toMatchSnapshot();
});

it('works on non text phrasing content', async () => {
const result = await processFixture('non-text-content');
expect(result).toMatchSnapshot();
Expand Down
8 changes: 3 additions & 5 deletions packages/docusaurus-mdx-loader/src/remark/toc/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,10 +89,8 @@ export default function plugin(): Transformer {
const {children} = root as Parent<Literal>;
const targetIndex = getOrCreateExistingTargetIndex(children);

if (headings.length) {
children[targetIndex]!.value = `export const ${name} = ${stringifyObject(
headings,
)};`;
}
children[targetIndex]!.value = `export const ${name} = ${stringifyObject(
headings,
)};`;
};
}
2 changes: 0 additions & 2 deletions packages/docusaurus-theme-classic/src/theme-classic.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -328,8 +328,6 @@ declare module '@theme/DocSidebar' {
readonly sidebar: readonly PropSidebarItem[];
readonly onCollapse: () => void;
readonly isHidden: boolean;
// MobileSecondaryFilter expects Record<string, unknown>
readonly [key: string]: unknown;
}

export default function DocSidebar(props: Props): JSX.Element;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,9 +65,7 @@ function BlogPostPageContent(props: Props): JSX.Element {
<BlogLayout
sidebar={sidebar}
toc={
!hideTableOfContents &&
BlogPostContents.toc &&
BlogPostContents.toc.length > 0 ? (
!hideTableOfContents && BlogPostContents.toc.length > 0 ? (
<TOC
toc={BlogPostContents.toc}
minHeadingLevel={tocMinHeadingLevel}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ export default function DocCardList({items, className}: Props): JSX.Element {
<section className={clsx('row', className)}>
{filterItems(items).map((item, index) => (
<article key={index} className="col col--6 margin-bottom--lg">
<DocCard key={index} item={item} />
<DocCard item={item} />
</article>
))}
</section>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,7 @@ function DocItemContent(props: Props): JSX.Element {

const windowSize = useWindowSize();

const canRenderTOC =
!hideTableOfContents && DocContent.toc && DocContent.toc.length > 0;
const canRenderTOC = !hideTableOfContents && DocContent.toc.length > 0;

const renderTocDesktop =
canRenderTOC && (windowSize === 'desktop' || windowSize === 'ssr');
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ export default function MDXPage(props: Props): JSX.Element {
<MDXPageContent />
</MDXContent>
</div>
{!hideTableOfContents && MDXPageContent.toc && (
{!hideTableOfContents && MDXPageContent.toc.length > 0 && (
<div className="col col--2">
<TOC
toc={MDXPageContent.toc}
Expand Down
4 changes: 1 addition & 3 deletions website/src/plugins/changelog/theme/ChangelogPage/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -77,9 +77,7 @@ function ChangelogPageContent(props: Props): JSX.Element {
<BlogLayout
sidebar={sidebar}
toc={
!hideTableOfContents &&
BlogPostContents.toc &&
BlogPostContents.toc.length > 0 ? (
!hideTableOfContents && BlogPostContents.toc.length > 0 ? (
<TOC
toc={BlogPostContents.toc}
minHeadingLevel={tocMinHeadingLevel}
Expand Down

0 comments on commit 0ccb493

Please sign in to comment.