Skip to content

Commit

Permalink
beta toc styling (#897)
Browse files Browse the repository at this point in the history
| [![PR App][icn]][demo] | RM-9179 |
| :--------------------: | :-----: |

## 🧰 Changes

Removes `p` tags from the TOC

## 🧬 QA & Testing

- [Broken on production][prod].
- [Working in this PR app][demo].

[demo]: https://markdown-pr-PR_NUMBER.herokuapp.com
[prod]: https://SUBDOMAIN.readme.io
[icn]:
https://user-images.githubusercontent.com/886627/160426047-1bee9488-305a-4145-bb2b-09d8b757d38a.svg
  • Loading branch information
kellyjosephprice authored Jun 4, 2024
1 parent 2f77967 commit 0d5cd53
Show file tree
Hide file tree
Showing 9 changed files with 10 additions and 10 deletions.
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

0 comments on commit 0d5cd53

Please sign in to comment.