Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

beta toc styling #897

Merged
merged 2 commits into from
Jun 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
12 changes: 6 additions & 6 deletions __tests__/transformers/rehype-toc.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ describe('rehype-toc transformer', () => {
const { ast } = module.data.toc;

const expected = h('ul', undefined, [
h('li', undefined, h('p', undefined, h('a', { href: '#title' }, 'Title'))),
h('li', undefined, h('a', { href: '#title' }, 'Title')),
h(
'li',
undefined,
h(
'ul',
undefined,
h('li', undefined, h('p', undefined, h('a', { href: '#subheading' }, 'Subheading'))),
h('li', undefined, h('p', undefined, h('a', { href: '#second-subheading' }, 'Second Subheading'))),
h('li', undefined, h('a', { href: '#subheading' }, 'Subheading')),
h('li', undefined, h('a', { href: '#second-subheading' }, 'Second Subheading')),
),
),
]);
Expand Down Expand Up @@ -68,15 +68,15 @@ describe('rehype-toc transformer', () => {
const { ast } = module.data.toc;

const expected = h('ul', undefined, [
h('li', undefined, h('p', undefined, h('a', { href: '#title' }, 'Title'))),
h('li', undefined, h('a', { href: '#title' }, 'Title')),
h(
'li',
undefined,
h(
'ul',
undefined,
h('li', undefined, h('p', undefined, h('a', { href: '#common-heading' }, 'Common Heading'))),
h('li', undefined, h('p', undefined, h('a', { href: '#subheading' }, 'Subheading'))),
h('li', undefined, h('a', { href: '#common-heading' }, 'Common Heading')),
h('li', undefined, h('a', { href: '#subheading' }, 'Subheading')),
),
),
]);
Expand Down
6 changes: 3 additions & 3 deletions lib/run.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,19 +55,19 @@ const run = async (stringOrFile: string | VFileWithToc, _opts: RunOpts = {}) =>
const { components, terms, variables, baseUrl, ...opts } = _opts;
const vfile = new VFile(stringOrFile) as VFileWithToc;

const exec = (file: VFile | string) =>
const exec = (file: VFile | string, toc = false) =>
mdxRun(file, {
...runtime,
Fragment,
baseUrl: import.meta.url,
imports: { React },
useMDXComponents: makeUseMDXComponents(components),
useMDXComponents: makeUseMDXComponents({ ...components, ...(toc && { p: Fragment }) }),
...opts,
});

const file = await exec(vfile);
const Content = file.default;
const { default: Toc } = 'toc' in vfile.data ? await exec(vfile.data.toc.vfile) : { default: null };
const { default: Toc } = 'toc' in vfile.data ? await exec(vfile.data.toc.vfile, true) : { default: null };

return {
default: () => (
Expand Down
2 changes: 1 addition & 1 deletion processor/transform/rehype-toc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ const rehypeToc = ({ components = {} }: Options): Transformer<Root, Root> => {
}

stack[stack.length - 1].children.push(
h('li', null, h('p', null, h('a', { href: `#${heading.properties.id}` }, heading.children))),
h('li', null, h('a', { href: `#${heading.properties.id}` }, heading.children)),
);
});

Expand Down
Loading