@astrojs/markdown-remark 2.0.0 → 2.0.1

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/markdown-remark:build: cache hit, replaying output 6cabe659bf0ade09
1
+ @astrojs/markdown-remark:build: cache hit, replaying output a66e40aa1dc7052c
2
2
  @astrojs/markdown-remark:build: 
3
- @astrojs/markdown-remark:build: > @astrojs/markdown-remark@2.0.0 build /home/runner/work/astro/astro/packages/markdown/remark
3
+ @astrojs/markdown-remark:build: > @astrojs/markdown-remark@2.0.1 build /home/runner/work/astro/astro/packages/markdown/remark
4
4
  @astrojs/markdown-remark:build: > astro-scripts build "src/**/*.ts" && tsc -p tsconfig.json
5
5
  @astrojs/markdown-remark:build: 
package/CHANGELOG.md CHANGED
@@ -1,5 +1,14 @@
1
1
  # @astrojs/markdown-remark
2
2
 
3
+ ## 2.0.1
4
+
5
+ ### Patch Changes
6
+
7
+ - [#5978](https://github.com/withastro/astro/pull/5978) [`7abb1e905`](https://github.com/withastro/astro/commit/7abb1e9056c4b4fd0abfced347df32a41cdfbf28) Thanks [@HiDeoo](https://github.com/HiDeoo)! - Fix MDX heading IDs generation when using a frontmatter reference
8
+
9
+ - Updated dependencies [[`b53e0717b`](https://github.com/withastro/astro/commit/b53e0717b7f6b042baaeec7f87999e99c76c031c), [`60b32d585`](https://github.com/withastro/astro/commit/60b32d58565d87e87573eb268408293fc28ec657), [`883e0cc29`](https://github.com/withastro/astro/commit/883e0cc29968d51ed6c7515be035a40b28bafdad), [`dabce6b8c`](https://github.com/withastro/astro/commit/dabce6b8c684f851c3535f8acead06cbef6dce2a), [`aedf23f85`](https://github.com/withastro/astro/commit/aedf23f8582e32a6b94b81ddba9b323831f2b22a)]:
10
+ - astro@2.0.2
11
+
3
12
  ## 2.0.0
4
13
 
5
14
  ### Major Changes
@@ -41,47 +50,47 @@
41
50
  - [#5785](https://github.com/withastro/astro/pull/5785) [`16107b6a1`](https://github.com/withastro/astro/commit/16107b6a10514ef1b563e585ec9add4b14f42b94) Thanks [@delucis](https://github.com/delucis)! - Drop support for legacy Astro-flavored Markdown
42
51
 
43
52
  - [#5684](https://github.com/withastro/astro/pull/5684) [`a9c292026`](https://github.com/withastro/astro/commit/a9c2920264e36cc5dc05f4adc1912187979edb0d) & [#5769](https://github.com/withastro/astro/pull/5769) [`93e633922`](https://github.com/withastro/astro/commit/93e633922c2e449df3bb2357b3683af1d3c0e07b) Thanks [@bholmesdev](https://github.com/bholmesdev)! - Refine Markdown and MDX configuration options for ease-of-use.
44
-
53
+
45
54
  - **Markdown**
46
55
 
47
- - **Replace the `extendDefaultPlugins` option** with a `gfm` boolean and a `smartypants` boolean. These are enabled by default, and can be disabled to remove GitHub-Flavored Markdown and SmartyPants.
48
-
49
- - Ensure GitHub-Flavored Markdown and SmartyPants are applied whether or not custom `remarkPlugins` or `rehypePlugins` are configured. If you want to apply custom plugins _and_ remove Astro's default plugins, manually set `gfm: false` and `smartypants: false` in your config.
56
+ - **Replace the `extendDefaultPlugins` option** with a `gfm` boolean and a `smartypants` boolean. These are enabled by default, and can be disabled to remove GitHub-Flavored Markdown and SmartyPants.
57
+
58
+ - Ensure GitHub-Flavored Markdown and SmartyPants are applied whether or not custom `remarkPlugins` or `rehypePlugins` are configured. If you want to apply custom plugins _and_ remove Astro's default plugins, manually set `gfm: false` and `smartypants: false` in your config.
50
59
 
51
60
  - **Migrate `extendDefaultPlugins` to `gfm` and `smartypants`**
52
61
 
53
- You may have disabled Astro's built-in plugins (GitHub-Flavored Markdown and Smartypants) with the `extendDefaultPlugins` option. This has now been split into 2 flags to disable each plugin individually:
62
+ You may have disabled Astro's built-in plugins (GitHub-Flavored Markdown and Smartypants) with the `extendDefaultPlugins` option. This has now been split into 2 flags to disable each plugin individually:
54
63
 
55
- - `markdown.gfm` to disable GitHub-Flavored Markdown
56
- - `markdown.smartypants` to disable SmartyPants
64
+ - `markdown.gfm` to disable GitHub-Flavored Markdown
65
+ - `markdown.smartypants` to disable SmartyPants
57
66
 
58
- ```diff
59
- // astro.config.mjs
60
- import { defineConfig } from 'astro/config';
67
+ ```diff
68
+ // astro.config.mjs
69
+ import { defineConfig } from 'astro/config';
61
70
 
62
- export default defineConfig({
63
- markdown: {
64
- - extendDefaultPlugins: false,
65
- + smartypants: false,
66
- + gfm: false,
67
- }
68
- });
69
- ```
71
+ export default defineConfig({
72
+ markdown: {
73
+ - extendDefaultPlugins: false,
74
+ + smartypants: false,
75
+ + gfm: false,
76
+ }
77
+ });
78
+ ```
70
79
 
71
- Additionally, applying remark and rehype plugins **no longer disables** `gfm` and `smartypants`. You will need to opt-out manually by setting `gfm` and `smartypants` to `false`.
80
+ Additionally, applying remark and rehype plugins **no longer disables** `gfm` and `smartypants`. You will need to opt-out manually by setting `gfm` and `smartypants` to `false`.
72
81
 
73
82
  - **MDX**
74
83
 
75
- - Support _all_ Markdown configuration options (except `drafts`) from your MDX integration config. This includes `syntaxHighlighting` and `shikiConfig` options to further customize the MDX renderer.
84
+ - Support _all_ Markdown configuration options (except `drafts`) from your MDX integration config. This includes `syntaxHighlighting` and `shikiConfig` options to further customize the MDX renderer.
76
85
 
77
- - Simplify `extendPlugins` to an `extendMarkdownConfig` option. MDX options will default to their equivalent in your Markdown config. By setting `extendMarkdownConfig` to false, you can "eject" to set your own syntax highlighting, plugins, and more.
86
+ - Simplify `extendPlugins` to an `extendMarkdownConfig` option. MDX options will default to their equivalent in your Markdown config. By setting `extendMarkdownConfig` to false, you can "eject" to set your own syntax highlighting, plugins, and more.
78
87
 
79
88
  - **Migrate MDX's `extendPlugins` to `extendMarkdownConfig`**
80
89
 
81
- You may have used the `extendPlugins` option to manage plugin defaults in MDX. This has been replaced by 3 flags:
90
+ You may have used the `extendPlugins` option to manage plugin defaults in MDX. This has been replaced by 3 flags:
82
91
 
83
- - `extendMarkdownConfig` (`true` by default) to toggle Markdown config inheritance. This replaces the `extendPlugins: 'markdown'` option.
84
- - `gfm` (`true` by default) and `smartypants` (`true` by default) to toggle GitHub-Flavored Markdown and SmartyPants in MDX. This replaces the `extendPlugins: 'defaults'` option.
92
+ - `extendMarkdownConfig` (`true` by default) to toggle Markdown config inheritance. This replaces the `extendPlugins: 'markdown'` option.
93
+ - `gfm` (`true` by default) and `smartypants` (`true` by default) to toggle GitHub-Flavored Markdown and SmartyPants in MDX. This replaces the `extendPlugins: 'defaults'` option.
85
94
 
86
95
  - [#5825](https://github.com/withastro/astro/pull/5825) [`52209ca2a`](https://github.com/withastro/astro/commit/52209ca2ad72a30854947dcb3a90ab4db0ac0a6f) Thanks [@bholmesdev](https://github.com/bholmesdev)! - Baseline the experimental `contentCollections` flag. You're free to remove this from your astro config!
87
96
 
@@ -98,7 +107,6 @@
98
107
 
99
108
  This marks `astro` as a `peerDependency` of several packages that are already getting `major` version bumps. This is so we can more properly track the dependency between them and what version of Astro they are being used with.
100
109
 
101
-
102
110
  **Patch Changes**
103
111
 
104
112
  - [#5837](https://github.com/withastro/astro/pull/5837) [`12f65a4d5`](https://github.com/withastro/astro/commit/12f65a4d55e3fd2993c2f67b18794dd536280c69) Thanks [@giuseppelt](https://github.com/giuseppelt)! - fix shiki css class replace logic
@@ -1,5 +1,6 @@
1
1
  import Slugger from "github-slugger";
2
2
  import { visit } from "unist-util-visit";
3
+ import { InvalidAstroDataError, safelyGetAstroData } from "./frontmatter-injection.js";
3
4
  const rawNodeTypes = /* @__PURE__ */ new Set(["text", "raw", "mdxTextExpression"]);
4
5
  const codeTagNames = /* @__PURE__ */ new Set(["code", "pre"]);
5
6
  function rehypeHeadingIds() {
@@ -7,6 +8,7 @@ function rehypeHeadingIds() {
7
8
  const headings = [];
8
9
  const slugger = new Slugger();
9
10
  const isMDX = isMDXFile(file);
11
+ const astroData = safelyGetAstroData(file.data);
10
12
  visit(tree, (node) => {
11
13
  if (node.type !== "element")
12
14
  return;
@@ -29,7 +31,17 @@ function rehypeHeadingIds() {
29
31
  }
30
32
  if (rawNodeTypes.has(child.type)) {
31
33
  if (isMDX || codeTagNames.has(parent.tagName)) {
32
- text += child.value;
34
+ let value = child.value;
35
+ if (isMdxTextExpression(child) && !(astroData instanceof InvalidAstroDataError)) {
36
+ const frontmatterPath = getMdxFrontmatterVariablePath(child);
37
+ if (Array.isArray(frontmatterPath) && frontmatterPath.length > 0) {
38
+ const frontmatterValue = getMdxFrontmatterVariableValue(astroData, frontmatterPath);
39
+ if (typeof frontmatterValue === "string") {
40
+ value = frontmatterValue;
41
+ }
42
+ }
43
+ }
44
+ text += value;
33
45
  } else {
34
46
  text += child.value.replace(/\{/g, "${");
35
47
  }
@@ -51,6 +63,37 @@ function isMDXFile(file) {
51
63
  var _a;
52
64
  return Boolean((_a = file.history[0]) == null ? void 0 : _a.endsWith(".mdx"));
53
65
  }
66
+ function getMdxFrontmatterVariablePath(node) {
67
+ var _a;
68
+ if (!((_a = node.data) == null ? void 0 : _a.estree) || node.data.estree.body.length !== 1)
69
+ return new Error();
70
+ const statement = node.data.estree.body[0];
71
+ if ((statement == null ? void 0 : statement.type) !== "ExpressionStatement" || statement.expression.type !== "MemberExpression")
72
+ return new Error();
73
+ let expression = statement.expression;
74
+ const expressionPath = [];
75
+ while (expression.type === "MemberExpression" && expression.property.type === (expression.computed ? "Literal" : "Identifier")) {
76
+ expressionPath.push(
77
+ expression.property.type === "Literal" ? String(expression.property.value) : expression.property.name
78
+ );
79
+ expression = expression.object;
80
+ }
81
+ if (expression.type !== "Identifier" || expression.name !== "frontmatter")
82
+ return new Error();
83
+ return expressionPath.reverse();
84
+ }
85
+ function getMdxFrontmatterVariableValue(astroData, path) {
86
+ let value = astroData.frontmatter;
87
+ for (const key of path) {
88
+ if (!value[key])
89
+ return void 0;
90
+ value = value[key];
91
+ }
92
+ return value;
93
+ }
94
+ function isMdxTextExpression(node) {
95
+ return node.type === "mdxTextExpression";
96
+ }
54
97
  export {
55
98
  rehypeHeadingIds
56
99
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@astrojs/markdown-remark",
3
- "version": "2.0.0",
3
+ "version": "2.0.1",
4
4
  "type": "module",
5
5
  "author": "withastro",
6
6
  "license": "MIT",
@@ -17,7 +17,7 @@
17
17
  "./dist/internal.js": "./dist/internal.js"
18
18
  },
19
19
  "peerDependencies": {
20
- "astro": "^2.0.0"
20
+ "astro": "^2.0.2"
21
21
  },
22
22
  "dependencies": {
23
23
  "@astrojs/prism": "^2.0.0",
@@ -36,6 +36,7 @@
36
36
  },
37
37
  "devDependencies": {
38
38
  "@types/chai": "^4.3.1",
39
+ "@types/estree": "^1.0.0",
39
40
  "@types/github-slugger": "^1.3.0",
40
41
  "@types/hast": "^2.3.4",
41
42
  "@types/mdast": "^3.0.10",
@@ -43,6 +44,7 @@
43
44
  "@types/unist": "^2.0.6",
44
45
  "astro-scripts": "0.0.10",
45
46
  "chai": "^4.3.6",
47
+ "mdast-util-mdx-expression": "^1.3.1",
46
48
  "mocha": "^9.2.2"
47
49
  },
48
50
  "scripts": {
@@ -1,7 +1,10 @@
1
+ import { type Expression, type Super } from 'estree';
1
2
  import Slugger from 'github-slugger';
2
- import { visit } from 'unist-util-visit';
3
+ import { type MdxTextExpression } from 'mdast-util-mdx-expression';
4
+ import { visit, type Node } from 'unist-util-visit';
3
5
 
4
- import type { MarkdownHeading, MarkdownVFile, RehypePlugin } from './types.js';
6
+ import { InvalidAstroDataError, safelyGetAstroData } from './frontmatter-injection.js';
7
+ import type { MarkdownAstroData, MarkdownHeading, MarkdownVFile, RehypePlugin } from './types.js';
5
8
 
6
9
  const rawNodeTypes = new Set(['text', 'raw', 'mdxTextExpression']);
7
10
  const codeTagNames = new Set(['code', 'pre']);
@@ -11,6 +14,7 @@ export function rehypeHeadingIds(): ReturnType<RehypePlugin> {
11
14
  const headings: MarkdownHeading[] = [];
12
15
  const slugger = new Slugger();
13
16
  const isMDX = isMDXFile(file);
17
+ const astroData = safelyGetAstroData(file.data);
14
18
  visit(tree, (node) => {
15
19
  if (node.type !== 'element') return;
16
20
  const { tagName } = node;
@@ -31,7 +35,17 @@ export function rehypeHeadingIds(): ReturnType<RehypePlugin> {
31
35
  }
32
36
  if (rawNodeTypes.has(child.type)) {
33
37
  if (isMDX || codeTagNames.has(parent.tagName)) {
34
- text += child.value;
38
+ let value = child.value;
39
+ if (isMdxTextExpression(child) && !(astroData instanceof InvalidAstroDataError)) {
40
+ const frontmatterPath = getMdxFrontmatterVariablePath(child);
41
+ if (Array.isArray(frontmatterPath) && frontmatterPath.length > 0) {
42
+ const frontmatterValue = getMdxFrontmatterVariableValue(astroData, frontmatterPath);
43
+ if (typeof frontmatterValue === 'string') {
44
+ value = frontmatterValue;
45
+ }
46
+ }
47
+ }
48
+ text += value;
35
49
  } else {
36
50
  text += child.value.replace(/\{/g, '${');
37
51
  }
@@ -57,3 +71,58 @@ export function rehypeHeadingIds(): ReturnType<RehypePlugin> {
57
71
  function isMDXFile(file: MarkdownVFile) {
58
72
  return Boolean(file.history[0]?.endsWith('.mdx'));
59
73
  }
74
+
75
+ /**
76
+ * Check if an ESTree entry is `frontmatter.*.VARIABLE`.
77
+ * If it is, return the variable path (i.e. `["*", ..., "VARIABLE"]`) minus the `frontmatter` prefix.
78
+ */
79
+ function getMdxFrontmatterVariablePath(node: MdxTextExpression): string[] | Error {
80
+ if (!node.data?.estree || node.data.estree.body.length !== 1) return new Error();
81
+
82
+ const statement = node.data.estree.body[0];
83
+
84
+ // Check for "[ANYTHING].[ANYTHING]".
85
+ if (statement?.type !== 'ExpressionStatement' || statement.expression.type !== 'MemberExpression')
86
+ return new Error();
87
+
88
+ let expression: Expression | Super = statement.expression;
89
+ const expressionPath: string[] = [];
90
+
91
+ // Traverse the expression, collecting the variable path.
92
+ while (
93
+ expression.type === 'MemberExpression' &&
94
+ expression.property.type === (expression.computed ? 'Literal' : 'Identifier')
95
+ ) {
96
+ expressionPath.push(
97
+ expression.property.type === 'Literal'
98
+ ? String(expression.property.value)
99
+ : expression.property.name
100
+ );
101
+
102
+ expression = expression.object;
103
+ }
104
+
105
+ // Check for "frontmatter.[ANYTHING]".
106
+ if (expression.type !== 'Identifier' || expression.name !== 'frontmatter') return new Error();
107
+
108
+ return expressionPath.reverse();
109
+ }
110
+
111
+ function getMdxFrontmatterVariableValue(astroData: MarkdownAstroData, path: string[]) {
112
+ let value: MdxFrontmatterVariableValue = astroData.frontmatter;
113
+
114
+ for (const key of path) {
115
+ if (!value[key]) return undefined;
116
+
117
+ value = value[key];
118
+ }
119
+
120
+ return value;
121
+ }
122
+
123
+ function isMdxTextExpression(node: Node): node is MdxTextExpression {
124
+ return node.type === 'mdxTextExpression';
125
+ }
126
+
127
+ type MdxFrontmatterVariableValue =
128
+ MarkdownAstroData['frontmatter'][keyof MarkdownAstroData['frontmatter']];