Skip to content

Commit 3430138

Browse files
authored
Don't parse ES syntax in open blocks (#763)
* Don't parse ES syntax in open blocks When a block is open, such as a list or blockquote, we shouldn't attempt to parse import/export syntax. Related gatsbyjs/gatsby#17315 * If an import node doesn't parse, make it a paragraph
1 parent 1294f77 commit 3430138

File tree

3 files changed

+23
-2
lines changed

3 files changed

+23
-2
lines changed
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
Hello, world!
2+
3+
import is a word
4+
5+
- import is a word
6+
- export is a word in lists, too!

packages/mdx/test/render.test.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ const EXPORT_SHORTCODE_FIXTURE = fs.readFileSync(
99
const PONYLANG_FIXTURE = fs.readFileSync(
1010
path.join(__dirname, './fixtures/ponylang.mdx')
1111
)
12+
const IMPORT_FIXTURE = fs.readFileSync(
13+
path.join(__dirname, './fixtures/imports.mdx')
14+
)
1215

1316
const components = {
1417
h1: ({children}) =>
@@ -55,3 +58,11 @@ it('turns a newline into a space with other adjacent phrasing content', async ()
5558

5659
expect(result).toContain('<em>foo</em>\n<code>bar</code>')
5760
})
61+
62+
it('ignores escaped import wording', async () => {
63+
const result = await renderWithReact(IMPORT_FIXTURE)
64+
65+
expect(result).toContain('<p>import')
66+
expect(result).toContain('<li>import')
67+
expect(result).toContain('<li>export')
68+
})

packages/remark-mdx/index.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ function attachParser(parser) {
3737
blocks.html = wrap(block)
3838
inlines.html = wrap(inlines.html, inlineJsx)
3939

40+
tokenizeEsSyntax.notInBlock = true
41+
4042
methods.splice(methods.indexOf('paragraph'), 0, 'esSyntax')
4143

4244
function wrap(original, customTokenizer) {
@@ -100,8 +102,10 @@ function tokenizeEsSyntax(eat, value) {
100102
const subvalue = index !== -1 ? value.slice(0, index) : value
101103

102104
if (isImportOrExport(subvalue)) {
103-
const nodes = extractImportsAndExports(subvalue, this.file)
104-
nodes.map(node => eat(node.value)(node))
105+
try {
106+
const nodes = extractImportsAndExports(subvalue, this.file)
107+
nodes.map(node => eat(node.value)(node))
108+
} catch (e) {}
105109
}
106110
}
107111

0 commit comments

Comments
 (0)