|
| 1 | +--- |
| 2 | +id: plugins |
| 3 | +description: Using MDX plugins to expand Markdown functionality in CodeHarborHub |
| 4 | +slug: /markdown-features/plugins |
| 5 | +--- |
| 6 | + |
| 7 | +# MDX Plugins |
| 8 | + |
| 9 | +CodeHarborHub uses [MDX](https://mdxjs.com/) to power its documentation. Sometimes, you may want to extend or tweak Markdown syntax. For example: |
| 10 | + |
| 11 | +- Embed a YouTube video using image-like syntax: `` |
| 12 | +- Style standalone links as social cards |
| 13 | +- Automatically prepend a copyright notice to every page |
| 14 | + |
| 15 | +The answer is: **create an MDX plugin!** |
| 16 | +MDX supports a powerful [plugin system](https://mdxjs.com/advanced/plugins/) to customize how Markdown is parsed and transformed into JSX. |
| 17 | + |
| 18 | +There are three common plugin use-cases: |
| 19 | + |
| 20 | +- Use existing [Remark plugins](https://github.com/remarkjs/remark/blob/main/doc/plugins.md#list-of-plugins) or [Rehype plugins](https://github.com/rehypejs/rehype/blob/main/doc/plugins.md#list-of-plugins) |
| 21 | +- Transform elements produced by MDX syntax |
| 22 | +- Introduce **new syntax** to MDX |
| 23 | + |
| 24 | +<AdsComponent /> |
| 25 | + |
| 26 | +## How MDX Plugins Work |
| 27 | + |
| 28 | +When Markdown is compiled, it goes through two intermediate steps: |
| 29 | + |
| 30 | +1. **Markdown AST (MDAST)** |
| 31 | +2. **Hypertext AST (HAST)** |
| 32 | +3. JSX Output |
| 33 | + |
| 34 | +Plugins operate on these ASTs: |
| 35 | + |
| 36 | +* **[Remark](https://github.com/remarkjs/remark/)** → processes the Markdown AST |
| 37 | +* **[Rehype](https://github.com/rehypejs/rehype/)** → processes the Hypertext AST |
| 38 | + |
| 39 | +:::tip |
| 40 | +Use plugins to introduce shorter syntax for frequently used JSX elements. |
| 41 | +For example, CodeHarborHub’s [admonition blocks](./markdown-features-admonitions.mdx) are powered by a Remark plugin. |
| 42 | +::: |
| 43 | + |
| 44 | +<AdsComponent /> |
| 45 | + |
| 46 | +## Default Plugins |
| 47 | + |
| 48 | +Docusaurus automatically injects a set of default Remark plugins during Markdown processing. |
| 49 | +These handle tasks such as: |
| 50 | + |
| 51 | +- Generating a **Table of Contents** |
| 52 | +- Adding **anchor links** to each heading |
| 53 | +- Transforming images and links to `require()` calls |
| 54 | + |
| 55 | +These built-in plugins are great examples to inspire your own custom plugins. |
| 56 | + |
| 57 | +<AdsComponent /> |
| 58 | + |
| 59 | +## Installing Plugins |
| 60 | + |
| 61 | +An MDX plugin is usually an **npm package**, so install it like any other dependency. |
| 62 | + |
| 63 | +Example: adding math support: |
| 64 | + |
| 65 | +```bash npm2yarn |
| 66 | +npm install --save remark-math@5 rehype-katex@6 |
| 67 | +``` |
| 68 | + |
| 69 | +<details> |
| 70 | + <summary>Remark vs Rehype in action</summary> |
| 71 | + |
| 72 | +* `remark-math` extracts `$...$` math expressions from Markdown and transforms them into a JSX placeholder. |
| 73 | +* `rehype-katex` then takes those placeholders and renders them into KaTeX-powered HTML. |
| 74 | +</details> |
| 75 | + |
| 76 | +:::warning |
| 77 | +Many official Remark/Rehype plugins are **ES Modules only**. Docusaurus supports ESM, but we recommend using an **ESM config file** for easier imports. |
| 78 | +::: |
| 79 | + |
| 80 | +<AdsComponent /> |
| 81 | + |
| 82 | +## Adding Plugins to `docusaurus.config.js` |
| 83 | + |
| 84 | +Once installed, import and add the plugin to your config: |
| 85 | + |
| 86 | +```js title="docusaurus.config.js" |
| 87 | +// highlight-start |
| 88 | +import remarkMath from 'remark-math'; |
| 89 | +import rehypeKatex from 'rehype-katex'; |
| 90 | +// highlight-end |
| 91 | + |
| 92 | +export default { |
| 93 | + presets: [ |
| 94 | + [ |
| 95 | + '@docusaurus/preset-classic', |
| 96 | + { |
| 97 | + docs: { |
| 98 | + remarkPlugins: [remarkMath], |
| 99 | + rehypePlugins: [rehypeKatex], |
| 100 | + }, |
| 101 | + }, |
| 102 | + ], |
| 103 | + ], |
| 104 | +}; |
| 105 | +``` |
| 106 | + |
| 107 | +Using **CommonJS**? Dynamic imports make it work: |
| 108 | + |
| 109 | +```js title="docusaurus.config.js" |
| 110 | +module.exports = async function createConfigAsync() { |
| 111 | + return { |
| 112 | + presets: [ |
| 113 | + [ |
| 114 | + '@docusaurus/preset-classic', |
| 115 | + { |
| 116 | + docs: { |
| 117 | + remarkPlugins: [(await import('remark-math')).default], |
| 118 | + rehypePlugins: [(await import('rehype-katex')).default], |
| 119 | + }, |
| 120 | + }, |
| 121 | + ], |
| 122 | + ], |
| 123 | + }; |
| 124 | +}; |
| 125 | +``` |
| 126 | + |
| 127 | +--- |
| 128 | + |
| 129 | +## Configuring Plugins |
| 130 | + |
| 131 | +Some plugins accept custom options. Use `[plugin, options]` syntax: |
| 132 | + |
| 133 | +```js title="docusaurus.config.js" |
| 134 | +import rehypeKatex from 'rehype-katex'; |
| 135 | + |
| 136 | +export default { |
| 137 | + presets: [ |
| 138 | + [ |
| 139 | + '@docusaurus/preset-classic', |
| 140 | + { |
| 141 | + docs: { |
| 142 | + rehypePlugins: [ |
| 143 | + [rehypeKatex, {strict: false}], |
| 144 | + ], |
| 145 | + }, |
| 146 | + }, |
| 147 | + ], |
| 148 | + ], |
| 149 | +}; |
| 150 | +``` |
| 151 | + |
| 152 | +Check the plugin’s documentation for available options. |
| 153 | + |
| 154 | +<AdsComponent /> |
| 155 | + |
| 156 | +## Creating Your Own Plugin |
| 157 | + |
| 158 | +If no existing plugin fits your needs, you can create one. A plugin is a function that operates on the AST. |
| 159 | + |
| 160 | +Example: prefix every `h2` heading with `Section X.` |
| 161 | + |
| 162 | +```js title="src/remark/section-prefix.js" |
| 163 | +import {visit} from 'unist-util-visit'; |
| 164 | + |
| 165 | +const plugin = () => { |
| 166 | + return async (ast) => { |
| 167 | + let count = 1; |
| 168 | + visit(ast, 'heading', (node) => { |
| 169 | + if (node.depth === 2 && node.children.length > 0) { |
| 170 | + node.children.unshift({ |
| 171 | + type: 'text', |
| 172 | + value: `Section ${count}. `, |
| 173 | + }); |
| 174 | + count++; |
| 175 | + } |
| 176 | + }); |
| 177 | + }; |
| 178 | +}; |
| 179 | + |
| 180 | +export default plugin; |
| 181 | +``` |
| 182 | + |
| 183 | +Import and register it: |
| 184 | + |
| 185 | +```js title="docusaurus.config.js" |
| 186 | +import sectionPrefix from './src/remark/section-prefix'; |
| 187 | + |
| 188 | +export default { |
| 189 | + presets: [ |
| 190 | + [ |
| 191 | + '@docusaurus/preset-classic', |
| 192 | + { |
| 193 | + docs: { |
| 194 | + remarkPlugins: [sectionPrefix], |
| 195 | + }, |
| 196 | + }, |
| 197 | + ], |
| 198 | + ], |
| 199 | +}; |
| 200 | +``` |
| 201 | + |
| 202 | +:::tip |
| 203 | +The `transformer` function receives a second parameter, [`vfile`](https://github.com/vfile/vfile), which provides metadata like the current Markdown file’s path. |
| 204 | +::: |
| 205 | + |
| 206 | +<AdsComponent /> |
| 207 | + |
| 208 | +## Processing Order |
| 209 | + |
| 210 | +By default, Docusaurus runs its internal plugins **before** your custom plugins. If you need to run your plugin first: |
| 211 | + |
| 212 | +```js title="docusaurus.config.js" |
| 213 | +export default { |
| 214 | + presets: [ |
| 215 | + [ |
| 216 | + '@docusaurus/preset-classic', |
| 217 | + { |
| 218 | + docs: { |
| 219 | + beforeDefaultRemarkPlugins: [sectionPrefix], |
| 220 | + }, |
| 221 | + }, |
| 222 | + ], |
| 223 | + ], |
| 224 | +}; |
| 225 | +``` |
| 226 | + |
| 227 | +This ensures your transformations (like heading prefixes) are included in generated tables of contents. |
0 commit comments