question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[v2] Fenced code blocks within JSX elements with indent > 2 parses incorrectly

See original GitHub issue

I’ve been playing around a bit more with MDX v2 and encountered what I think is a bug, or at the very least, some surprising behavior. I have the need to render fenced code blocks within table cells. Because of the highly nested structure of a table, and to maintain sane indentation, I’ve placed the code block indented within the <td />. Unfortunately MDX v2 parses this incorrectly.

Steps to reproduce

For example, here is a simple table with a single cell that outputs a code block in one of the tds:

<table>
  <tbody>
    <tr>
      <td>
        ```
        console.log(test)
        ```
      </td>
      <td>
        Some more text
      </td>
    </tr>
  </tbody>
</table>

This parses into an AST like the following:

{
  "type": "code",
  "lang": null,
  "meta": null,
  "value": "console.log(test)\n```\n",
  "position": {
    "start": {
      "line": 5,
      "column": 1,
      "offset": 38
    },
    "end": {
      "line": 8,
      "column": 7,
      "offset": 94
    },
    "indent": [
      1,
      1,
      1
    ]
  }
}

Notice how the value ends with the fence literal and a newline. This results in text that renders to the screen as:

console.log(test)
```

If I remove the indentation in front of the code block, it seems to parse and output the correct text:

Markdown

<table>
  <tbody>
    <tr>
      <td>
```
console.log(test)
```
      </td>
      <td>
        Some more text
      </td>
    </tr>
  </tbody>
</table>

AST

{
  "type": "code",
  "lang": null,
  "meta": null,
  "value": "console.log(test)",
  "position": {
    "start": {
      "line": 5,
      "column": 1,
      "offset": 38
    },
    "end": {
      "line": 7,
      "column": 4,
      "offset": 63
    },
    "indent": [
      1,
      1
    ]
  }
}

Output

console.log(test)

You can see the result of each of these using the playground

Expected behaviour

I would expect that regardless of the indentation, the code block’s value would result in the same thing.

Is this intentional behavior or a bug? I find it a bit surprising at the least.

Minimal fix

I’ve been able to “fix” this issue by creating my own MDX remark transform:

const visit = require('unist-util-visit');

const INCORRECT_CODE_BLOCK = /`{3,}\n?$/;

const indentedCodeBlock = () => (tree) => {
  visit(
    tree,
    (node) => node.type === 'code' && INCORRECT_CODE_BLOCK.test(node.value),
    (codeBlock) => {
      codeBlock.value = codeBlock.value.replace(INCORRECT_CODE_BLOCK, '').trim();
    }
  );
};

This rewrites the value by removing the additional backticks in the output. Maybe this is the best way to go about this for now? Maybe not?

Any help would be appreciated. Thank you so much!!

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Reactions:1
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

7reactions
wooormcommented, Dec 8, 2020

Solved this with micromark (https://github.com/micromark/micromark-extension-mdxjs). pulling the new parser into mdx soon

2reactions
wooormcommented, Dec 18, 2020

Hi all! I’m going to close this as it landed on the main, the (now default) branch we’re using to gear up to the MDX@2 release.

The reason is so that it’s more clear which issues still need to be solved already and don’t require further work.

Expect another next release soonish, which might include it, or maybe it’s already released as a beta!

For more info, see https://github.com/mdx-js/mdx/issues/1041.

Read more comments on GitHub >

github_iconTop Results From Across the Web

JS/JSX auto indent incorrectly when saving file - Stack Overflow
It shows below the improper indents in render function. import React from 'react'; import './Question_4.scss'; ...
Read more >
Migrating from v1 to v2 - MDX
That's because to parse markdown, we first have to divide it into “blocks”. So in this case two paragraphs and a heading. Leaving...
Read more >
CodeMirror: Release History - PASIORA
Fix mistakes in passing through the arguments to indent in several wrapping modes. javascript mode: Fix parsing for a number of new and...
Read more >
cdn.la.utexas.edu/codemirror/codemirror-5.32.0/CHA...
[markdown mode](http://codemirror.net/mode/markdown/): Don't block inner mode's indentation support. ## 5.27.2 (2017-06-22) ### Bug fixes Fix crash in the ...
Read more >
Prettier 2.3. In which assignments are consistent, short keys ...
This means Prettier won't format Handlebars files that can't be parsed into such a tree, either because the underlying syntax isn't HTML or ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found