safe-mdx 0.0.4 → 0.0.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +50 -4
- package/dist/plugins.d.ts +12 -0
- package/dist/plugins.d.ts.map +1 -0
- package/dist/plugins.js +68 -0
- package/dist/plugins.js.map +1 -0
- package/dist/safe-mdx.d.ts +10 -9
- package/dist/safe-mdx.d.ts.map +1 -1
- package/dist/safe-mdx.js +381 -70
- package/dist/safe-mdx.js.map +1 -1
- package/dist/safe-mdx.test.js +182 -20
- package/dist/safe-mdx.test.js.map +1 -1
- package/package.json +54 -51
- package/src/plugins.ts +87 -0
- package/src/safe-mdx.test.tsx +190 -20
- package/src/safe-mdx.tsx +367 -56
- package/dist/index.d.ts +0 -2
- package/dist/index.d.ts.map +0 -1
- package/dist/index.js +0 -2
- package/dist/index.js.map +0 -1
- package/src/index.ts +0 -1
package/src/safe-mdx.test.tsx
CHANGED
|
@@ -1,8 +1,8 @@
|
|
|
1
|
-
import React from 'react'
|
|
2
1
|
import dedent from 'dedent'
|
|
3
|
-
import
|
|
4
|
-
import { MdastToJsx } from './safe-mdx'
|
|
2
|
+
import React from 'react'
|
|
5
3
|
import { renderToStaticMarkup } from 'react-dom/server'
|
|
4
|
+
import { expect, test } from 'vitest'
|
|
5
|
+
import { mdastBfs, MdastToJsx, mdxParse } from './safe-mdx.js'
|
|
6
6
|
void React
|
|
7
7
|
|
|
8
8
|
const components = {
|
|
@@ -19,11 +19,106 @@ function render(code) {
|
|
|
19
19
|
return { result, errors: visitor.errors || [], html }
|
|
20
20
|
}
|
|
21
21
|
|
|
22
|
+
test('remark and jsx does not wrap in p', () => {
|
|
23
|
+
const code = dedent`
|
|
24
|
+
# Hello
|
|
25
|
+
|
|
26
|
+
i am a paragraph
|
|
27
|
+
|
|
28
|
+
<Heading>heading</Heading>
|
|
29
|
+
|
|
30
|
+
something
|
|
31
|
+
`
|
|
32
|
+
const mdast = mdxParse(code)
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
mdastBfs(mdast, (x) => {
|
|
36
|
+
delete x.position
|
|
37
|
+
})
|
|
38
|
+
expect(mdast).toMatchInlineSnapshot(`
|
|
39
|
+
{
|
|
40
|
+
"children": [
|
|
41
|
+
{
|
|
42
|
+
"children": [
|
|
43
|
+
{
|
|
44
|
+
"type": "text",
|
|
45
|
+
"value": "Hello",
|
|
46
|
+
},
|
|
47
|
+
],
|
|
48
|
+
"depth": 1,
|
|
49
|
+
"type": "heading",
|
|
50
|
+
},
|
|
51
|
+
{
|
|
52
|
+
"children": [
|
|
53
|
+
{
|
|
54
|
+
"type": "text",
|
|
55
|
+
"value": "i am a paragraph",
|
|
56
|
+
},
|
|
57
|
+
],
|
|
58
|
+
"type": "paragraph",
|
|
59
|
+
},
|
|
60
|
+
{
|
|
61
|
+
"attributes": [],
|
|
62
|
+
"children": [
|
|
63
|
+
{
|
|
64
|
+
"type": "text",
|
|
65
|
+
"value": "heading",
|
|
66
|
+
},
|
|
67
|
+
],
|
|
68
|
+
"name": "Heading",
|
|
69
|
+
"type": "mdxJsxFlowElement",
|
|
70
|
+
},
|
|
71
|
+
{
|
|
72
|
+
"children": [
|
|
73
|
+
{
|
|
74
|
+
"type": "text",
|
|
75
|
+
"value": "something",
|
|
76
|
+
},
|
|
77
|
+
],
|
|
78
|
+
"type": "paragraph",
|
|
79
|
+
},
|
|
80
|
+
],
|
|
81
|
+
"type": "root",
|
|
82
|
+
}
|
|
83
|
+
`)
|
|
84
|
+
})
|
|
85
|
+
|
|
22
86
|
test('basic', () => {
|
|
23
87
|
expect(
|
|
24
88
|
render(dedent`
|
|
25
89
|
# Hello
|
|
26
90
|
|
|
91
|
+
i am a paragraph
|
|
92
|
+
|
|
93
|
+
<Heading>heading</Heading>
|
|
94
|
+
`),
|
|
95
|
+
).toMatchInlineSnapshot(`
|
|
96
|
+
{
|
|
97
|
+
"errors": [],
|
|
98
|
+
"html": "<h1>Hello</h1><p>i am a paragraph</p><h1>heading</h1>",
|
|
99
|
+
"result": <React.Fragment>
|
|
100
|
+
<h1>
|
|
101
|
+
Hello
|
|
102
|
+
</h1>
|
|
103
|
+
<p>
|
|
104
|
+
i am a paragraph
|
|
105
|
+
</p>
|
|
106
|
+
<Heading>
|
|
107
|
+
heading
|
|
108
|
+
</Heading>
|
|
109
|
+
</React.Fragment>,
|
|
110
|
+
}
|
|
111
|
+
`)
|
|
112
|
+
})
|
|
113
|
+
test('frontmatter', () => {
|
|
114
|
+
expect(
|
|
115
|
+
render(dedent`
|
|
116
|
+
---
|
|
117
|
+
hello: 5
|
|
118
|
+
---
|
|
119
|
+
|
|
120
|
+
# Hello
|
|
121
|
+
|
|
27
122
|
i am a paragraph
|
|
28
123
|
`),
|
|
29
124
|
).toMatchInlineSnapshot(`
|
|
@@ -179,15 +274,13 @@ test('inline jsx', () => {
|
|
|
179
274
|
).toMatchInlineSnapshot(`
|
|
180
275
|
{
|
|
181
276
|
"errors": [],
|
|
182
|
-
"html": "<
|
|
277
|
+
"html": "<h1>hello</h1>",
|
|
183
278
|
"result": <React.Fragment>
|
|
184
|
-
<
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
</Heading>
|
|
190
|
-
</p>
|
|
279
|
+
<Heading
|
|
280
|
+
level={2}
|
|
281
|
+
>
|
|
282
|
+
hello
|
|
283
|
+
</Heading>
|
|
191
284
|
</React.Fragment>,
|
|
192
285
|
}
|
|
193
286
|
`)
|
|
@@ -767,11 +860,41 @@ test('kitchen sink', () => {
|
|
|
767
860
|
).toMatchInlineSnapshot(`
|
|
768
861
|
{
|
|
769
862
|
"errors": [
|
|
863
|
+
{
|
|
864
|
+
"message": "Unsupported language no-highlight",
|
|
865
|
+
},
|
|
866
|
+
{
|
|
867
|
+
"message": "Unsupported language no-highlight",
|
|
868
|
+
},
|
|
869
|
+
{
|
|
870
|
+
"message": "Unsupported language no-highlight",
|
|
871
|
+
},
|
|
872
|
+
{
|
|
873
|
+
"message": "Unsupported language no-highlight",
|
|
874
|
+
},
|
|
875
|
+
{
|
|
876
|
+
"message": "Unsupported language no-highlight",
|
|
877
|
+
},
|
|
878
|
+
{
|
|
879
|
+
"message": "Unsupported language no-highlight",
|
|
880
|
+
},
|
|
881
|
+
{
|
|
882
|
+
"message": "Unsupported language no-highlight",
|
|
883
|
+
},
|
|
884
|
+
{
|
|
885
|
+
"message": "Unsupported language no-highlight",
|
|
886
|
+
},
|
|
770
887
|
{
|
|
771
888
|
"message": "Unsupported jsx component dl",
|
|
772
889
|
},
|
|
890
|
+
{
|
|
891
|
+
"message": "Unsupported language no-highlight",
|
|
892
|
+
},
|
|
893
|
+
{
|
|
894
|
+
"message": "Unsupported language no-highlight",
|
|
895
|
+
},
|
|
773
896
|
],
|
|
774
|
-
"html": "<h1>Markdown Kitchen Sink</h1><p>This file is <a href="https://github.com/adam-p/markdown-here/wiki/Markdown-Cheatsheet" title="">https://github.com/adam-p/markdown-here/wiki/Markdown-Cheatsheet</a> plus a few fixes and additions. Used by <a href="https://github.com/obedm503/bootmark" title="">obedm503/bootmark</a> to <a href="https://obedm503.github.io/bootmark/docs/markdown-cheatsheet.html" title="">demonstrate</a> it's styling features.</p><p>This is intended as a quick reference and showcase. For more complete info, see <a href="http://daringfireball.net/projects/markdown/" title="">John Gruber's original spec</a> and the <a href="http://github.github.com/github-flavored-markdown/" title="">Github-flavored Markdown info page</a>.</p><p>Note that there is also a <a href="./Markdown-Here-Cheatsheet" title="">Cheatsheet specific to Markdown Here</a> if that's what you're looking for. You can also check out <a href="./Other-Markdown-Tools" title="">more Markdown tools</a>.</p><h5>Table of Contents</h5><p><a href="#headers" title="">Headers</a><br/><a href="#emphasis" title="">Emphasis</a><br/><a href="#lists" title="">Lists</a><br/><a href="#links" title="">Links</a><br/><a href="#images" title="">Images</a><br/><a href="#code" title="">Code and Syntax Highlighting</a><br/><a href="#tables" title="">Tables</a><br/><a href="#blockquotes" title="">Blockquotes</a><br/><a href="#html" title="">Inline HTML</a><br/><a href="#hr" title="">Horizontal Rule</a><br/><a href="#lines" title="">Line Breaks</a><br/><a href="#videos" title="">YouTube Videos</a></p><a name="headers"></a><h2>Headers</h2><h1>H1</h1><h2>H2</h2><h3>H3</h3><h4>H4</h4><h5>H5</h5><h6>H6</h6><p>Alternatively, for H1 and H2, an underline-ish style:</p><h1>Alt-H1</h1><h2>Alt-H2</h2><h1>H1</h1><h2>H2</h2><h3>H3</h3><h4>H4</h4><h5>H5</h5><h6>H6</h6><p>Alternatively, for H1 and H2, an underline-ish style:</p><h1>Alt-H1</h1><h2>Alt-H2</h2><a name="emphasis"></a><h2>Emphasis</h2><pre><code>Emphasis, aka italics, with *asterisks* or _underscores_.
|
|
897
|
+
"html": "<link rel="preload" as="image" href="https://github.com/adam-p/markdown-here/raw/master/src/common/images/icon48.png"/><h1>Markdown Kitchen Sink</h1><p>This file is <a href="https://github.com/adam-p/markdown-here/wiki/Markdown-Cheatsheet" title="">https://github.com/adam-p/markdown-here/wiki/Markdown-Cheatsheet</a> plus a few fixes and additions. Used by <a href="https://github.com/obedm503/bootmark" title="">obedm503/bootmark</a> to <a href="https://obedm503.github.io/bootmark/docs/markdown-cheatsheet.html" title="">demonstrate</a> it's styling features.</p><p>This is intended as a quick reference and showcase. For more complete info, see <a href="http://daringfireball.net/projects/markdown/" title="">John Gruber's original spec</a> and the <a href="http://github.github.com/github-flavored-markdown/" title="">Github-flavored Markdown info page</a>.</p><p>Note that there is also a <a href="./Markdown-Here-Cheatsheet" title="">Cheatsheet specific to Markdown Here</a> if that's what you're looking for. You can also check out <a href="./Other-Markdown-Tools" title="">more Markdown tools</a>.</p><h5>Table of Contents</h5><p><a href="#headers" title="">Headers</a><br/><a href="#emphasis" title="">Emphasis</a><br/><a href="#lists" title="">Lists</a><br/><a href="#links" title="">Links</a><br/><a href="#images" title="">Images</a><br/><a href="#code" title="">Code and Syntax Highlighting</a><br/><a href="#tables" title="">Tables</a><br/><a href="#blockquotes" title="">Blockquotes</a><br/><a href="#html" title="">Inline HTML</a><br/><a href="#hr" title="">Horizontal Rule</a><br/><a href="#lines" title="">Line Breaks</a><br/><a href="#videos" title="">YouTube Videos</a></p><a name="headers"></a><h2>Headers</h2><h1>H1</h1><h2>H2</h2><h3>H3</h3><h4>H4</h4><h5>H5</h5><h6>H6</h6><p>Alternatively, for H1 and H2, an underline-ish style:</p><h1>Alt-H1</h1><h2>Alt-H2</h2><h1>H1</h1><h2>H2</h2><h3>H3</h3><h4>H4</h4><h5>H5</h5><h6>H6</h6><p>Alternatively, for H1 and H2, an underline-ish style:</p><h1>Alt-H1</h1><h2>Alt-H2</h2><a name="emphasis"></a><h2>Emphasis</h2><pre><code>Emphasis, aka italics, with *asterisks* or _underscores_.
|
|
775
898
|
|
|
776
899
|
Strong emphasis, aka bold, with **asterisks** or __underscores__.
|
|
777
900
|
|
|
@@ -824,11 +947,11 @@ test('kitchen sink', () => {
|
|
|
824
947
|
|
|
825
948
|
[logo]: https://github.com/adam-p/markdown-here/raw/master/src/common/images/icon48.png "Logo Title Text 2"</code></pre><p>Here's our logo (hover to see the title text):</p><p>Inline-style:
|
|
826
949
|
<img src="https://github.com/adam-p/markdown-here/raw/master/src/common/images/icon48.png" alt="alt text" title="Logo Title Text 1"/></p><p>Reference-style:
|
|
827
|
-
</p><a name="code"></a><h2>Code and Syntax Highlighting</h2><p>Code blocks are part of the Markdown spec, but syntax highlighting isn't. However, many renderers -- like Github's and <em>Markdown Here</em> -- support syntax highlighting. Which languages are supported and how those language names should be written will vary from renderer to renderer. <em>Markdown Here</em> supports highlighting for dozens of languages (and not-really-languages, like diffs and HTTP headers); to see the complete list, and how to write the language names, see the <a href="http://softwaremaniacs.org/media/soft/highlight/test.html" title="">highlight.js demo page</a>.</p><pre><code>Inline \`code\` has \`back-ticks around\` it.</code></pre><p>Inline <code>code</code> has <code>back-ticks around</code> it.</p><p>Blocks of code are either fenced by lines with three back-ticks <code>\`\`\`</code>, or are indented with four spaces. I recommend only using the fenced code blocks -- they're easier and only they support syntax highlighting.</p><pre><code>var s = "JavaScript syntax highlighting";
|
|
828
|
-
alert(s);</code></pre><pre><code>s = "Python syntax highlighting"
|
|
950
|
+
</p><a name="code"></a><h2>Code and Syntax Highlighting</h2><p>Code blocks are part of the Markdown spec, but syntax highlighting isn't. However, many renderers -- like Github's and <em>Markdown Here</em> -- support syntax highlighting. Which languages are supported and how those language names should be written will vary from renderer to renderer. <em>Markdown Here</em> supports highlighting for dozens of languages (and not-really-languages, like diffs and HTTP headers); to see the complete list, and how to write the language names, see the <a href="http://softwaremaniacs.org/media/soft/highlight/test.html" title="">highlight.js demo page</a>.</p><pre><code>Inline \`code\` has \`back-ticks around\` it.</code></pre><p>Inline <code>code</code> has <code>back-ticks around</code> it.</p><p>Blocks of code are either fenced by lines with three back-ticks <code>\`\`\`</code>, or are indented with four spaces. I recommend only using the fenced code blocks -- they're easier and only they support syntax highlighting.</p><pre><code class="language-javascript">var s = "JavaScript syntax highlighting";
|
|
951
|
+
alert(s);</code></pre><pre><code class="language-python">s = "Python syntax highlighting"
|
|
829
952
|
print s</code></pre><pre><code>No language indicated, so no syntax highlighting.
|
|
830
|
-
But let's throw in a &lt;b&gt;tag&lt;/b&gt;.</code></pre><pre><code>var s = "JavaScript syntax highlighting";
|
|
831
|
-
alert(s);</code></pre><pre><code>s = "Python syntax highlighting"
|
|
953
|
+
But let's throw in a &lt;b&gt;tag&lt;/b&gt;.</code></pre><pre><code class="language-javascript">var s = "JavaScript syntax highlighting";
|
|
954
|
+
alert(s);</code></pre><pre><code class="language-python">s = "Python syntax highlighting"
|
|
832
955
|
print s</code></pre><pre><code>No language indicated, so no syntax highlighting in Markdown Here (varies on Github).
|
|
833
956
|
But let's throw in a <b>tag</b>.</code></pre><a name="tables"></a><h2>Tables</h2><p>Tables aren't part of the core Markdown spec, but they are part of GFM and <em>Markdown Here</em> supports them. They are an easy way of adding tables to your email -- a task that would otherwise require copy-pasting from another application.</p><pre><code>Colons can be used to align columns.
|
|
834
957
|
|
|
@@ -1421,13 +1544,17 @@ test('kitchen sink', () => {
|
|
|
1421
1544
|
, or are indented with four spaces. I recommend only using the fenced code blocks -- they're easier and only they support syntax highlighting.
|
|
1422
1545
|
</p>
|
|
1423
1546
|
<pre>
|
|
1424
|
-
<code
|
|
1547
|
+
<code
|
|
1548
|
+
className="language-javascript"
|
|
1549
|
+
>
|
|
1425
1550
|
var s = "JavaScript syntax highlighting";
|
|
1426
1551
|
alert(s);
|
|
1427
1552
|
</code>
|
|
1428
1553
|
</pre>
|
|
1429
1554
|
<pre>
|
|
1430
|
-
<code
|
|
1555
|
+
<code
|
|
1556
|
+
className="language-python"
|
|
1557
|
+
>
|
|
1431
1558
|
s = "Python syntax highlighting"
|
|
1432
1559
|
print s
|
|
1433
1560
|
</code>
|
|
@@ -1439,13 +1566,17 @@ test('kitchen sink', () => {
|
|
|
1439
1566
|
</code>
|
|
1440
1567
|
</pre>
|
|
1441
1568
|
<pre>
|
|
1442
|
-
<code
|
|
1569
|
+
<code
|
|
1570
|
+
className="language-javascript"
|
|
1571
|
+
>
|
|
1443
1572
|
var s = "JavaScript syntax highlighting";
|
|
1444
1573
|
alert(s);
|
|
1445
1574
|
</code>
|
|
1446
1575
|
</pre>
|
|
1447
1576
|
<pre>
|
|
1448
|
-
<code
|
|
1577
|
+
<code
|
|
1578
|
+
className="language-python"
|
|
1579
|
+
>
|
|
1449
1580
|
s = "Python syntax highlighting"
|
|
1450
1581
|
print s
|
|
1451
1582
|
</code>
|
|
@@ -1818,3 +1949,42 @@ test('kitchen sink', () => {
|
|
|
1818
1949
|
}
|
|
1819
1950
|
`)
|
|
1820
1951
|
})
|
|
1952
|
+
|
|
1953
|
+
test('code block rendering', () => {
|
|
1954
|
+
const code = dedent`
|
|
1955
|
+
`
|
|
1956
|
+
expect(
|
|
1957
|
+
render(dedent`
|
|
1958
|
+
\`\`\`typescript
|
|
1959
|
+
const x = 1;
|
|
1960
|
+
\`\`\`
|
|
1961
|
+
|
|
1962
|
+
\`\`\`invalid-language
|
|
1963
|
+
const y = 2;
|
|
1964
|
+
\`\`\`
|
|
1965
|
+
`),
|
|
1966
|
+
).toMatchInlineSnapshot(`
|
|
1967
|
+
{
|
|
1968
|
+
"errors": [
|
|
1969
|
+
{
|
|
1970
|
+
"message": "Unsupported language invalid-language",
|
|
1971
|
+
},
|
|
1972
|
+
],
|
|
1973
|
+
"html": "<pre><code class="language-typescript">const x = 1;</code></pre><pre><code>const y = 2;</code></pre>",
|
|
1974
|
+
"result": <React.Fragment>
|
|
1975
|
+
<pre>
|
|
1976
|
+
<code
|
|
1977
|
+
className="language-typescript"
|
|
1978
|
+
>
|
|
1979
|
+
const x = 1;
|
|
1980
|
+
</code>
|
|
1981
|
+
</pre>
|
|
1982
|
+
<pre>
|
|
1983
|
+
<code>
|
|
1984
|
+
const y = 2;
|
|
1985
|
+
</code>
|
|
1986
|
+
</pre>
|
|
1987
|
+
</React.Fragment>,
|
|
1988
|
+
}
|
|
1989
|
+
`)
|
|
1990
|
+
})
|