mdx-js / mdx

Markdown for the component era
https://mdxjs.com
MIT License
17.77k stars 1.14k forks source link

need to set an option for `remark plugins` to be used before the "remark-mdx" in the pipeline #2236

Closed talatkuyuk closed 1 year ago

talatkuyuk commented 1 year ago

Initial checklist

Problem

In the remark eco system, some plugins process the character "<" or ">" or "<<" or ">>", in order to, for example, make guillemets turning "\<\<abc>>" to "«abc»" . link to remark-fix-guillemets (for guillemets there is need to use a remark-textr plugin also.)

Here is another issue for remark-smartypants the link

If the format is md, there is no problem. I can produce the quillemets. But, if the format is mdx, the string "\<\<abc>>" cause error in the mdx files.

I digged the issue. In the code of @mdx-js/mdx I found that: link

const pipeline = unified().use(remarkParse)

if (format !== 'md') {
  pipeline.use(remarkMdx)
}

pipeline
  .use(remarkMarkAndUnravel)
  .use(remarkPlugins || [])
  ...

Means that, remark-mdx comes prior to remarkPlugins and used just after remark-parse, and this causes the issue.

Solution

If the remark-mdx would used after the remarkPlugins in the pipeline, there would be no problem.

Alternatives

In this point, if it is not convenient to use the remark-mdx after the remarkPlugins, we can set another option like remarkPluginsBeforeMdx something like that, in order some plugins to be used before remark-mdx in the pipeline. In that case the plugins like remark-fix-guillemets and remark-smartypants could be useful even for mdx format markdown files.

wooorm commented 1 year ago

Hi Talat! This can’t happen, your solution/alternatives won’t work! Feel free to try it out locally, but here’s why:

a) Using the plugins you want before remark-mdx doesn‘t solve your problem, as it is a syntax extension, it changes how the parser works, and parsing always comes first. The other plugins are transform plugins, they work on the AST, they can’t work before parsing, because they need a parsed thing b) if this was possible, this would break several users of MDX, remark-mdx must come before other syntax extensions. c) remark-fix-guillemets can’t work with MDX, it depends on HTML nodes, MDX does not have HTML. d) it’s by design that < is a syntax error in these cases in MDX. You can escape them for MDX (\<\<). remark-smartypants will see it then.

Conceptually though, it’s really intentional that < is unambiguous in MDX and either starts JSX or causes a parse error.

talatkuyuk commented 1 year ago

Ohh.. thank you for quick reply.