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: compat parser #1004

Merged
merged 28 commits into from
Oct 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
31d14c4
fix: migrate table newlines (#12959)
kellyjosephprice Oct 8, 2024
fcb1a36
feat: migrate emphasis (#12963)
kellyjosephprice Oct 11, 2024
ea55754
feat: migrate emphasis (#12963)
kellyjosephprice Oct 11, 2024
760ab03
fix: link ref migration (#12965)
kellyjosephprice Oct 14, 2024
89c62a9
fix: link ref migration (#12965)
kellyjosephprice Oct 14, 2024
2975511
fix: image border migration (#13027)
kellyjosephprice Oct 15, 2024
afd8c60
fix: image border migration (#13027)
kellyjosephprice Oct 15, 2024
0c6ba57
fix: migrating null table align values (#13033)
kellyjosephprice Oct 15, 2024
30f676a
fix: migrating null table align values (#13033)
kellyjosephprice Oct 15, 2024
eaa360f
fix: normalizing the doc (#13047)
kellyjosephprice Oct 16, 2024
d3b5152
fix: normalizing the doc (#13047)
kellyjosephprice Oct 16, 2024
11fc0d1
fix: table alignment (#13063)
kellyjosephprice Oct 17, 2024
28b6650
fix: table alignment (#13063)
kellyjosephprice Oct 17, 2024
bbf66a2
fix: dont match regular emphasis (#13022)
kellyjosephprice Oct 22, 2024
2c8828b
fix: dont match regular emphasis (#13022)
kellyjosephprice Oct 22, 2024
c4cee1a
chore: lint file (#13096)
kellyjosephprice Oct 22, 2024
36b9fa1
fix: migrating html comments (#13104)
kellyjosephprice Oct 22, 2024
c393b33
fix: migrating html comments (#13104)
kellyjosephprice Oct 22, 2024
89eccc8
feat: WIP compat parser
kellyjosephprice Oct 28, 2024
fa56eb2
Merge branch 'temp' into feat/compat-parser
kellyjosephprice Oct 28, 2024
3e3129b
chore: get transfomers
kellyjosephprice Oct 28, 2024
f7bd3f8
chore: dir structure
kellyjosephprice Oct 28, 2024
69b3684
Merge branch 'temp' into feat/migrate
kellyjosephprice Oct 28, 2024
5cc0c20
Merge branch 'feat/migrate' into feat/compat-parser
kellyjosephprice Oct 28, 2024
c5cebc8
fix: all the things
kellyjosephprice Oct 28, 2024
a94000c
fix: test + type
kellyjosephprice Oct 28, 2024
30cbfe1
externalize
kellyjosephprice Oct 28, 2024
d3a8ed9
fix: emphasis with links (#1007)
kellyjosephprice Oct 30, 2024
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
65 changes: 29 additions & 36 deletions __tests__/compilers/compatability.test.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import React from 'react';
import fs from 'node:fs';
import { vi } from 'vitest';
import { render, screen, prettyDOM, configure } from '@testing-library/react';
import { render, screen } from '@testing-library/react';

import { mdx, compile, run } from '../../index';
import * as rdmd from '@readme/markdown-legacy';
import { mdastV6, mdx, compile, run } from '../../index';

describe('compatability with RDMD', () => {
it('compiles glossary nodes', () => {
Expand Down Expand Up @@ -126,7 +125,7 @@ describe('compatability with RDMD', () => {
This is some in progress <!-- commented out stuff -->
`;

expect(mdx(rdmd.mdast(md)).trim()).toBe('This is some in progress {/* commented out stuff */}');
expect(mdx(mdastV6(md)).trim()).toBe('This is some in progress {/* commented out stuff */}');
});

it('compiles multi-line html comments to JSX comments', () => {
Expand All @@ -140,7 +139,7 @@ This is some in progress <!-- commented out stuff -->
-->
`;

expect(mdx(rdmd.mdast(md)).trim()).toMatchInlineSnapshot(`
expect(mdx(mdastV6(md)).trim()).toMatchInlineSnapshot(`
"## Wip

{/*
Expand All @@ -156,52 +155,52 @@ This is some in progress <!-- commented out stuff -->
This is a break: <br>
`;

expect(mdx(rdmd.mdast(md)).trim()).toBe('This is a break: <br />');
expect(mdx(mdastV6(md)).trim()).toBe('This is a break: <br />');
});

it.skip('closes un-closed self closing tags with a space', () => {
const md = `
This is a break: <br >
`;

expect(mdx(rdmd.mdast(md)).trim()).toBe('This is a break: <br />');
expect(mdx(mdastV6(md)).trim()).toBe('This is a break: <br />');
});

it.skip('closes complex un-closed self closing tags', () => {
const md = `
This is an image: <img src="http://example.com/#\\>" >
`;

expect(mdx(rdmd.mdast(md)).trim()).toBe('This is an image: <img src="http://example.com/#\\>" />');
expect(mdx(mdastV6(md)).trim()).toBe('This is an image: <img src="http://example.com/#\\>" />');
});

it('compiles escapes', () => {
const md = `
\\- not a list item
`;

expect(mdx(rdmd.mdast(md)).trim()).toBe('\\- not a list item');
expect(mdx(mdastV6(md)).trim()).toBe('\\- not a list item');
});

it('compiles escapes of backslashes', () => {
const md = `
\\\\**still emphatic**
`;

expect(mdx(rdmd.mdast(md)).trim()).toBe('\\\\**still emphatic**');
expect(mdx(mdastV6(md)).trim()).toBe('\\\\**still emphatic**');
});

it('compiles magic block images into blocks', () => {
const imageMd = fs.readFileSync('__tests__/fixtures/image-block-no-attrs.md', { encoding: 'utf8' });
const imageMdx = fs.readFileSync('__tests__/fixtures/image-block-no-attrs.mdx', { encoding: 'utf8' });

expect(mdx(rdmd.mdast(imageMd))).toBe(imageMdx);
expect(mdx(mdastV6(imageMd))).toBe(imageMdx);
});

it('compiles user variables', () => {
const md = `Contact me at <<email>>`;

expect(mdx(rdmd.mdast(md))).toBe(`Contact me at {user.email}\n`);
expect(mdx(mdastV6(md))).toBe(`Contact me at {user.email}\n`);
});

describe('<HTMLBlock> wrapping', () => {
Expand All @@ -225,7 +224,7 @@ This is an image: <img src="http://example.com/#\\>" >
`;

it('should wrap raw <style> tags in an <HTMLBlock>', async () => {
const legacyAST = rdmd.mdast(rawStyle);
const legacyAST = mdastV6(rawStyle);
const converted = mdx(legacyAST);
const compiled = compile(converted);
const Component = (await run(compiled)).default;
Expand All @@ -238,7 +237,7 @@ This is an image: <img src="http://example.com/#\\>" >
});

it('should wrap raw <script> tags in an <HTMLBlock>', async () => {
const legacyAST = rdmd.mdast(rawScript);
const legacyAST = mdastV6(rawScript);
const converted = mdx(legacyAST);
const compiled = compile(converted);
const Component = (await run(compiled)).default;
Expand All @@ -255,7 +254,7 @@ This is an image: <img src="http://example.com/#\\>" >
* @note compatability mode has been deprecated for RMDX
*/
const spy = vi.spyOn(console, 'log');
const legacyAST = rdmd.mdast(magicScript);
const legacyAST = mdastV6(magicScript);
const converted = mdx(legacyAST);
const compiled = compile(converted);
const Component = await run(compiled);
Expand Down Expand Up @@ -287,7 +286,7 @@ This is an image: <img src="http://example.com/#\\>" >
[/block]
`;

const rmdx = mdx(rdmd.mdast(md));
const rmdx = mdx(mdastV6(md));

expect(rmdx).toMatch('<Image align="center" width="250px" src="https://files.readme.io/4a1c7a0-Iphone.jpeg" />');
});
Expand All @@ -311,7 +310,7 @@ This is an image: <img src="http://example.com/#\\>" >
}
[/block]`;

const rmdx = mdx(rdmd.mdast(md));
const rmdx = mdx(mdastV6(md));
expect(rmdx).toMatchInlineSnapshot(
`
"<Image alt="Data Plane Setup" align="center" border={true} src="https://files.readme.io/fd21f977cfbb9f55b3a13ab0b827525e94ee1576f21bbe82945cdc22cc966d82-Screenshot_2024-09-12_at_3.47.05_PM.png">
Expand All @@ -325,7 +324,7 @@ This is an image: <img src="http://example.com/#\\>" >
it('trims whitespace surrounding phrasing content (emphasis, strong, etc)', () => {
const md = `** bold ** and _ italic _ and *** bold italic ***`;

const rmdx = mdx(rdmd.mdast(md));
const rmdx = mdx(mdastV6(md));
expect(rmdx).toMatchInlineSnapshot(`
"**bold** and *italic* and ***bold italic***
"
Expand All @@ -352,7 +351,7 @@ This is an image: <img src="http://example.com/#\\>" >
## Tile View
`;

const tree = rdmd.mdast(md);
const tree = mdastV6(md);
const rmdx = mdx(tree);
expect(rmdx).toMatchInlineSnapshot(`
"![806](https://files.readme.io/9ac3bf4-SAP_Folders_Note.png "SAP Folders Note.png")
Expand Down Expand Up @@ -383,7 +382,7 @@ ${JSON.stringify(
[/block]
`;

const rmdx = mdx(rdmd.mdast(md));
const rmdx = mdx(mdastV6(md));
expect(rmdx).toMatchInlineSnapshot(`
"<Table align={["left","left"]}>
<thead>
Expand All @@ -405,16 +404,8 @@ ${JSON.stringify(
</td>

<td style={{ textAlign: "left" }}>
Pseudo-list:




● One




Pseudo-list:\\
● One\\
● Two
</td>
</tr>
Expand All @@ -431,7 +422,7 @@ ${JSON.stringify(
> Lorem ipsum dolor sit amet consectetur adipisicing elit. Error eos animi obcaecati quod repudiandae aliquid nemo veritatis ex, quos delectus minus sit omnis vel dolores libero, recusandae ea dignissimos iure?
`;

const rmdx = mdx(rdmd.mdast(md));
const rmdx = mdx(mdastV6(md));
expect(rmdx).toMatchInlineSnapshot(`
"> 🥈
>
Expand Down Expand Up @@ -461,7 +452,7 @@ ${JSON.stringify(
[/block]
`;

const rmdx = mdx(rdmd.mdast(md));
const rmdx = mdx(mdastV6(md));
expect(rmdx).toMatchInlineSnapshot(`
"<Table align={["left","left"]}>
<thead>
Expand All @@ -483,10 +474,12 @@ ${JSON.stringify(
</td>

<td style={{ textAlign: "left" }}>
\`{
\`\`\`
{
"field": "ID",
"type": "ASC"
}\`
}
\`\`\`
</td>
</tr>
</tbody>
Expand All @@ -498,7 +491,7 @@ ${JSON.stringify(
it('compiles inline html', () => {
const md = `Inline html: <small>_string_</small>`;

const rmdx = mdx(rdmd.mdast(md));
const rmdx = mdx(mdastV6(md));
expect(rmdx).toMatchInlineSnapshot(`
"Inline html: <small>*string*</small>
"
Expand All @@ -508,7 +501,7 @@ ${JSON.stringify(
it('compiles tag-like syntax', () => {
const md = `Inline: <what even is this>`;

const rmdx = mdx(rdmd.mdast(md));
const rmdx = mdx(mdastV6(md));
expect(rmdx).toMatchInlineSnapshot(`
"Inline: <what even is this>
"
Expand Down
34 changes: 34 additions & 0 deletions __tests__/migration/emphasis.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import * as rmdx from '../../index';

describe('migrating emphasis', () => {
it('trims whitespace surrounding phrasing content (emphasis, strong, etc)', () => {
const md = '** bold ** and _ italic _ and *** bold italic ***';

const mdx = rmdx.mdx(rmdx.mdastV6(md));
expect(mdx).toMatchInlineSnapshot(`
"**bold** and *italic* and ***bold italic***
"
`);
});

it('moves whitespace surrounding phrasing content (emphasis, strong, etc) to the appropriate place', () => {
const md = '**bold **and also_ italic_ and*** bold italic***aaaaaah';

const mdx = rmdx.mdx(rmdx.mdastV6(md));
expect(mdx).toMatchInlineSnapshot(`
"**bold** and also *italic* and ***bold italic***aaaaaah
"
`);
});

it('migrates a complex case', () => {
const md =
'*the recommended initial action is to**initiate a [reversal operation (rollback)](https://docs.jupico.com/reference/ccrollback) test**. *';

const mdx = rmdx.mdx(rmdx.mdastV6(md));
expect(mdx).toMatchInlineSnapshot(`
"*the recommended initial action is to**initiate a [reversal operation (rollback)](https://docs.jupico.com/reference/ccrollback) test**.*
"
`);
});
});
48 changes: 48 additions & 0 deletions __tests__/migration/html-comments.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import * as rmdx from '../../index';

describe('migrating html comments', () => {
it('migrates escaped html comments', () => {
const md = `
<!--



## Walkthrough

[block:html]
{
"html": "<div style="position: relative; padding-bottom: 56.25%; height: 0;"><iframe src="https://www.loom.com/embed/53dd194717bb4965a8e838b95715ff18" frameborder="0" webkitallowfullscreen mozallowfullscreen allowfullscreen style="position: absolute; top: 0; left: 0; width: 100%; height: 100%;"></iframe></div>"
}
[/block]


<br />

\\-->
`;

const ast = rmdx.mdastV6(md);
const mdx = rmdx.mdx(ast);
expect(mdx).toMatchInlineSnapshot(`
"{/*



## Walkthrough


[block:html]
{
"html": "<div style="position: relative; padding-bottom: 56.25%; height: 0;"><iframe src="https://www.loom.com/embed/53dd194717bb4965a8e838b95715ff18" frameborder="0" webkitallowfullscreen mozallowfullscreen allowfullscreen style="position: absolute; top: 0; left: 0; width: 100%; height: 100%;"></iframe></div>"
}
[/block]



<br />

*/}
"
`);
});
});
30 changes: 30 additions & 0 deletions __tests__/migration/image.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import * as rmdx from '../../index';

describe('migrating images', () => {
it('compiles images', () => {
const md = `
[block:image]
{
"images": [
{
"image": [
"https://fastly.picsum.photos/id/507/200/300.jpg?hmac=v0NKvUrOWTKZuZFmMlLN_7-RdRgeF-qFLeBGXpufxgg",
"",
""
],
"align": "center",
"border": true
}
]
}
[/block]
`;

const ast = rmdx.mdastV6(md);
const mdx = rmdx.mdx(ast);
expect(mdx).toMatchInlineSnapshot(`
"<Image align="center" className="border" border={true} src="https://fastly.picsum.photos/id/507/200/300.jpg?hmac=v0NKvUrOWTKZuZFmMlLN_7-RdRgeF-qFLeBGXpufxgg" />
"
`);
});
});
31 changes: 31 additions & 0 deletions __tests__/migration/link-reference.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import * as rmdx from '../../index';

describe('mdx migration of link references', () => {
it('compiles link references correctly', () => {
const md = '[wat_wat]';

const ast = rmdx.mdastV6(md);
const mdx = rmdx.mdx(ast);
expect(mdx).toMatchInlineSnapshot(`
"\\[wat\\_wat]
"
`);
});

it('compiles link references with defintions correctly', () => {
const md = `
[wat_wat]

[wat_wat]: https://wat.com
`;

const ast = rmdx.mdastV6(md);
const mdx = rmdx.mdx(ast);
expect(mdx).toMatchInlineSnapshot(`
"[wat\\_wat][wat_wat]

[wat_wat]: https://wat.com
"
`);
});
});
Loading