@astrojs/mdx 0.11.5 → 0.12.0

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.
@@ -1,5 +1,5 @@
1
- @astrojs/mdx:build: cache hit, replaying output 2b17bb94a2cdc3f6
2
- @astrojs/mdx:build: 
3
- @astrojs/mdx:build: > @astrojs/mdx@0.11.5 build /home/runner/work/astro/astro/packages/integrations/mdx
4
- @astrojs/mdx:build: > astro-scripts build "src/**/*.ts" && tsc
5
- @astrojs/mdx:build: 
1
+ @astrojs/mdx:build: cache hit, replaying output dac9ca86e0959274
2
+ @astrojs/mdx:build: 
3
+ @astrojs/mdx:build: > @astrojs/mdx@0.12.0 build /home/runner/work/astro/astro/packages/integrations/mdx
4
+ @astrojs/mdx:build: > astro-scripts build "src/**/*.ts" && tsc
5
+ @astrojs/mdx:build: 
package/CHANGELOG.md CHANGED
@@ -1,5 +1,21 @@
1
1
  # @astrojs/mdx
2
2
 
3
+ ## 0.12.0
4
+
5
+ ### Minor Changes
6
+
7
+ - [#5427](https://github.com/withastro/astro/pull/5427) [`2a1c085b1`](https://github.com/withastro/astro/commit/2a1c085b199f24e34424ec8c19041c03602c53c5) Thanks [@backflip](https://github.com/backflip)! - Uses remark-rehype options from astro.config.mjs
8
+
9
+ ### Patch Changes
10
+
11
+ - [#5448](https://github.com/withastro/astro/pull/5448) [`ef2ffc7ae`](https://github.com/withastro/astro/commit/ef2ffc7ae9ff554860238ecd2fb3bf6d82b5801b) Thanks [@delucis](https://github.com/delucis)! - Fix broken link in README
12
+
13
+ ## 0.11.6
14
+
15
+ ### Patch Changes
16
+
17
+ - [#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``
18
+
3
19
  ## 0.11.5
4
20
 
5
21
  ### Patch Changes
package/README.md CHANGED
@@ -191,11 +191,10 @@ export default {
191
191
  };
192
192
  ```
193
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
+ …every MDX file will have `customProperty` in its frontmatter! See [our Markdown documentation](https://docs.astro.build/en/guides/markdown-content/#example-injecting-frontmatter) for more usage instructions and a [reading time plugin example](https://docs.astro.build/en/guides/markdown-content/#example-calculate-reading-time).
195
195
 
196
196
  ### Layouts
197
-
198
- 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:
199
198
 
200
199
  ```yaml
201
200
  ---
@@ -519,6 +518,26 @@ These are plugins that modify the output [estree](https://github.com/estree/estr
519
518
 
520
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.
521
520
 
521
+ ### remarkRehype
522
+
523
+ Markdown content is transformed into HTML through remark-rehype which has [a number of options](https://github.com/remarkjs/remark-rehype#options).
524
+
525
+ You can use remark-rehype options in your MDX integration config file like so:
526
+
527
+ ```js
528
+ // astro.config.mjs
529
+ export default {
530
+ integrations: [mdx({
531
+ remarkRehype: {
532
+ footnoteLabel: 'Catatan kaki',
533
+ footnoteBackLabel: 'Kembali ke konten',
534
+ },
535
+ })],
536
+ };
537
+ ```
538
+
539
+ This inherits the configuration of `markdown.remarkRehype`. This behavior can be changed by configuring `extendPlugins`.
540
+
522
541
  ## Examples
523
542
 
524
543
  - 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
@@ -1,5 +1,6 @@
1
1
  import { PluggableList } from '@mdx-js/mdx/lib/core.js';
2
2
  import type { AstroIntegration } from 'astro';
3
+ import type { Options as RemarkRehypeOptions } from 'remark-rehype';
3
4
  export declare type MdxOptions = {
4
5
  remarkPlugins?: PluggableList;
5
6
  rehypePlugins?: PluggableList;
@@ -12,5 +13,6 @@ export declare type MdxOptions = {
12
13
  * - false - do not inherit any plugins
13
14
  */
14
15
  extendPlugins?: 'markdown' | 'astroDefaults' | false;
16
+ remarkRehype?: RemarkRehypeOptions;
15
17
  };
16
18
  export default function mdx(mdxOptions?: MdxOptions): AstroIntegration;
package/dist/index.js CHANGED
@@ -34,6 +34,13 @@ function mdx(mdxOptions = {}) {
34
34
  );
35
35
  console.info(`See "extendPlugins" option to configure this behavior.`);
36
36
  }
37
+ let remarkRehypeOptions = mdxOptions.remarkRehype;
38
+ if (mdxOptions.extendPlugins === "markdown") {
39
+ remarkRehypeOptions = {
40
+ ...config.markdown.remarkRehype,
41
+ ...remarkRehypeOptions
42
+ };
43
+ }
37
44
  const mdxPluginOpts = {
38
45
  remarkPlugins: await getRemarkPlugins(mdxOptions, config),
39
46
  rehypePlugins: getRehypePlugins(mdxOptions, config),
@@ -41,7 +48,8 @@ function mdx(mdxOptions = {}) {
41
48
  jsx: true,
42
49
  jsxImportSource: "astro",
43
50
  format: "mdx",
44
- mdExtensions: []
51
+ mdExtensions: [],
52
+ remarkRehypeOptions
45
53
  };
46
54
  let importMetaEnv = {
47
55
  SITE: config.site
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.5",
4
+ "version": "0.12.0",
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.1",
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.5.3",
50
- "astro-scripts": "0.0.8",
49
+ "astro": "1.6.11",
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,8 @@
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",
59
+ "remark-rehype": "^10.1.0",
58
60
  "remark-shiki-twoslash": "^3.1.0",
59
61
  "remark-toc": "^8.0.1",
60
62
  "vite": "^3.0.0"
package/src/index.ts CHANGED
@@ -5,6 +5,7 @@ import type { AstroIntegration } from 'astro';
5
5
  import { parse as parseESM } from 'es-module-lexer';
6
6
  import { blue, bold } from 'kleur/colors';
7
7
  import fs from 'node:fs/promises';
8
+ import type { Options as RemarkRehypeOptions } from 'remark-rehype';
8
9
  import { VFile } from 'vfile';
9
10
  import type { Plugin as VitePlugin } from 'vite';
10
11
  import {
@@ -33,6 +34,7 @@ export type MdxOptions = {
33
34
  * - false - do not inherit any plugins
34
35
  */
35
36
  extendPlugins?: 'markdown' | 'astroDefaults' | false;
37
+ remarkRehype?: RemarkRehypeOptions;
36
38
  };
37
39
 
38
40
  export default function mdx(mdxOptions: MdxOptions = {}): AstroIntegration {
@@ -62,15 +64,25 @@ export default function mdx(mdxOptions: MdxOptions = {}): AstroIntegration {
62
64
  console.info(`See "extendPlugins" option to configure this behavior.`);
63
65
  }
64
66
 
67
+ let remarkRehypeOptions = mdxOptions.remarkRehype;
68
+
69
+ if (mdxOptions.extendPlugins === 'markdown') {
70
+ remarkRehypeOptions = {
71
+ ...config.markdown.remarkRehype,
72
+ ...remarkRehypeOptions,
73
+ };
74
+ }
75
+
65
76
  const mdxPluginOpts: MdxRollupPluginOptions = {
66
77
  remarkPlugins: await getRemarkPlugins(mdxOptions, config),
67
78
  rehypePlugins: getRehypePlugins(mdxOptions, config),
68
79
  recmaPlugins: mdxOptions.recmaPlugins,
69
80
  jsx: true,
70
81
  jsxImportSource: 'astro',
71
- // Note: disable `.md` support
82
+ // Note: disable `.md` (and other alternative extensions for markdown files like `.markdown`) support
72
83
  format: 'mdx',
73
84
  mdExtensions: [],
85
+ remarkRehypeOptions,
74
86
  };
75
87
 
76
88
  let importMetaEnv: Record<string, any> = {
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
+ }
@@ -0,0 +1,5 @@
1
+ # Hello world
2
+
3
+ This[^1] should be visible.
4
+
5
+ [^1]: And there would be a footnote.
@@ -1,6 +1,6 @@
1
1
  # Syntax highlighting
2
2
 
3
- ```astro
3
+ ```astro {2}
4
4
  ---
5
5
  const handlesAstroSyntax = true
6
6
  ---
@@ -0,0 +1,85 @@
1
+ import mdx from '@astrojs/mdx';
2
+
3
+ import { expect } from 'chai';
4
+ import { parseHTML } from 'linkedom';
5
+ import { loadFixture } from '../../../astro/test/test-utils.js';
6
+
7
+ describe('MDX with Astro Markdown remark-rehype config', () => {
8
+ it('Renders footnotes with values from the default configuration', async () => {
9
+ const fixture = await loadFixture({
10
+ root: new URL('./fixtures/mdx-astro-markdown-remarkRehype/', import.meta.url),
11
+ integrations: [mdx()],
12
+ markdown: {
13
+ remarkRehype: {
14
+ footnoteLabel: 'Catatan kaki',
15
+ footnoteBackLabel: 'Kembali ke konten',
16
+ },
17
+ },
18
+ });
19
+
20
+ await fixture.build();
21
+ const html = await fixture.readFile('/index.html');
22
+ const { document } = parseHTML(html);
23
+
24
+ expect(document.querySelector('#footnote-label').textContent).to.equal('Catatan kaki');
25
+ expect(document.querySelector('.data-footnote-backref').getAttribute('aria-label')).to.equal(
26
+ 'Kembali ke konten'
27
+ );
28
+ });
29
+
30
+ it('Renders footnotes with values from custom configuration extending the default', async () => {
31
+ const fixture = await loadFixture({
32
+ root: new URL('./fixtures/mdx-astro-markdown-remarkRehype/', import.meta.url),
33
+ integrations: [
34
+ mdx({
35
+ remarkRehype: {
36
+ footnoteLabel: 'Catatan kaki',
37
+ footnoteBackLabel: 'Kembali ke konten',
38
+ },
39
+ }),
40
+ ],
41
+ markdown: {
42
+ remarkRehype: {
43
+ footnoteBackLabel: 'Replace me',
44
+ },
45
+ },
46
+ });
47
+
48
+ await fixture.build();
49
+ const html = await fixture.readFile('/index.html');
50
+ const { document } = parseHTML(html);
51
+
52
+ expect(document.querySelector('#footnote-label').textContent).to.equal('Catatan kaki');
53
+ expect(document.querySelector('.data-footnote-backref').getAttribute('aria-label')).to.equal(
54
+ 'Kembali ke konten'
55
+ );
56
+ });
57
+
58
+ it('Renders footnotes with values from custom configuration without extending the default', async () => {
59
+ const fixture = await loadFixture({
60
+ root: new URL('./fixtures/mdx-astro-markdown-remarkRehype/', import.meta.url),
61
+ integrations: [
62
+ mdx({
63
+ extendPlugins: 'astroDefaults',
64
+ remarkRehype: {
65
+ footnoteLabel: 'Catatan kaki',
66
+ },
67
+ }),
68
+ ],
69
+ markdown: {
70
+ remarkRehype: {
71
+ footnoteBackLabel: 'Kembali ke konten',
72
+ },
73
+ },
74
+ });
75
+
76
+ await fixture.build();
77
+ const html = await fixture.readFile('/index.html');
78
+ const { document } = parseHTML(html);
79
+
80
+ expect(document.querySelector('#footnote-label').textContent).to.equal('Catatan kaki');
81
+ expect(document.querySelector('.data-footnote-backref').getAttribute('aria-label')).to.equal(
82
+ 'Back to content'
83
+ );
84
+ });
85
+ });
@@ -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
  });