mirror of
https://github.com/facebook/docusaurus.git
synced 2025-06-02 10:52:35 +02:00
refactor(mdx-loader): use vfile.path to access Markdown file path (#6443)
This commit is contained in:
parent
e40cafccd5
commit
3d7ba337c2
4 changed files with 48 additions and 22 deletions
|
@ -169,6 +169,26 @@ module.exports = {
|
|||
};
|
||||
```
|
||||
|
||||
:::tip
|
||||
|
||||
The `transformer` has a second parameter [`vfile`](https://github.com/vfile/vfile) which is useful if you need to access the current Markdown file's path.
|
||||
|
||||
```js
|
||||
const plugin = (options) => {
|
||||
const transformer = async (ast, vfile) => {
|
||||
ast.children.unshift({
|
||||
type: 'text',
|
||||
value: `The current file path is ${vfile.path}`,
|
||||
});
|
||||
};
|
||||
return transformer;
|
||||
};
|
||||
```
|
||||
|
||||
Our `transformImage` plugin uses this parameter, for example, to transform relative image references to `require()` calls.
|
||||
|
||||
:::
|
||||
|
||||
:::note
|
||||
|
||||
The default plugins of Docusaurus would operate before the custom remark plugins, and that means the images or links have been converted to JSX with `require()` calls already. For example, in the example above, the table of contents generated is still the same even when all `h2` headings are now prefixed by `Section X.`, because the TOC-generating plugin is called before our custom plugin. If you need to process the MDAST before the default plugins do, use the `beforeDefaultRemarkPlugins` and `beforeDefaultRehypePlugins`.
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue