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

feat: mdx TOC #883

Merged
merged 26 commits into from
Jun 3, 2024
Merged
Show file tree
Hide file tree
Changes from 23 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
1 change: 1 addition & 0 deletions .env
kellyjosephprice marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export README_PATH=~/git/readme
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.
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.
4 changes: 2 additions & 2 deletions __tests__/components/CodeTabs.test.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { render } from '@testing-library/react';
import React from 'react';
import { compile, run } from '../../index';
import { execute } from '../helpers';

describe('CodeTabs', () => {
it.skip('render _all_ its children', async () => {
Expand All @@ -12,7 +12,7 @@ assert('theme', 'dark');
assert('theme', 'light');
\`\`\`
`;
const Component = await run(compile(md));
const Component = await execute(md);
const { container } = render(<Component />);

expect(container).toHaveTextContent(`assert('theme', 'dark')`);
Expand Down
4 changes: 2 additions & 2 deletions __tests__/components/Glossary.test.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { render, screen } from '@testing-library/react';
import React from 'react';

import { run, compile } from '../../index';
import { execute } from '../helpers';

describe('Glossary', () => {
it('renders a glossary item', async () => {
const md = `<Glossary>parliament</Glossary>`;
const Content = await run(compile(md));
const Content = await execute(md);
render(<Content />);

expect(screen.getByText('parliament')).toBeVisible();
Expand Down
5 changes: 2 additions & 3 deletions __tests__/components/HTMLBlock.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { renderToStaticMarkup, renderToString } from 'react-dom/server';
import { vi } from 'vitest';

import HTMLBlock from '../../components/HTMLBlock';
import { compile, run } from '../../index';
import { execute } from '../helpers';

describe('HTML Block', () => {
beforeEach(() => {
Expand Down Expand Up @@ -55,8 +55,7 @@ describe('HTML Block', () => {

it('renders the html in a `<pre>` tag if safeMode={true}', async () => {
const md = '<HTMLBlock safeMode={true}>{`<button onload="alert(\'gotcha!\')"/>`}</HTMLBlock>';
const code = compile(md);
const Component = await run(code);
const Component = await execute(md);
expect(renderToStaticMarkup(<Component />)).toMatchInlineSnapshot(
'"<pre class="html-unsafe"><code>&lt;button onload=&quot;alert(&#x27;gotcha!&#x27;)&quot;/&gt;</code></pre>"',
);
Expand Down
37 changes: 0 additions & 37 deletions __tests__/components/Style.test.jsx

This file was deleted.

6 changes: 3 additions & 3 deletions __tests__/components/Variable.test.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { render, screen } from '@testing-library/react';
import React from 'react';

import { run, compile } from '../../index';
import { execute } from '../helpers';

describe('Variable', () => {
it('render a variable', async () => {
const md = `<Variable variable="name" />`;
const Content = await run(compile(md));
const Content = await execute(md);

render(<Content />);

expect(screen.getByText('NAME')).toBeVisible();
Expand Down
127 changes: 7 additions & 120 deletions __tests__/components/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,45 +1,7 @@
import { cleanup, fireEvent, render } from '@testing-library/react';
import React from 'react';
import { vi } from 'vitest';

import { compile, run, utils } from '../../index';

describe.skip('Data Replacements', () => {
it('Variables', () => {
const { container } = render(
React.createElement(
utils.VariablesContext.Provider,
{
value: {
defaults: [{ test: 'Default Value' }],
user: { test: 'User Override' },
},
},
compile('<<test>>'),
),
);
expect(container).toContainHTML('<p><span>User Override</span></p>');
});

it('Glossary Term', () => {
const { container } = render(
React.createElement(
utils.GlossaryContext.Provider,
{
value: [
{
term: 'term',
definition: 'a word or phrase used to describe a thing or to express a concept.',
_id: '1',
},
],
},
compile('<<glossary:term>>'),
),
);
expect(container).toContainHTML('<p><span class="GlossaryItem-trigger">term</span></p>');
});
});
import { execute } from '../helpers';

describe('Components', () => {
it('Callout', async () => {
Expand All @@ -56,16 +18,16 @@ describe('Components', () => {
`,
];

const code0 = compile(callout[0]);
let component = await run(code0);
let md = callout[0];
let component = await execute(md);
let { container } = render(React.createElement(component));

expect(container.innerHTML).toMatchSnapshot();

cleanup();

const code1 = compile(callout[1]);
component = await run(code1);
md = callout[1];
component = await execute(md);
({ container } = render(React.createElement(component)));

expect(container.innerHTML).toMatchInlineSnapshot(
Expand All @@ -75,18 +37,6 @@ describe('Components', () => {
cleanup();
});

it.skip('Multi Code Block', () => {
const tabs = '```\nhello\n```\n```php\nworld\n```\n\n';
const rdmd = compile(tabs);
const { container } = render(rdmd);

expect(container.querySelectorAll('pre')[1]).not.toHaveClass();

fireEvent.click(container.querySelectorAll('.CodeTabs-toolbar button')[1]);

expect(container.querySelectorAll('pre')[1]).toHaveClass('CodeTabs_active');
});

it('Embed', () => {
const fixtures = {
html: `<Embed
Expand Down Expand Up @@ -115,7 +65,7 @@ describe('Components', () => {
};

Object.values(fixtures).map(async fx => {
const component = await run(compile(fx));
const component = await execute(fx);
const { container } = render(React.createElement(component));
return expect(container.innerHTML).toMatchSnapshot();
});
Expand All @@ -125,8 +75,7 @@ describe('Components', () => {
const text =
'![Bro eats pizza and makes an OK gesture.](https://files.readme.io/6f52e22-man-eating-pizza-and-making-an-ok-gesture.jpg "Pizza Face")';

const code = compile(text);
const component = await run(code);
const component = await execute(text);
const { container } = render(React.createElement(component));

expect(container.innerHTML).toMatchSnapshot();
Expand All @@ -149,66 +98,4 @@ describe('Components', () => {
fireEvent.keyDown(img, { key: '.', metaKey: true });
expect(box).not.toHaveClass('open');
});

it.skip('Heading', () => {
let { container } = render(compile('### Heading Level 3\n\n### Heading Level 3'));
expect(container.querySelectorAll('.heading')).toHaveLength(2);

cleanup();

({ container } = render(compile('Pretest.\n\n###\n\nPosttest.')));
expect(container.querySelector('.heading')).toHaveTextContent('');
});

it.skip('Heading no children', () => {
const { container } = render(compile('### Heading Level 3'));
expect(container.querySelectorAll('.heading')).toHaveLength(1);
});
});

describe.skip('Compatibility Mode', () => {
global.eval = vi.fn();
const tabs = `[block:api-header]
{
"title": "I am a magical, mystical heading."
}
[/block]
<div id="custom-target">Loading...</div>
[block:html]
${JSON.stringify({
html: '<script>\nconsole.log("World");\n</script>\n\n<b>Hello!</b>\n\n<script>\nconsole.log("World");\n</script>',
})}
[/block]
[block:parameters]
${JSON.stringify({
data: {
'0-0': "```js Tab Zed\nconsole.log('tab zed');\n```\n```js Tab One\nconsole.log('tab one')\n```",
'0-1':
'> Lorem ipsum dolor sit amet, consectetur adipisicing elit. Maxime repellat placeat expedita voluptatum fugiat rerum, accusamus eius dolorum sequi eveniet esse, adipisci soluta quia mollitia? Dolorem minus, dolores, rerum, pariatur sit quia eum esse voluptatibus ea veritatis non.',
'0-2': '',
'h-0': 'Hello',
'h-1': 'Bonjour',
'h-2': 'Willkommen',
},
cols: 2,
rows: 1,
})}
[/block]`;

let rdmd;
let container;
beforeEach(() => {
rdmd = compile(tabs, { compatibilityMode: true });
// eslint-disable-next-line testing-library/no-render-in-setup
({ container } = render(rdmd));
});

it('Should use h1 tags for magic heading blocks.', () => expect(container.querySelectorAll('h1')).toHaveLength(1));

it('Should allow block-level RDMD compoonents in tables.', () => {
const table = container.querySelector('table');
expect(table.querySelectorAll('.CodeTabs')).toHaveLength(1);
expect(table.querySelectorAll('blockquote')).toHaveLength(1);
});
});
25 changes: 0 additions & 25 deletions __tests__/components/style-attributes.test.js

This file was deleted.

6 changes: 3 additions & 3 deletions __tests__/custom-components/index.test.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { compile, run } from '../../index';
import React from 'react';

import { render, screen } from '@testing-library/react';
import { execute } from '../helpers';

describe('Custom Components', () => {
const Example = () => <div>It works!</div>;
Expand All @@ -16,7 +16,7 @@ describe('Custom Components', () => {
const doc = `
<Example />
`;
const Page = await run(compile(doc), { components: { Example } });
const Page = await execute(doc, undefined, { components: { Example } });
render(<Page />);

expect(screen.getByText('It works!')).toBeVisible();
Expand All @@ -27,7 +27,7 @@ describe('Custom Components', () => {
<Composite />
`;

const Page = await run(compile(doc), { components: { Example, Composite } });
const Page = await execute(doc, undefined, { components: { Example, Composite } });
render(<Page />);

expect(screen.getByText('It works!')).toBeVisible();
Expand Down
8 changes: 6 additions & 2 deletions __tests__/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { vi } from 'vitest';
import { run, compile } from '../index';

const silenceConsole =
export const silenceConsole =
(prop: keyof Console = 'error', impl = () => {}) =>
fn => {
let spy;
Expand All @@ -14,4 +15,7 @@ const silenceConsole =
}
};

export { silenceConsole };
export const execute = async (doc: string, compileOpts = {}, runOpts = {}) => {
const module = await run(compile(doc, compileOpts), runOpts);
return module.default;
};
Loading
Loading