safe-mdx 0.0.5 → 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.
@@ -1,8 +1,8 @@
1
- import React from 'react'
2
1
  import dedent from 'dedent'
3
- import { test, expect } from 'vitest'
4
- import { MdastToJsx } from './safe-mdx.js'
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,17 +19,83 @@ 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
 
27
91
  i am a paragraph
92
+
93
+ <Heading>heading</Heading>
28
94
  `),
29
95
  ).toMatchInlineSnapshot(`
30
96
  {
31
97
  "errors": [],
32
- "html": "<h1>Hello</h1><p>i am a paragraph</p>",
98
+ "html": "<h1>Hello</h1><p>i am a paragraph</p><h1>heading</h1>",
33
99
  "result": <React.Fragment>
34
100
  <h1>
35
101
  Hello
@@ -37,6 +103,9 @@ test('basic', () => {
37
103
  <p>
38
104
  i am a paragraph
39
105
  </p>
106
+ <Heading>
107
+ heading
108
+ </Heading>
40
109
  </React.Fragment>,
41
110
  }
42
111
  `)
@@ -205,15 +274,13 @@ test('inline jsx', () => {
205
274
  ).toMatchInlineSnapshot(`
206
275
  {
207
276
  "errors": [],
208
- "html": "<p><h1>hello</h1></p>",
277
+ "html": "<h1>hello</h1>",
209
278
  "result": <React.Fragment>
210
- <p>
211
- <Heading
212
- level={2}
213
- >
214
- hello
215
- </Heading>
216
- </p>
279
+ <Heading
280
+ level={2}
281
+ >
282
+ hello
283
+ </Heading>
217
284
  </React.Fragment>,
218
285
  }
219
286
  `)
@@ -793,11 +860,41 @@ test('kitchen sink', () => {
793
860
  ).toMatchInlineSnapshot(`
794
861
  {
795
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
+ },
796
887
  {
797
888
  "message": "Unsupported jsx component dl",
798
889
  },
890
+ {
891
+ "message": "Unsupported language no-highlight",
892
+ },
893
+ {
894
+ "message": "Unsupported language no-highlight",
895
+ },
799
896
  ],
800
- "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&#x27;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&#x27;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&#x27;s what you&#x27;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&#x27;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&#x27;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&#x27;s what you&#x27;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_.
801
898
 
802
899
  Strong emphasis, aka bold, with **asterisks** or __underscores__.
803
900
 
@@ -850,11 +947,11 @@ test('kitchen sink', () => {
850
947
 
851
948
  [logo]: https://github.com/adam-p/markdown-here/raw/master/src/common/images/icon48.png &quot;Logo Title Text 2&quot;</code></pre><p>Here&#x27;s our logo (hover to see the title text):</p><p>Inline-style:
852
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:
853
- </p><a name="code"></a><h2>Code and Syntax Highlighting</h2><p>Code blocks are part of the Markdown spec, but syntax highlighting isn&#x27;t. However, many renderers -- like Github&#x27;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&#x27;re easier and only they support syntax highlighting.</p><pre><code>var s = &quot;JavaScript syntax highlighting&quot;;
854
- alert(s);</code></pre><pre><code>s = &quot;Python syntax highlighting&quot;
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&#x27;t. However, many renderers -- like Github&#x27;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&#x27;re easier and only they support syntax highlighting.</p><pre><code class="language-javascript">var s = &quot;JavaScript syntax highlighting&quot;;
951
+ alert(s);</code></pre><pre><code class="language-python">s = &quot;Python syntax highlighting&quot;
855
952
  print s</code></pre><pre><code>No language indicated, so no syntax highlighting.
856
- But let&#x27;s throw in a &amp;lt;b&amp;gt;tag&amp;lt;/b&amp;gt;.</code></pre><pre><code>var s = &quot;JavaScript syntax highlighting&quot;;
857
- alert(s);</code></pre><pre><code>s = &quot;Python syntax highlighting&quot;
953
+ But let&#x27;s throw in a &amp;lt;b&amp;gt;tag&amp;lt;/b&amp;gt;.</code></pre><pre><code class="language-javascript">var s = &quot;JavaScript syntax highlighting&quot;;
954
+ alert(s);</code></pre><pre><code class="language-python">s = &quot;Python syntax highlighting&quot;
858
955
  print s</code></pre><pre><code>No language indicated, so no syntax highlighting in Markdown Here (varies on Github).
859
956
  But let&#x27;s throw in a &lt;b&gt;tag&lt;/b&gt;.</code></pre><a name="tables"></a><h2>Tables</h2><p>Tables aren&#x27;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.
860
957
 
@@ -1447,13 +1544,17 @@ test('kitchen sink', () => {
1447
1544
  , or are indented with four spaces. I recommend only using the fenced code blocks -- they're easier and only they support syntax highlighting.
1448
1545
  </p>
1449
1546
  <pre>
1450
- <code>
1547
+ <code
1548
+ className="language-javascript"
1549
+ >
1451
1550
  var s = "JavaScript syntax highlighting";
1452
1551
  alert(s);
1453
1552
  </code>
1454
1553
  </pre>
1455
1554
  <pre>
1456
- <code>
1555
+ <code
1556
+ className="language-python"
1557
+ >
1457
1558
  s = "Python syntax highlighting"
1458
1559
  print s
1459
1560
  </code>
@@ -1465,13 +1566,17 @@ test('kitchen sink', () => {
1465
1566
  </code>
1466
1567
  </pre>
1467
1568
  <pre>
1468
- <code>
1569
+ <code
1570
+ className="language-javascript"
1571
+ >
1469
1572
  var s = "JavaScript syntax highlighting";
1470
1573
  alert(s);
1471
1574
  </code>
1472
1575
  </pre>
1473
1576
  <pre>
1474
- <code>
1577
+ <code
1578
+ className="language-python"
1579
+ >
1475
1580
  s = "Python syntax highlighting"
1476
1581
  print s
1477
1582
  </code>
@@ -1844,3 +1949,42 @@ test('kitchen sink', () => {
1844
1949
  }
1845
1950
  `)
1846
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
+ })