refactor: mark toc in loaded MDX as non-optional (#7524)

This commit is contained in:
Joshua Chen 2022-05-29 22:36:41 +08:00 committed by GitHub
parent 8b1acb50d1
commit f443e992b9
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 34 additions and 18 deletions

View file

@ -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;
/**

View file

@ -0,0 +1,7 @@
foo
`bar`
```js
baz
```

View file

@ -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 = [
{

View file

@ -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();

View file

@ -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,
)};`;
};
}