-
-
Notifications
You must be signed in to change notification settings - Fork 8.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
RFC: CommonMark compatibility, supporting multiple markdown/content parsers #3018
Comments
Sidenote: Edit: I think it will still work because it's processed before mdx compilation |
That sounds excellent! Thinking about the common abstraction you mentioned, and assuming that this is still the overarching goal:
I think that Docusaurus could document an interface for plugins / formats / loaders (I don't know how to call them) that could possibly look like this:
Some wilders use cases this would cover (I actually had them in the past):
For this RFC, I think it's more than enough to support CommonMark but since I've now spent some time thinking about how we'd use Docusaurus and what I'd love it to allow me to do, I thought I'd post it here. Thanks a lot for this RFC and all the work that goes into Docusaurus! |
@slorber I'd like to create a prototype of CommonMark support but am unfamiliar with Docusaurus codebase so would really appreciate high-level guidelines if you will. Roughly speaking, if I wanted to parse Any hints appreciated 🙏 . |
Hi, My first intuition would be to modify "docusaurus-mdx-loader", and provide a loader option to tell it to load the files as md or mdx. In the end, we need a React component anyway, using MDX, but in md mode, we could convert the html elements to JSX elements just before feeding mdx, so that mdx is happy? Not sure, this would require some experiments to see if this is possible |
Thanks a lot, I'll give it a go later this week or the next one. |
plugin-content-docs that supports CommonMark for
|
An alternative approach would be to convert MD to MDX first and then just let the |
thanks for those details, that looks interesting. If MDX provided a converter that would be great, also would helpful for v1->v2 migrations I don't have much time to explore these ideas but we'll come back to it someday. Note, not sure it's related, but there's a large docs plugin refactor here: #3245 |
Is it possible to have something simple, which works out of the box. I need math blocks, I see MDX documentation, it's too messy and complicated. Docusaurus seems to work on the basic assumptions or at-least targeted or Is there a simple way to get math blocks support. Thank you. |
@nilsocket I don't think math blocks (latex/katex?) are really related to the markdown parser. But you are right, and we should make this easy. Can you explain better your usecase on this new issue I just created? #4625 |
@slorber is there an official way of handling this? I have a similar situation where I don't want my .md files validated with .mdx. |
@lukejgaskell unfortunately no easy solution can be implemented in userland to solve this properly. MDX is not a "validator" for md files, it converts those files to React components that are loaded as JS modules in the client app through webpack loaders. To make this compatible with CommonMark, this would require the loader to not use MDX in some cases but use a different Remark parsing logic. For
Some challenges to consider:
This is something I want to work on but I don't have time in the short term. |
@slorber That makes sense, thank you for the detailed explanation. If it's helpful, my use case is that I'm importing markdown from different sources to host on a single site. That markdown may or may not follow the same syntax as the current loader. For example, some of it uses Usually it ends up breaking in the build (because of the mdx loader) even though I'd like it to just show a broken file in those scenarios. Anyways, here's the regex I end up doing to solve some of this: const replaceLT = (m, group1) => (!group1 ? m : "<");
const replaceGT = (m, group1) => (!group1 ? m : ">");
const replaceFileLink = (m) => m.replace("(", "(pathname://");
async function run() {
await replace({
files: ["docs/**/*.md"],
from: [
/<pre>/g,
/<\/pre>/g,
/<!--.*-->/g,
/\[.*?\]\(.*?\.(json|xlsx|xls|zip|docx|ps1)\)/g, // fix file type links to not be picked up by loader
/\\`|`(?:\\`|[^`])*`|(<)/gm, //find all less than symbols that are not between backticks
/\\`|`(?:\\`|[^`])*`|(>)/gm, //find all greater than symbols that are not between backticks
],
to: ["```", "```", "", replaceFileLink, replaceLT, replaceGT],
});
} |
@zepatrik If you want to do post-processing, don't sanitize code in code blocks. Also, you can use a remark plugin to strip imports/exports very easily. Apart from import/exports, MDX can't execute arbitrary code. |
Can you elaborate on that? I can easily run arbitrary javascript on the MDX playground using e.g.
Of course with that, I could e.g. leak stuff from local storage to one of my servers or do all kinds of things. |
what is the status on this? does docusaurus 2 split |
@timothyerwin all the updates are here, it's not necessary to ask. Docusaurus is based on MDX, and you have to make sure your docs are compatible. This might require editing some of them, particularly HTML tags so that they conform with JSX. |
I also have the same problem, the people who write the documents are not proficient in React. Then the official provided automatic migration script cannot convert markdown to mdx format very well. Is there a way to specify that files with .mdx extension use docusaurus/mdx-loader, while files with .md extension use version 1.0 of the markdown renderer? Looking forward to your reply. |
With the upcoming Docusaurus 3, we upgrade to MDX 2 (#8288), and there's a Note: the content is parsed as CommonMark, and it's not possible to use JSX inside that content anymore, but you can start using raw html and inline styles like on GitHub (enabled by #8960), but under the hood, the content is still compiled as a React component. Features such as admonitions, code blocks etc keep working. If you want early access to these features, use a canary version of Docusaurus and follow what's written in this PR to turn on CommonMark: #8288 (for now just having |
Can we have an option to disable commonmark? This is creating a lot of issues when I just want to use React 18. |
@ntucker I was going to add a global Note: you can use |
Altering every single file when the last edit time is used in the final site for publish time is not exciting to me. However, I'm very glad to hear about upcoming global control! |
Note: the new CommonMark mode will be probably marked as experimental in v3.0 and opt-in. The basic rendering works fine, but it is currently missing some Docusaurus features. |
If you're coming from the blog and want to opt into CommonMark, use |
- Prism themes for code syntax highlight - Treat .md as Markdown and .mdx as MDX facebook/docusaurus#3018
💥 Proposal
People using Docusaurus don't always like the MDX parser:
Related discussions:
Solution ?
These libs:
We may be able to build some shared abstraction on top of react-markdown + MDX.
If this works, we could switch from one parser to another with a simple switch/setting, that could be:
The idea would be that, if a doc does not embed any html/jsx, we could switch from one parser to the other, and shouldn't notice any change.
--
Feedbacks welcome
The text was updated successfully, but these errors were encountered: