-
Notifications
You must be signed in to change notification settings - Fork 15
feat: jsx-coercion #876
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
Merged
Merged
feat: jsx-coercion #876
Changes from 6 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
05a23ad
wip
kellyjosephprice 8ec73d7
wip
kellyjosephprice 6cc622a
fix test
kellyjosephprice 4ee8964
wat
kellyjosephprice 84a8347
fix code highlighting
kellyjosephprice 361862c
fix rdme transformer
kellyjosephprice ec4d710
Update __tests__/matchers.ts
kellyjosephprice 05cc6c1
oops...I did it again
kellyjosephprice 2ac2967
ignore custom components
kellyjosephprice File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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,19 @@ | ||
| import { expect } from 'vitest'; | ||
| import { map } from 'unist-util-map'; | ||
|
|
||
| const removePosition = ({ position, ...node }) => node; | ||
|
|
||
| function toStrictEqualExceptPosition(received, expected) { | ||
| 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 hidden or 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 hidden or 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,85 @@ | ||
| 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: ``, | ||
| 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); | ||
| }); | ||
| }); |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,5 @@ | ||
| { | ||
| "name": "@readme/markdown", | ||
| "name": "@readme/mdx", | ||
| "description": "ReadMe's React-based Markdown parser", | ||
| "author": "Rafe Goldberg <[email protected]>", | ||
| "version": "6.75.0-beta.30", | ||
|
|
@@ -111,6 +111,7 @@ | |
| "terser-webpack-plugin": "^5.3.7", | ||
| "ts-loader": "^9.4.2", | ||
| "typescript": "^5.4.5", | ||
| "unist-util-map": "^4.0.0", | ||
| "vitest": "^1.4.0", | ||
| "webpack": "^5.56.0", | ||
| "webpack-cli": "^5.0.1", | ||
|
|
||
This file contains hidden or 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 hidden or 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 hidden or 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]; |
This file contains hidden or 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,97 @@ | ||
| import { BlockContent, Paragraph, Root, TableRow } from 'mdast'; | ||
| import { MdxJsxFlowElement } from 'mdast-util-mdx'; | ||
| import { visit } from 'unist-util-visit'; | ||
|
|
||
| const types = { | ||
| Callout: 'rdme-callout', | ||
| Code: 'code', | ||
| CodeTabs: 'code-tabs', | ||
| Image: 'image', | ||
| Table: 'table', | ||
| tr: 'tableRow', | ||
| td: 'tableCell', | ||
| }; | ||
|
|
||
| const attributes = (jsx: MdxJsxFlowElement) => | ||
| jsx.attributes.reduce((memo, attr) => { | ||
| memo[attr.name] = attr.value; | ||
| return memo; | ||
| }, {}); | ||
|
|
||
| const readmeComponents = | ||
| ({ components }) => | ||
| (tree: Root) => { | ||
| visit(tree, ['mdxJsxFlowElement', 'mdxJsxTextElement'], (node, index, parent) => { | ||
| if (node.name === 'Code') { | ||
| const { position } = node; | ||
| const { value, lang = null, meta = null } = attributes(node); | ||
|
|
||
| const mdNode = { | ||
| lang, | ||
| meta, | ||
| position, | ||
| type: 'code', | ||
| value, | ||
| data: { | ||
| hProperties: { value, lang, meta }, | ||
| }, | ||
| }; | ||
|
|
||
| parent.children[index] = mdNode; | ||
| } else if (node.name === 'Image') { | ||
| const { position } = node; | ||
| const { alt = '', src, title = null } = attributes(node); | ||
|
|
||
| const mdNode = { | ||
| alt, | ||
| position, | ||
| title, | ||
| type: 'image', | ||
| url: src, | ||
| }; | ||
|
|
||
| parent.children[index] = mdNode; | ||
| } else if (node.name === 'Table') { | ||
| const { children, position } = node; | ||
| const { align = [...new Array(node.children.length)].map(() => null) } = attributes(node); | ||
|
|
||
| const mdNode = { | ||
| align, | ||
| type: 'table', | ||
| position, | ||
| children, | ||
| }; | ||
|
|
||
| parent.children[index] = mdNode; | ||
| } else if (node.name in types) { | ||
| const hProperties = attributes(node); | ||
|
|
||
| const mdNode = { | ||
| children: node.children, | ||
| type: types[node.name], | ||
| ...(['tr', 'td'].includes(node.name) | ||
| ? {} | ||
| : { | ||
| data: { | ||
| hName: node.name, | ||
| ...(Object.keys(hProperties).length ? { hProperties } : {}), | ||
| }, | ||
| }), | ||
| position: node.position, | ||
| }; | ||
|
|
||
| parent.children[index] = mdNode; | ||
| } | ||
| }); | ||
|
|
||
| visit(tree, 'paragraph', (node: Paragraph, index: number, parent: BlockContent | TableRow) => { | ||
| if (parent.type !== 'tableRow') return; | ||
|
|
||
| // @ts-ignore | ||
| parent.children.splice(index, 1, ...node.children); | ||
| }); | ||
|
|
||
| return tree; | ||
| }; | ||
|
|
||
| export default readmeComponents; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.