@astrojs/mdx 0.11.5 → 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.
@@ -1,5 +1,5 @@
1
- @astrojs/mdx:build: cache hit, replaying output 2b17bb94a2cdc3f6
1
+ @astrojs/mdx:build: cache hit, replaying output 788a04a94f8adf80
2
2
  @astrojs/mdx:build: 
3
- @astrojs/mdx:build: > @astrojs/mdx@0.11.5 build /home/runner/work/astro/astro/packages/integrations/mdx
3
+ @astrojs/mdx:build: > @astrojs/mdx@0.11.6 build /home/runner/work/astro/astro/packages/integrations/mdx
4
4
  @astrojs/mdx:build: > astro-scripts build "src/**/*.ts" && tsc
5
5
  @astrojs/mdx:build: 
package/CHANGELOG.md CHANGED
@@ -1,5 +1,11 @@
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
+
3
9
  ## 0.11.5
4
10
 
5
11
  ### Patch Changes
package/README.md CHANGED
@@ -194,8 +194,7 @@ export default {
194
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).
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
  ---
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.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.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.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"
package/src/index.ts CHANGED
@@ -68,7 +68,7 @@ export default function mdx(mdxOptions: MdxOptions = {}): AstroIntegration {
68
68
  recmaPlugins: mdxOptions.recmaPlugins,
69
69
  jsx: true,
70
70
  jsxImportSource: 'astro',
71
- // Note: disable `.md` support
71
+ // Note: disable `.md` (and other alternative extensions for markdown files like `.markdown`) support
72
72
  format: 'mdx',
73
73
  mdExtensions: [],
74
74
  };
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,6 +1,6 @@
1
1
  # Syntax highlighting
2
2
 
3
- ```astro
3
+ ```astro {2}
4
4
  ---
5
5
  const handlesAstroSyntax = true
6
6
  ---
@@ -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
  });