-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
| [![PR App][icn]][demo] | Fix RM-9722 | | :--------------------: | :---------: | ## 🧰 Changes Coerces readme JSX components to mdast nodes. This is to support the editor, it _should_ just make the editor widgets just work. I say it _should_, but I haven't been able to test it yet. ## 🧬 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 --------- Co-authored-by: Jon Ursenbach <erunion@users.noreply.github.com>
- Loading branch information
1 parent
76da750
commit b742c6c
Showing
16 changed files
with
298 additions
and
95 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { expect } from 'vitest'; | ||
import { map } from 'unist-util-map'; | ||
|
||
import type { ExpectationResult } from '@vitest/expect'; | ||
import { Root, Node } from 'mdast'; | ||
|
||
const removePosition = ({ position, ...node }: Node) => node; | ||
|
||
function toStrictEqualExceptPosition(received: Root, expected: Root): ExpectationResult { | ||
const { equals } = this; | ||
const receivedTrimmed = map(received, removePosition); | ||
const expectedTrimmed = map(expected, removePosition); | ||
|
||
return { | ||
pass: equals(receivedTrimmed, expectedTrimmed), | ||
message: () => 'Expected two trees to be equal!', | ||
actual: receivedTrimmed, | ||
expected: expectedTrimmed, | ||
}; | ||
} | ||
|
||
expect.extend({ toStrictEqualExceptPosition }); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
import { mdast } from '../../index'; | ||
|
||
describe('Readme Components Transformer', () => { | ||
const nodes = [ | ||
{ md: '<Callout />', type: 'rdme-callout' }, | ||
{ md: '<Code />', type: 'code' }, | ||
{ md: '<CodeTabs />', type: 'code-tabs' }, | ||
{ md: '<Image />', type: 'image' }, | ||
{ md: '<Table />', type: 'table' }, | ||
]; | ||
|
||
it.each(nodes)('transforms $md into a(n) $type node', ({ md, type }) => { | ||
const tree = mdast(md); | ||
|
||
expect(tree.children[0].type).toBe(type); | ||
}); | ||
|
||
const docs = { | ||
['rdme-callout']: { | ||
md: `> 📘 It works!`, | ||
mdx: `<Callout icon="📘" heading="It works!" />`, | ||
}, | ||
code: { | ||
md: ` | ||
~~~ | ||
This is a code block | ||
~~~ | ||
`, | ||
mdx: `<Code value="This is a code block" />`, | ||
}, | ||
['code-tabs']: { | ||
md: ` | ||
~~~ | ||
First | ||
~~~ | ||
~~~ | ||
Second | ||
~~~ | ||
`, | ||
mdx: ` | ||
<CodeTabs> | ||
<Code value='First' /> | ||
<Code value='Second' /> | ||
</CodeTabs> | ||
`, | ||
}, | ||
image: { | ||
md: `![](http://placekitten.com/600/200)`, | ||
mdx: `<Image src="http://placekitten.com/600/200" />`, | ||
}, | ||
table: { | ||
md: ` | ||
| h1 | h2 | | ||
| --- | --- | | ||
| a1 | a2 | | ||
`, | ||
// @todo there's text nodes that get inserted between the td's. Pretty sure | ||
// they'd get filtered out by rehype, but lets keep the tests easy. | ||
mdx: ` | ||
<Table> | ||
<tr> | ||
<td>h1</td><td>h2</td> | ||
</tr> | ||
<tr> | ||
<td>a1</td><td>a2</td> | ||
</tr> | ||
</Table> | ||
`, | ||
}, | ||
}; | ||
it.each(Object.entries(docs))('matches the equivalent markdown for %s', (type, { md, mdx }) => { | ||
let mdTree = mdast(md); | ||
const mdxTree = mdast(mdx); | ||
|
||
if (type === 'image') { | ||
// @todo something about these dang paragraphs! | ||
mdTree = { | ||
type: 'root', | ||
children: mdTree.children[0].children, | ||
}; | ||
} | ||
|
||
expect(mdxTree).toStrictEqualExceptPosition(mdTree); | ||
}); | ||
|
||
it('does not convert components that have custom implementations', () => { | ||
const mdx = ` | ||
<Callout heading="Much wow" icon="❗" /> | ||
`; | ||
|
||
const tree = mdast(mdx, { | ||
components: { | ||
Callout: () => null, | ||
}, | ||
}); | ||
|
||
expect(tree.children[0].type).toBe('mdxJsxFlowElement'); | ||
expect(tree.children[0].name).toBe('Callout'); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,8 @@ | ||
import calloutTransformer from './callouts'; | ||
import codeTabsTransfromer from './code-tabs'; | ||
import gemojiTransformer from './gemoji+'; | ||
import readmeComponentsTransformer from './readme-components'; | ||
|
||
export { readmeComponentsTransformer }; | ||
|
||
export default [calloutTransformer, codeTabsTransfromer, gemojiTransformer]; |
Oops, something went wrong.