mdx-editor / editor

A rich text editor React component for markdown
https://mdxeditor.dev
MIT License
1.75k stars 139 forks source link

[BUG] Parsing of the following markdown structure failed: {"type":"code","name":"N/A"}. #370

Closed floatrx closed 6 months ago

floatrx commented 6 months ago

Description: When using the MDXEditor in source mode and attempting to insert a code block using triple backticks (```), the editor fails to parse the markdown structure, resulting in a parsing error. This issue prevents users from adding code snippets to their markdown documents directly in the source mode.

Error Message: Parsing of the following markdown structure failed: {"type":"code","name":"N/A"}. Steps to Reproduce:

Steps 2 reproduce

Expected Behavior: The editor should correctly parse the code block enclosed in triple backticks, allowing the user to insert code snippets into their markdown document without encountering a parsing error.

Actual Behavior: The editor displays a parsing error when attempting to insert a code block using triple backticks, preventing the user from successfully adding code snippets in source mode.

petyosi commented 6 months ago

My guess is that you need to configure the codeMirrorPlugin to support code blocks without any language specified, by passing a '': 'Text' to the codeBlockLanguages. Give it a try.

floatrx commented 6 months ago

My guess is that you need to configure the codeMirrorPlugin to support code blocks without any language specified, by passing a '': 'Text' to the codeBlockLanguages. Give it a try.

I don't use the codeMirrorPlugin, and it's unnecessary for my purposes. Markdown inherently supports code blocks without the requirement to specify a language.

This bug was also reproduced in the official live demo

Screenshot 2024-03-01 at 17 16 57 Screenshot 2024-03-01 at 17 17 33

This issue is not critical but it would be nice to have it fixed.

petyosi commented 6 months ago

Perhaps my response was not clear. The editor supports code blocks without a language, but you need to configure it so that it actually knows what kind of editor to associate with such a code block. I suggested the codeMirror one, but you can also use whatever you wish, the docs include an example with a text area.

wp0403 commented 6 months ago

I have encountered the same error as you, and need to configure all the code block languages you use in markdown to codeMirrorPlugin

image image
floatrx commented 6 months ago

@wp0403 Helps, but we should explicitly provide an empty string ('')


    const codeBlockLanguages = ['', ...]; // <- here                                           
    ...
    codeMirrorPlugin({
      codeBlockLanguages: codeBlockLanguages.reduce((acc, language) => {
        acc[language] = language;
        return acc;
      }, {}),
    }),```
pbjorklund commented 3 months ago

My guess is that you need to configure the codeMirrorPlugin to support code blocks without any language specified, by passing a '': 'Text' to the codeBlockLanguages. Give it a try.

This would be great to have in the example in the docs.