@astrojs/mdx 0.11.4 → 0.11.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.turbo/turbo-build.log +5 -5
- package/CHANGELOG.md +12 -0
- package/README.md +27 -14
- package/dist/index.d.ts +1 -0
- package/dist/index.js +5 -1
- package/dist/plugins.js +2 -0
- package/dist/rehype-meta-string.d.ts +6 -0
- package/dist/rehype-meta-string.js +15 -0
- package/package.json +7 -5
- package/src/index.ts +7 -2
- package/src/plugins.ts +3 -0
- package/src/rehype-meta-string.ts +17 -0
- package/test/fixtures/mdx-plugins/src/pages/with-plugins.mdx +4 -0
- package/test/fixtures/mdx-syntax-hightlighting/src/pages/index.mdx +1 -1
- package/test/mdx-plugins.test.js +38 -0
- package/test/mdx-syntax-highlighting.test.js +28 -0
package/.turbo/turbo-build.log
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
[
|
|
2
|
-
[
|
|
3
|
-
[
|
|
4
|
-
[
|
|
5
|
-
[
|
|
1
|
+
[35m@astrojs/mdx:build: [0mcache hit, replaying output [2m788a04a94f8adf80[0m
|
|
2
|
+
[35m@astrojs/mdx:build: [0m
|
|
3
|
+
[35m@astrojs/mdx:build: [0m> @astrojs/mdx@0.11.6 build /home/runner/work/astro/astro/packages/integrations/mdx
|
|
4
|
+
[35m@astrojs/mdx:build: [0m> astro-scripts build "src/**/*.ts" && tsc
|
|
5
|
+
[35m@astrojs/mdx:build: [0m
|
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,17 @@
|
|
|
1
1
|
# @astrojs/mdx
|
|
2
2
|
|
|
3
|
+
## 0.11.6
|
|
4
|
+
|
|
5
|
+
### Patch Changes
|
|
6
|
+
|
|
7
|
+
- [#5335](https://github.com/withastro/astro/pull/5335) [`dca762cf7`](https://github.com/withastro/astro/commit/dca762cf734a657d8f126fd6958892b6163a4f67) Thanks [@bluwy](https://github.com/bluwy)! - Preserve code element node `data.meta` in `properties.metastring` for rehype syntax highlighters, like `rehype-pretty-code``
|
|
8
|
+
|
|
9
|
+
## 0.11.5
|
|
10
|
+
|
|
11
|
+
### Patch Changes
|
|
12
|
+
|
|
13
|
+
- [#5146](https://github.com/withastro/astro/pull/5146) [`308e565ad`](https://github.com/withastro/astro/commit/308e565ad39957e3353d72ca5d3bbce1a1b45008) Thanks [@bholmesdev](https://github.com/bholmesdev)! - Support recmaPlugins config option
|
|
14
|
+
|
|
3
15
|
## 0.11.4
|
|
4
16
|
|
|
5
17
|
### Patch Changes
|
package/README.md
CHANGED
|
@@ -71,11 +71,12 @@ Finally, restart the dev server.
|
|
|
71
71
|
|
|
72
72
|
## Usage
|
|
73
73
|
|
|
74
|
-
You can [add MDX pages to your project](https://docs.astro.build/en/guides/markdown-content/#markdown-and-mdx-pages) by adding `.mdx` files within your `src/pages/` directory.
|
|
74
|
+
You can [add MDX pages to your project](https://docs.astro.build/en/guides/markdown-content/#markdown-and-mdx-pages) by adding `.mdx` files within your `src/pages/` directory.
|
|
75
75
|
|
|
76
76
|
### Components
|
|
77
77
|
|
|
78
78
|
To use components in your MDX pages in Astro, head to our [UI framework documentation][astro-ui-frameworks]. You'll explore:
|
|
79
|
+
|
|
79
80
|
- 📦 how framework components are loaded,
|
|
80
81
|
- 💧 client-side hydration options, and
|
|
81
82
|
- 🤝 opportunities to mix and nest frameworks together
|
|
@@ -160,7 +161,7 @@ const posts = await Astro.glob('./*.mdx');
|
|
|
160
161
|
|
|
161
162
|
### Inject frontmatter via remark or rehype plugins
|
|
162
163
|
|
|
163
|
-
You may want to inject frontmatter properties across all of your MDX files. By using a [remark](#
|
|
164
|
+
You may want to inject frontmatter properties across all of your MDX files. By using a [remark](#remarkplugins) or [rehype](#rehypeplugins) plugin, you can generate these properties based on a file’s contents.
|
|
164
165
|
|
|
165
166
|
You can append to the `data.astro.frontmatter` property from your plugin’s `file` argument like so:
|
|
166
167
|
|
|
@@ -193,8 +194,7 @@ export default {
|
|
|
193
194
|
…every MDX file will have `customProperty` in its frontmatter! See [our Markdown documentation](https://docs.astro.build/en/guides/markdown-content/#injecting-frontmatter) for more usage instructions and a [reading time plugin example](https://docs.astro.build/en/guides/markdown-content/#example-calculate-reading-time).
|
|
194
195
|
|
|
195
196
|
### Layouts
|
|
196
|
-
|
|
197
|
-
Layouts can be applied [in the same way as standard Astro Markdown](https://docs.astro.build/en/guides/markdown-content/#markdown-layouts). You can add a `layout` to [your frontmatter](#frontmatter) like so:
|
|
197
|
+
Layouts can be applied [in the same way as standard Astro Markdown](https://docs.astro.build/en/guides/markdown-content/#frontmatter-layout). You can add a `layout` to [your frontmatter](#frontmatter) like so:
|
|
198
198
|
|
|
199
199
|
```yaml
|
|
200
200
|
---
|
|
@@ -260,6 +260,7 @@ const { frontmatter, url } = Astro.props;
|
|
|
260
260
|
#### Layout props
|
|
261
261
|
|
|
262
262
|
All [exported properties](#exported-properties) are available from `Astro.props` in your layout, **with two key differences:**
|
|
263
|
+
|
|
263
264
|
- Heading information (i.e. `h1 -> h6` elements) is available via the `headings` array, rather than a `getHeadings()` function.
|
|
264
265
|
- `file` and `url` are _also_ available as nested `frontmatter` properties (i.e. `frontmatter.url` and `frontmatter.file`). This is consistent with Astro's Markdown layout properties.
|
|
265
266
|
|
|
@@ -286,6 +287,7 @@ function fancyJsHelper() {
|
|
|
286
287
|
Welcome to my new Astro blog, using MDX!
|
|
287
288
|
</BaseLayout>
|
|
288
289
|
```
|
|
290
|
+
|
|
289
291
|
Then, your values are available to you through `Astro.props` in your layout, and your MDX content will be injected into the page where your `<slot />` component is written:
|
|
290
292
|
|
|
291
293
|
```astro
|
|
@@ -319,13 +321,17 @@ will be converted into this HTML:
|
|
|
319
321
|
But what if you want to specify your own markup for these blockquotes? In the above example, you could create a custom `<Blockquote />` component (in any language) that either has a `<slot />` component or accepts a `children` prop.
|
|
320
322
|
|
|
321
323
|
```astro title="src/components/Blockquote.astro"
|
|
322
|
-
|
|
324
|
+
---
|
|
325
|
+
const props = Astro.props;
|
|
326
|
+
---
|
|
327
|
+
|
|
328
|
+
<blockquote {...props} class="bg-blue-50 p-4">
|
|
323
329
|
<span class="text-4xl text-blue-600 mb-2">“</span>
|
|
324
330
|
<slot />
|
|
325
331
|
</blockquote>
|
|
326
332
|
```
|
|
327
333
|
|
|
328
|
-
Then in the MDX file you import the component and export it to the `components` export.
|
|
334
|
+
Then in the MDX file you import the component and export it to the `components` export.
|
|
329
335
|
|
|
330
336
|
```mdx title="src/pages/posts/post-1.mdx" {2}
|
|
331
337
|
import Blockquote from '../components/Blockquote.astro';
|
|
@@ -334,18 +340,19 @@ export const components = { blockquote: Blockquote };
|
|
|
334
340
|
|
|
335
341
|
Now, writing the standard Markdown blockquote syntax (`>`) will use your custom `<Blockquote />` component instead. No need to use a component in Markdown, or write a remark/rehype plugin! Visit the [MDX website](https://mdxjs.com/table-of-components/) for a full list of HTML elements that can be overwritten as custom components.
|
|
336
342
|
|
|
337
|
-
|
|
338
343
|
#### Custom components with imported `mdx`
|
|
339
344
|
|
|
340
|
-
When rendering imported MDX content, custom components can
|
|
345
|
+
When rendering imported MDX content, custom components can be passed via the `components` prop.
|
|
346
|
+
|
|
347
|
+
Note: An MDX file's exported components will _not_ be used unless you manually import and pass them via the `components` property. See the example below:
|
|
341
348
|
|
|
342
|
-
```astro title="src/pages/page.astro" "components={{ h1: Heading }}"
|
|
349
|
+
```astro title="src/pages/page.astro" "components={{...components, h1: Heading }}"
|
|
343
350
|
---
|
|
344
|
-
import Content from '../content.mdx';
|
|
351
|
+
import { Content, components } from '../content.mdx';
|
|
345
352
|
import Heading from '../Heading.astro';
|
|
346
353
|
---
|
|
347
354
|
|
|
348
|
-
<Content components={{ h1: Heading }} />
|
|
355
|
+
<Content components={{...components, h1: Heading }} />
|
|
349
356
|
```
|
|
350
357
|
|
|
351
358
|
### Syntax highlighting
|
|
@@ -407,7 +414,7 @@ export default {
|
|
|
407
414
|
|
|
408
415
|
[Remark plugins](https://github.com/remarkjs/remark/blob/main/doc/plugins.md) allow you to extend your Markdown with new capabilities. This includes [auto-generating a table of contents](https://github.com/remarkjs/remark-toc), [applying accessible emoji labels](https://github.com/florianeckerstorfer/remark-a11y-emoji), and more. We encourage you to browse [awesome-remark](https://github.com/remarkjs/awesome-remark) for a full curated list!
|
|
409
416
|
|
|
410
|
-
This example applies the [`remark-toc`](https://github.com/remarkjs/remark-toc) plugin to `.mdx` files. To customize plugin inheritance from your Markdown config or Astro's defaults, [see the `extendPlugins` option](#
|
|
417
|
+
This example applies the [`remark-toc`](https://github.com/remarkjs/remark-toc) plugin to `.mdx` files. To customize plugin inheritance from your Markdown config or Astro's defaults, [see the `extendPlugins` option](#extendplugins).
|
|
411
418
|
|
|
412
419
|
```js
|
|
413
420
|
// astro.config.mjs
|
|
@@ -426,7 +433,7 @@ export default {
|
|
|
426
433
|
|
|
427
434
|
We apply our own (non-removable) [`collect-headings`](https://github.com/withastro/astro/blob/main/packages/integrations/mdx/src/rehype-collect-headings.ts) plugin. This applies IDs to all headings (i.e. `h1 -> h6`) in your MDX files to [link to headings via anchor tags](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/a#linking_to_an_element_on_the_same_page).
|
|
428
435
|
|
|
429
|
-
This example applies the [`rehype-minify`](https://github.com/rehypejs/rehype-minify) plugin to `.mdx` files. To customize plugin inheritance from your Markdown config or Astro's defaults, [see the `extendPlugins` option](#
|
|
436
|
+
This example applies the [`rehype-minify`](https://github.com/rehypejs/rehype-minify) plugin to `.mdx` files. To customize plugin inheritance from your Markdown config or Astro's defaults, [see the `extendPlugins` option](#extendplugins).
|
|
430
437
|
|
|
431
438
|
```js
|
|
432
439
|
// astro.config.mjs
|
|
@@ -447,7 +454,7 @@ export default {
|
|
|
447
454
|
|
|
448
455
|
#### `markdown` (default)
|
|
449
456
|
|
|
450
|
-
By default, Astro inherits all [remark](#
|
|
457
|
+
By default, Astro inherits all [remark](#remarkplugins) and [rehype](#rehypeplugins) plugins from [the `markdown` option in your Astro config](https://docs.astro.build/en/guides/markdown-content/#markdown-plugins). This also respects the [`markdown.extendDefaultPlugins`](https://docs.astro.build/en/reference/configuration-reference/#markdownextenddefaultplugins) option to extend Astro's defaults. Any additional plugins you apply in your MDX config will be applied _after_ your configured Markdown plugins.
|
|
451
458
|
|
|
452
459
|
This example applies [`remark-toc`](https://github.com/remarkjs/remark-toc) to Markdown _and_ MDX, and [`rehype-minify`](https://github.com/rehypejs/rehype-minify) to MDX alone:
|
|
453
460
|
|
|
@@ -505,6 +512,12 @@ export default {
|
|
|
505
512
|
}
|
|
506
513
|
```
|
|
507
514
|
|
|
515
|
+
### recmaPlugins
|
|
516
|
+
|
|
517
|
+
These are plugins that modify the output [estree](https://github.com/estree/estree) directly. This is useful for modifying or injecting JavaScript variables in your MDX files.
|
|
518
|
+
|
|
519
|
+
We suggest [using AST Explorer](https://astexplorer.net/) to play with estree outputs, and trying [`estree-util-visit`](https://unifiedjs.com/explore/package/estree-util-visit/) for searching across JavaScript nodes.
|
|
520
|
+
|
|
508
521
|
## Examples
|
|
509
522
|
|
|
510
523
|
- The [Astro MDX example](https://github.com/withastro/astro/tree/latest/examples/with-mdx) shows how to use MDX files in your Astro project.
|
package/dist/index.d.ts
CHANGED
package/dist/index.js
CHANGED
|
@@ -37,6 +37,7 @@ function mdx(mdxOptions = {}) {
|
|
|
37
37
|
const mdxPluginOpts = {
|
|
38
38
|
remarkPlugins: await getRemarkPlugins(mdxOptions, config),
|
|
39
39
|
rehypePlugins: getRehypePlugins(mdxOptions, config),
|
|
40
|
+
recmaPlugins: mdxOptions.recmaPlugins,
|
|
40
41
|
jsx: true,
|
|
41
42
|
jsxImportSource: "astro",
|
|
42
43
|
format: "mdx",
|
|
@@ -66,7 +67,10 @@ function mdx(mdxOptions = {}) {
|
|
|
66
67
|
...mdxPluginOpts.rehypePlugins ?? [],
|
|
67
68
|
() => rehypeApplyFrontmatterExport(frontmatter)
|
|
68
69
|
],
|
|
69
|
-
recmaPlugins: [
|
|
70
|
+
recmaPlugins: [
|
|
71
|
+
...mdxPluginOpts.recmaPlugins ?? [],
|
|
72
|
+
() => recmaInjectImportMetaEnvPlugin({ importMetaEnv })
|
|
73
|
+
]
|
|
70
74
|
});
|
|
71
75
|
return {
|
|
72
76
|
code: escapeViteEnvReferences(String(compiled.value)),
|
package/dist/plugins.js
CHANGED
|
@@ -5,6 +5,7 @@ import rehypeRaw from "rehype-raw";
|
|
|
5
5
|
import remarkGfm from "remark-gfm";
|
|
6
6
|
import remarkSmartypants from "remark-smartypants";
|
|
7
7
|
import rehypeCollectHeadings from "./rehype-collect-headings.js";
|
|
8
|
+
import rehypeMetaString from "./rehype-meta-string.js";
|
|
8
9
|
import remarkPrism from "./remark-prism.js";
|
|
9
10
|
import remarkShiki from "./remark-shiki.js";
|
|
10
11
|
import { jsToTreeNode } from "./utils.js";
|
|
@@ -119,6 +120,7 @@ async function getRemarkPlugins(mdxOptions, config) {
|
|
|
119
120
|
function getRehypePlugins(mdxOptions, config) {
|
|
120
121
|
let rehypePlugins = [
|
|
121
122
|
rehypeCollectHeadings,
|
|
123
|
+
rehypeMetaString,
|
|
122
124
|
[rehypeRaw, { passThrough: nodeTypes }]
|
|
123
125
|
];
|
|
124
126
|
switch (mdxOptions.extendPlugins) {
|
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Moves `data.meta` to `properties.metastring` for the `code` element node
|
|
3
|
+
* as `rehype-raw` strips `data` from all nodes, which may contain useful information.
|
|
4
|
+
* e.g. ```js {1:3} => metastring: "{1:3}"
|
|
5
|
+
*/
|
|
6
|
+
export default function rehypeMetaString(): (tree: any) => void;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { visit } from "unist-util-visit";
|
|
2
|
+
function rehypeMetaString() {
|
|
3
|
+
return function(tree) {
|
|
4
|
+
visit(tree, (node) => {
|
|
5
|
+
var _a;
|
|
6
|
+
if (node.type === "element" && node.tagName === "code" && ((_a = node.data) == null ? void 0 : _a.meta)) {
|
|
7
|
+
node.properties ?? (node.properties = {});
|
|
8
|
+
node.properties.metastring = node.data.meta;
|
|
9
|
+
}
|
|
10
|
+
});
|
|
11
|
+
};
|
|
12
|
+
}
|
|
13
|
+
export {
|
|
14
|
+
rehypeMetaString as default
|
|
15
|
+
};
|
package/package.json
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@astrojs/mdx",
|
|
3
3
|
"description": "Use MDX within Astro",
|
|
4
|
-
"version": "0.11.
|
|
4
|
+
"version": "0.11.6",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"types": "./dist/index.d.ts",
|
|
7
7
|
"author": "withastro",
|
|
@@ -23,7 +23,7 @@
|
|
|
23
23
|
"./package.json": "./package.json"
|
|
24
24
|
},
|
|
25
25
|
"dependencies": {
|
|
26
|
-
"@astrojs/prism": "^1.0.
|
|
26
|
+
"@astrojs/prism": "^1.0.2",
|
|
27
27
|
"@mdx-js/mdx": "^2.1.2",
|
|
28
28
|
"@mdx-js/rollup": "^2.1.1",
|
|
29
29
|
"acorn": "^8.8.0",
|
|
@@ -46,8 +46,8 @@
|
|
|
46
46
|
"@types/github-slugger": "^1.3.0",
|
|
47
47
|
"@types/mocha": "^9.1.1",
|
|
48
48
|
"@types/yargs-parser": "^21.0.0",
|
|
49
|
-
"astro": "1.
|
|
50
|
-
"astro-scripts": "0.0.
|
|
49
|
+
"astro": "1.6.6",
|
|
50
|
+
"astro-scripts": "0.0.9",
|
|
51
51
|
"chai": "^4.3.6",
|
|
52
52
|
"cheerio": "^1.0.0-rc.11",
|
|
53
53
|
"linkedom": "^0.14.12",
|
|
@@ -55,6 +55,7 @@
|
|
|
55
55
|
"mdast-util-to-string": "^3.1.0",
|
|
56
56
|
"mocha": "^9.2.2",
|
|
57
57
|
"reading-time": "^1.5.0",
|
|
58
|
+
"rehype-pretty-code": "^0.4.0",
|
|
58
59
|
"remark-shiki-twoslash": "^3.1.0",
|
|
59
60
|
"remark-toc": "^8.0.1",
|
|
60
61
|
"vite": "^3.0.0"
|
|
@@ -66,6 +67,7 @@
|
|
|
66
67
|
"build": "astro-scripts build \"src/**/*.ts\" && tsc",
|
|
67
68
|
"build:ci": "astro-scripts build \"src/**/*.ts\"",
|
|
68
69
|
"dev": "astro-scripts dev \"src/**/*.ts\"",
|
|
69
|
-
"test": "mocha --exit --timeout 20000"
|
|
70
|
+
"test": "mocha --exit --timeout 20000",
|
|
71
|
+
"test:match": "mocha --timeout 20000 -g"
|
|
70
72
|
}
|
|
71
73
|
}
|
package/src/index.ts
CHANGED
|
@@ -24,6 +24,7 @@ const COMPILED_CONTENT_ERROR =
|
|
|
24
24
|
export type MdxOptions = {
|
|
25
25
|
remarkPlugins?: PluggableList;
|
|
26
26
|
rehypePlugins?: PluggableList;
|
|
27
|
+
recmaPlugins?: PluggableList;
|
|
27
28
|
/**
|
|
28
29
|
* Choose which remark and rehype plugins to inherit, if any.
|
|
29
30
|
*
|
|
@@ -64,9 +65,10 @@ export default function mdx(mdxOptions: MdxOptions = {}): AstroIntegration {
|
|
|
64
65
|
const mdxPluginOpts: MdxRollupPluginOptions = {
|
|
65
66
|
remarkPlugins: await getRemarkPlugins(mdxOptions, config),
|
|
66
67
|
rehypePlugins: getRehypePlugins(mdxOptions, config),
|
|
68
|
+
recmaPlugins: mdxOptions.recmaPlugins,
|
|
67
69
|
jsx: true,
|
|
68
70
|
jsxImportSource: 'astro',
|
|
69
|
-
// Note: disable `.md` support
|
|
71
|
+
// Note: disable `.md` (and other alternative extensions for markdown files like `.markdown`) support
|
|
70
72
|
format: 'mdx',
|
|
71
73
|
mdExtensions: [],
|
|
72
74
|
};
|
|
@@ -100,7 +102,10 @@ export default function mdx(mdxOptions: MdxOptions = {}): AstroIntegration {
|
|
|
100
102
|
...(mdxPluginOpts.rehypePlugins ?? []),
|
|
101
103
|
() => rehypeApplyFrontmatterExport(frontmatter),
|
|
102
104
|
],
|
|
103
|
-
recmaPlugins: [
|
|
105
|
+
recmaPlugins: [
|
|
106
|
+
...(mdxPluginOpts.recmaPlugins ?? []),
|
|
107
|
+
() => recmaInjectImportMetaEnvPlugin({ importMetaEnv }),
|
|
108
|
+
],
|
|
104
109
|
});
|
|
105
110
|
|
|
106
111
|
return {
|
package/src/plugins.ts
CHANGED
|
@@ -11,6 +11,7 @@ import remarkSmartypants from 'remark-smartypants';
|
|
|
11
11
|
import type { Data, VFile } from 'vfile';
|
|
12
12
|
import { MdxOptions } from './index.js';
|
|
13
13
|
import rehypeCollectHeadings from './rehype-collect-headings.js';
|
|
14
|
+
import rehypeMetaString from './rehype-meta-string.js';
|
|
14
15
|
import remarkPrism from './remark-prism.js';
|
|
15
16
|
import remarkShiki from './remark-shiki.js';
|
|
16
17
|
import { jsToTreeNode } from './utils.js';
|
|
@@ -150,6 +151,8 @@ export function getRehypePlugins(
|
|
|
150
151
|
let rehypePlugins: PluggableList = [
|
|
151
152
|
// getHeadings() is guaranteed by TS, so we can't allow user to override
|
|
152
153
|
rehypeCollectHeadings,
|
|
154
|
+
// ensure `data.meta` is preserved in `properties.metastring` for rehype syntax highlighters
|
|
155
|
+
rehypeMetaString,
|
|
153
156
|
// rehypeRaw allows custom syntax highlighters to work without added config
|
|
154
157
|
[rehypeRaw, { passThrough: nodeTypes }] as any,
|
|
155
158
|
];
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
import { visit } from 'unist-util-visit';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Moves `data.meta` to `properties.metastring` for the `code` element node
|
|
5
|
+
* as `rehype-raw` strips `data` from all nodes, which may contain useful information.
|
|
6
|
+
* e.g. ```js {1:3} => metastring: "{1:3}"
|
|
7
|
+
*/
|
|
8
|
+
export default function rehypeMetaString() {
|
|
9
|
+
return function (tree: any) {
|
|
10
|
+
visit(tree, (node) => {
|
|
11
|
+
if (node.type === 'element' && node.tagName === 'code' && node.data?.meta) {
|
|
12
|
+
node.properties ??= {};
|
|
13
|
+
node.properties.metastring = node.data.meta;
|
|
14
|
+
}
|
|
15
|
+
});
|
|
16
|
+
};
|
|
17
|
+
}
|
|
@@ -1,3 +1,5 @@
|
|
|
1
|
+
export let recmaPluginWorking = false
|
|
2
|
+
|
|
1
3
|
# TOC test
|
|
2
4
|
|
|
3
5
|
## Table of contents
|
|
@@ -17,3 +19,5 @@ Oh cool, more text!
|
|
|
17
19
|
## Section 2
|
|
18
20
|
|
|
19
21
|
And section 2, with a hyperlink to check GFM is preserved: https://handle-me-gfm.com
|
|
22
|
+
|
|
23
|
+
<div data-recma-plugin-works={recmaPluginWorking}></div>
|
package/test/mdx-plugins.test.js
CHANGED
|
@@ -4,6 +4,7 @@ import { expect } from 'chai';
|
|
|
4
4
|
import { parseHTML } from 'linkedom';
|
|
5
5
|
import { loadFixture } from '../../../astro/test/test-utils.js';
|
|
6
6
|
import remarkToc from 'remark-toc';
|
|
7
|
+
import { visit as estreeVisit } from 'estree-util-visit';
|
|
7
8
|
|
|
8
9
|
const FIXTURE_ROOT = new URL('./fixtures/mdx-plugins/', import.meta.url);
|
|
9
10
|
const FILE = '/with-plugins/index.html';
|
|
@@ -164,6 +165,21 @@ describe('MDX plugins', () => {
|
|
|
164
165
|
expect(selectGfmLink(document)).to.be.null;
|
|
165
166
|
expect(selectRemarkExample(document)).to.be.null;
|
|
166
167
|
});
|
|
168
|
+
|
|
169
|
+
it('supports custom recma plugins', async () => {
|
|
170
|
+
const fixture = await buildFixture({
|
|
171
|
+
integrations: [
|
|
172
|
+
mdx({
|
|
173
|
+
recmaPlugins: [recmaExamplePlugin],
|
|
174
|
+
}),
|
|
175
|
+
],
|
|
176
|
+
});
|
|
177
|
+
|
|
178
|
+
const html = await fixture.readFile(FILE);
|
|
179
|
+
const { document } = parseHTML(html);
|
|
180
|
+
|
|
181
|
+
expect(selectRecmaExample(document)).to.not.be.null;
|
|
182
|
+
});
|
|
167
183
|
});
|
|
168
184
|
|
|
169
185
|
async function buildFixture(config) {
|
|
@@ -194,6 +210,24 @@ function rehypeExamplePlugin() {
|
|
|
194
210
|
};
|
|
195
211
|
}
|
|
196
212
|
|
|
213
|
+
function recmaExamplePlugin() {
|
|
214
|
+
return (tree) => {
|
|
215
|
+
estreeVisit(tree, (node) => {
|
|
216
|
+
if (
|
|
217
|
+
node.type === 'VariableDeclarator' &&
|
|
218
|
+
node.id.name === 'recmaPluginWorking' &&
|
|
219
|
+
node.init?.type === 'Literal'
|
|
220
|
+
) {
|
|
221
|
+
node.init = {
|
|
222
|
+
...(node.init ?? {}),
|
|
223
|
+
value: true,
|
|
224
|
+
raw: 'true',
|
|
225
|
+
};
|
|
226
|
+
}
|
|
227
|
+
});
|
|
228
|
+
};
|
|
229
|
+
}
|
|
230
|
+
|
|
197
231
|
function selectTocLink(document) {
|
|
198
232
|
return document.querySelector('ul a[href="#section-1"]');
|
|
199
233
|
}
|
|
@@ -209,3 +243,7 @@ function selectRemarkExample(document) {
|
|
|
209
243
|
function selectRehypeExample(document) {
|
|
210
244
|
return document.querySelector('div[data-rehype-plugin-works]');
|
|
211
245
|
}
|
|
246
|
+
|
|
247
|
+
function selectRecmaExample(document) {
|
|
248
|
+
return document.querySelector('div[data-recma-plugin-works]');
|
|
249
|
+
}
|
|
@@ -4,6 +4,7 @@ import { expect } from 'chai';
|
|
|
4
4
|
import { parseHTML } from 'linkedom';
|
|
5
5
|
import { loadFixture } from '../../../astro/test/test-utils.js';
|
|
6
6
|
import shikiTwoslash from 'remark-shiki-twoslash';
|
|
7
|
+
import rehypePrettyCode from 'rehype-pretty-code';
|
|
7
8
|
|
|
8
9
|
const FIXTURE_ROOT = new URL('./fixtures/mdx-syntax-hightlighting/', import.meta.url);
|
|
9
10
|
|
|
@@ -88,4 +89,31 @@ describe('MDX syntax highlighting', () => {
|
|
|
88
89
|
const twoslashCodeBlock = document.querySelector('pre.shiki');
|
|
89
90
|
expect(twoslashCodeBlock).to.not.be.null;
|
|
90
91
|
});
|
|
92
|
+
|
|
93
|
+
it('supports custom highlighter - rehype-pretty-code', async () => {
|
|
94
|
+
const fixture = await loadFixture({
|
|
95
|
+
root: FIXTURE_ROOT,
|
|
96
|
+
markdown: {
|
|
97
|
+
syntaxHighlight: false,
|
|
98
|
+
},
|
|
99
|
+
integrations: [
|
|
100
|
+
mdx({
|
|
101
|
+
rehypePlugins: [
|
|
102
|
+
[
|
|
103
|
+
rehypePrettyCode,
|
|
104
|
+
{
|
|
105
|
+
onVisitHighlightedLine(node) {
|
|
106
|
+
node.properties.style = 'background-color:#000000';
|
|
107
|
+
},
|
|
108
|
+
},
|
|
109
|
+
],
|
|
110
|
+
],
|
|
111
|
+
}),
|
|
112
|
+
],
|
|
113
|
+
});
|
|
114
|
+
await fixture.build();
|
|
115
|
+
|
|
116
|
+
const html = await fixture.readFile('/index.html');
|
|
117
|
+
expect(html).to.include('style="background-color:#000000"');
|
|
118
|
+
});
|
|
91
119
|
});
|