Skip to content

Commit

Permalink
fix: toc
Browse files Browse the repository at this point in the history
  • Loading branch information
kellyjosephprice committed Feb 25, 2025
1 parent 1276e7c commit 2486f69
Show file tree
Hide file tree
Showing 5 changed files with 40 additions and 28 deletions.
1 change: 1 addition & 0 deletions __tests__/browser/markdown.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ describe('visual regression tests', () => {
const docs = [
'callouts',
'calloutTests',
'childTests',
'codeBlocks',
// skipping this because they sporadically failure with network timing
// issues
Expand Down
54 changes: 34 additions & 20 deletions __tests__/custom-components/index.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,32 +3,30 @@ import type { RMDXModule } from '../../types';
import { render, screen } from '@testing-library/react';
import React from 'react';

import { execute } from '../helpers';
import { compile, run } from '../../lib';
import { RMDXModule } from '../../types';
import { execute } from '../helpers';

describe('Custom Components', () => {
let Example;
let Multiple;

beforeEach(async () => {
Example = await execute('It works!', {}, {}, { getDefault: false });
Multiple = await execute(
`
const Example = 'It works!';
const Multiple = `
export const First = () => <div>First</div>;
export const Second = () => <div>Second</div>;
`,
{},
{},
{ getDefault: false },
);
});
`;
const Nesting = `
export const WithChildren = ({ children }) => <div>{children}</div>;
<div>{props.children}</div>
`;

it('renders custom components', async () => {
const doc = `
<Example />
`;
const Page = (await execute(doc, undefined, { components: { Example } })) as RMDXModule['default'];
const Page = (await execute(
doc,
{ components: { Example } },
{ components: { Example } },
)) as RMDXModule['default'];
render(<Page />);

expect(screen.getByText('It works!')).toBeVisible();
Expand All @@ -40,17 +38,33 @@ export const Second = () => <div>Second</div>;
<Second />
`;
const Page = (await execute(doc, undefined, { components: { Multiple } })) as RMDXModule['default'];
const Page = (await execute(
doc,
{ components: { Multiple } },
{ components: { Multiple } },
)) as RMDXModule['default'];
render(<Page />);

expect(screen.getByText('First')).toBeVisible();
expect(screen.getByText('Second')).toBeVisible();
});

it('renders a nested exported custom component', async () => {
const doc = '<Nesting><WithChildren>Hello, Test User!</WithChildren></Nesting>';
const Page = (await execute(
doc,
{ components: { Nesting } },
{ components: { Nesting } },
)) as RMDXModule['default'];
render(<Page />);

expect(screen.getByText('Hello, Test User!')).toBeVisible();
});

it('renders the default export of a custom component and passes through props', async () => {
const Test = (await run(await compile(`{props.attr}`))) as RMDXModule;
const doc = `<Test attr="Hello" />`;
const Page = await run(await compile(doc), { components: { Test } });
const Test = '{props.attr}';
const doc = '<Test attr="Hello" />';
const Page = await run(await compile(doc, { components: { Test } }), { components: { Test } });
render(<Page.default />);

expect(screen.getByText('Hello')).toBeVisible();
Expand Down
2 changes: 1 addition & 1 deletion example/components.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export const Test = ({ color = 'thistle' } = {}) => {
export default Test;
`,
MultipleExports: `
export const One = () => <div>"One"</div>;
export const One = () => "One";
export const Two = () => "Two";
`,
Expand Down
5 changes: 1 addition & 4 deletions lib/run.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,10 @@ const run = async (string: string, _opts: RunOpts = {}) => {
const { Fragment } = runtime;
const { components = {}, terms, variables, baseUrl, imports = {}, ...opts } = _opts;

const tocsByTag = {};
const tocsByTag: Record<string, RMDXModule['toc']> = {};
const promises: [string, RMDXModule][] = await Promise.all(
Object.entries(components).map(async ([tag, body]) => {
const code = await compile(body);
console.log(code);
const mod = await run(code);
return [tag, mod];
}),
Expand All @@ -81,8 +80,6 @@ const run = async (string: string, _opts: RunOpts = {}) => {
return memo;
}, {});

console.log(string);

const exec = (text: string, { useMDXComponents = makeUseMDXComponents(exportedComponents) }: RunOpts = {}) => {
return mdxRun(text, {
...runtime,
Expand Down
6 changes: 3 additions & 3 deletions processor/plugin/toc.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { CustomComponents, HastHeading, IndexableElements, TocList, TocListItem } from '../../types';
import type { CustomComponents, HastHeading, IndexableElements, RMDXModule, TocList, TocListItem } from '../../types';
import type { Root } from 'hast';
import type { MdxjsEsm } from 'mdast-util-mdxjs-esm';
import type { Transformer } from 'unified';
Expand Down Expand Up @@ -89,11 +89,11 @@ const tocToHast = (headings: HastHeading[] = []): TocList => {
return ast;
};

export const tocHastToMdx = (toc: IndexableElements[], components: CustomComponents) => {
export const tocHastToMdx = (toc: IndexableElements[], components: Record<string, RMDXModule['toc']>) => {
const tree: Root = { type: 'root', children: toc };

visit(tree, 'mdxJsxFlowElement', (node, index, parent) => {
const subToc = components[node.name].toc || [];
const subToc = components[node.name] || [];
parent.children.splice(index, 1, ...subToc);
});

Expand Down

0 comments on commit 2486f69

Please sign in to comment.