-
-
Notifications
You must be signed in to change notification settings - Fork 8.7k
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
@docusuarus/mdx-loader
frontmatter removal prevents rehype vfile having correct line numbers
#3935
Comments
I agree that it is cumbersome not to have the frontmatter inside rehype/remark plugins. We have a different use case, we only want to check certain frontmatter values inside a remark plugin, but have no chance of doing that, without reading the file again. It would make sense to put Btw this is the updated source: docusaurus/packages/docusaurus-mdx-loader/src/loader.ts Lines 161 to 165 in 7ab2bd3
and docusaurus/packages/docusaurus-mdx-loader/src/loader.ts Lines 209 to 214 in 7ab2bd3
|
@vjpr as users are going to upgrade to Docusaurus v3 and MDX v2 soon they will likely encounter compilation errors and it's important to show correct line numbers if possible. In #9386 I'm now trying to avoid altering the original content before passing it to MDX, using @JPeer264 the front matter becomes available in the AST as raw string, with nodes of type yaml/toml.
I understand it's not super convenient to have to parse multiple times the same front matter, but it should unlock your use-case more efficiently now. In the future we'll try to optimize all this, because currently we already parse files at multiple layers (mdx loader and plugin code) and it's not super efficient. I'm not sure yet how to do that properly though. |
Nevermind, found a way to expose the const vfile = new VFile({
value: content,
path: filePath,
data: {
frontMatter
}
}); function remarkPlugin() {
return async (root, vfile) => {
console.log(vfile.data.frontMatter)
};
} |
docusaurus/packages/docusaurus-mdx-loader/src/index.js
Lines 24 to 58 in ae45b11
I am writing an plugin that adds the source file + line number to every heading element.
Below you can see that the frontmatter is being parsed and then only the remaining markdown content is passed onwards.
This means that in a rehype plugin the
vfile.history[].position.start.line
will not be correct, it not take into account the frontmatter lines.I think a better approach would be to parse the frontmatter using a remark plugin so to retain the original line numbers. Like this: https://www.npmjs.com/package/remark-extract-frontmatter
This should also be corrected on https://mdxjs.com/guides/custom-loader#custom-loader page I think.
Workaround
I could use grey matter to parse the gray matter again in my plugin but that means a lot of duplicate file read operations.
Remark
In remark plugins, I am just using the first comment to apply config.
The text was updated successfully, but these errors were encountered: