mirror of
https://github.com/facebook/docusaurus.git
synced 2025-06-09 14:22:27 +02:00
refactor: mark toc in loaded MDX as non-optional (#7524)
This commit is contained in:
parent
8b1acb50d1
commit
f443e992b9
11 changed files with 34 additions and 18 deletions
|
@ -21,7 +21,7 @@ export type LoadedMDXContent<FrontMatter, Metadata, Assets = undefined> = {
|
||||||
/** As provided by the content plugin. */
|
/** As provided by the content plugin. */
|
||||||
readonly metadata: Metadata;
|
readonly metadata: Metadata;
|
||||||
/** A list of TOC items (headings). */
|
/** A list of TOC items (headings). */
|
||||||
readonly toc?: readonly TOCItem[];
|
readonly toc: readonly TOCItem[];
|
||||||
/** First h1 title before any content. */
|
/** First h1 title before any content. */
|
||||||
readonly contentTitle: string | undefined;
|
readonly contentTitle: string | undefined;
|
||||||
/**
|
/**
|
||||||
|
|
7
packages/docusaurus-mdx-loader/src/remark/toc/__tests__/__fixtures__/no-heading.md
generated
Normal file
7
packages/docusaurus-mdx-loader/src/remark/toc/__tests__/__fixtures__/no-heading.md
generated
Normal file
|
@ -0,0 +1,7 @@
|
||||||
|
foo
|
||||||
|
|
||||||
|
`bar`
|
||||||
|
|
||||||
|
```js
|
||||||
|
baz
|
||||||
|
```
|
|
@ -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`] = `
|
exports[`toc remark plugin works on non text phrasing content 1`] = `
|
||||||
"export const toc = [
|
"export const toc = [
|
||||||
{
|
{
|
||||||
|
|
|
@ -25,6 +25,11 @@ const processFixture = async (name: string) => {
|
||||||
};
|
};
|
||||||
|
|
||||||
describe('toc remark plugin', () => {
|
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 () => {
|
it('works on non text phrasing content', async () => {
|
||||||
const result = await processFixture('non-text-content');
|
const result = await processFixture('non-text-content');
|
||||||
expect(result).toMatchSnapshot();
|
expect(result).toMatchSnapshot();
|
||||||
|
|
|
@ -89,10 +89,8 @@ export default function plugin(): Transformer {
|
||||||
const {children} = root as Parent<Literal>;
|
const {children} = root as Parent<Literal>;
|
||||||
const targetIndex = getOrCreateExistingTargetIndex(children);
|
const targetIndex = getOrCreateExistingTargetIndex(children);
|
||||||
|
|
||||||
if (headings.length) {
|
|
||||||
children[targetIndex]!.value = `export const ${name} = ${stringifyObject(
|
children[targetIndex]!.value = `export const ${name} = ${stringifyObject(
|
||||||
headings,
|
headings,
|
||||||
)};`;
|
)};`;
|
||||||
}
|
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
|
@ -328,8 +328,6 @@ declare module '@theme/DocSidebar' {
|
||||||
readonly sidebar: readonly PropSidebarItem[];
|
readonly sidebar: readonly PropSidebarItem[];
|
||||||
readonly onCollapse: () => void;
|
readonly onCollapse: () => void;
|
||||||
readonly isHidden: boolean;
|
readonly isHidden: boolean;
|
||||||
// MobileSecondaryFilter expects Record<string, unknown>
|
|
||||||
readonly [key: string]: unknown;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export default function DocSidebar(props: Props): JSX.Element;
|
export default function DocSidebar(props: Props): JSX.Element;
|
||||||
|
|
|
@ -65,9 +65,7 @@ function BlogPostPageContent(props: Props): JSX.Element {
|
||||||
<BlogLayout
|
<BlogLayout
|
||||||
sidebar={sidebar}
|
sidebar={sidebar}
|
||||||
toc={
|
toc={
|
||||||
!hideTableOfContents &&
|
!hideTableOfContents && BlogPostContents.toc.length > 0 ? (
|
||||||
BlogPostContents.toc &&
|
|
||||||
BlogPostContents.toc.length > 0 ? (
|
|
||||||
<TOC
|
<TOC
|
||||||
toc={BlogPostContents.toc}
|
toc={BlogPostContents.toc}
|
||||||
minHeadingLevel={tocMinHeadingLevel}
|
minHeadingLevel={tocMinHeadingLevel}
|
||||||
|
|
|
@ -27,7 +27,7 @@ export default function DocCardList({items, className}: Props): JSX.Element {
|
||||||
<section className={clsx('row', className)}>
|
<section className={clsx('row', className)}>
|
||||||
{filterItems(items).map((item, index) => (
|
{filterItems(items).map((item, index) => (
|
||||||
<article key={index} className="col col--6 margin-bottom--lg">
|
<article key={index} className="col col--6 margin-bottom--lg">
|
||||||
<DocCard key={index} item={item} />
|
<DocCard item={item} />
|
||||||
</article>
|
</article>
|
||||||
))}
|
))}
|
||||||
</section>
|
</section>
|
||||||
|
|
|
@ -55,8 +55,7 @@ function DocItemContent(props: Props): JSX.Element {
|
||||||
|
|
||||||
const windowSize = useWindowSize();
|
const windowSize = useWindowSize();
|
||||||
|
|
||||||
const canRenderTOC =
|
const canRenderTOC = !hideTableOfContents && DocContent.toc.length > 0;
|
||||||
!hideTableOfContents && DocContent.toc && DocContent.toc.length > 0;
|
|
||||||
|
|
||||||
const renderTocDesktop =
|
const renderTocDesktop =
|
||||||
canRenderTOC && (windowSize === 'desktop' || windowSize === 'ssr');
|
canRenderTOC && (windowSize === 'desktop' || windowSize === 'ssr');
|
||||||
|
|
|
@ -42,7 +42,7 @@ export default function MDXPage(props: Props): JSX.Element {
|
||||||
<MDXPageContent />
|
<MDXPageContent />
|
||||||
</MDXContent>
|
</MDXContent>
|
||||||
</div>
|
</div>
|
||||||
{!hideTableOfContents && MDXPageContent.toc && (
|
{!hideTableOfContents && MDXPageContent.toc.length > 0 && (
|
||||||
<div className="col col--2">
|
<div className="col col--2">
|
||||||
<TOC
|
<TOC
|
||||||
toc={MDXPageContent.toc}
|
toc={MDXPageContent.toc}
|
||||||
|
|
|
@ -77,9 +77,7 @@ function ChangelogPageContent(props: Props): JSX.Element {
|
||||||
<BlogLayout
|
<BlogLayout
|
||||||
sidebar={sidebar}
|
sidebar={sidebar}
|
||||||
toc={
|
toc={
|
||||||
!hideTableOfContents &&
|
!hideTableOfContents && BlogPostContents.toc.length > 0 ? (
|
||||||
BlogPostContents.toc &&
|
|
||||||
BlogPostContents.toc.length > 0 ? (
|
|
||||||
<TOC
|
<TOC
|
||||||
toc={BlogPostContents.toc}
|
toc={BlogPostContents.toc}
|
||||||
minHeadingLevel={tocMinHeadingLevel}
|
minHeadingLevel={tocMinHeadingLevel}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue