@astrojs/markdown-remark 0.10.1 → 0.10.2

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/CHANGELOG.md CHANGED
@@ -1,5 +1,11 @@
1
1
  # @astrojs/markdown-remark
2
2
 
3
+ ## 0.10.2
4
+
5
+ ### Patch Changes
6
+
7
+ - [#3486](https://github.com/withastro/astro/pull/3486) [`119ecf8d`](https://github.com/withastro/astro/commit/119ecf8d469f034eaf1b1217523954d29f492cb6) Thanks [@hippotastic](https://github.com/hippotastic)! - Fix components in markdown regressions
8
+
3
9
  ## 0.10.1
4
10
 
5
11
  ### Patch Changes
@@ -4,6 +4,9 @@ function rehypeEscape() {
4
4
  return visit(node, "element", (el) => {
5
5
  if (el.tagName === "code" || el.tagName === "pre") {
6
6
  el.properties["is:raw"] = true;
7
+ visit(el, "raw", (raw) => {
8
+ raw.value = raw.value.replace(/</g, "&lt;").replace(/>/g, "&gt;");
9
+ });
7
10
  }
8
11
  return el;
9
12
  });
@@ -1,69 +1,45 @@
1
- var __defProp = Object.defineProperty;
2
- var __defProps = Object.defineProperties;
3
- var __getOwnPropDescs = Object.getOwnPropertyDescriptors;
4
- var __getOwnPropSymbols = Object.getOwnPropertySymbols;
5
- var __hasOwnProp = Object.prototype.hasOwnProperty;
6
- var __propIsEnum = Object.prototype.propertyIsEnumerable;
7
- var __defNormalProp = (obj, key, value) => key in obj ? __defProp(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
8
- var __spreadValues = (a, b) => {
9
- for (var prop in b || (b = {}))
10
- if (__hasOwnProp.call(b, prop))
11
- __defNormalProp(a, prop, b[prop]);
12
- if (__getOwnPropSymbols)
13
- for (var prop of __getOwnPropSymbols(b)) {
14
- if (__propIsEnum.call(b, prop))
15
- __defNormalProp(a, prop, b[prop]);
16
- }
17
- return a;
18
- };
19
- var __spreadProps = (a, b) => __defProps(a, __getOwnPropDescs(b));
20
- import { map } from "unist-util-map";
21
- const MDX_ELEMENTS = /* @__PURE__ */ new Set(["mdxJsxFlowElement", "mdxJsxTextElement"]);
1
+ import { visit } from "unist-util-visit";
2
+ const MDX_ELEMENTS = ["mdxJsxFlowElement", "mdxJsxTextElement"];
22
3
  function rehypeJsx() {
23
4
  return function(node) {
24
- return map(node, (child) => {
25
- if (child.type === "element") {
26
- return __spreadProps(__spreadValues({}, child), { tagName: `${child.tagName}` });
27
- }
28
- if (MDX_ELEMENTS.has(child.type)) {
29
- const attrs = child.attributes.reduce((acc, entry) => {
30
- let attr = entry.value;
31
- if (attr && typeof attr === "object") {
32
- attr = `{${attr.value}}`;
33
- } else if (attr && entry.type === "mdxJsxExpressionAttribute") {
34
- attr = `{${attr}}`;
35
- } else if (attr === null) {
36
- attr = "";
37
- } else if (typeof attr === "string") {
38
- attr = `"${attr}"`;
39
- }
40
- if (!entry.name) {
41
- return acc + ` ${attr}`;
42
- }
43
- return acc + ` ${entry.name}${attr ? "=" : ""}${attr}`;
44
- }, "");
45
- if (child.children.length === 0) {
46
- return {
47
- type: "raw",
48
- value: `<${child.name}${attrs} />`
49
- };
5
+ visit(node, "element", (child) => {
6
+ child.tagName = `${child.tagName}`;
7
+ });
8
+ visit(node, MDX_ELEMENTS, (child, index, parent) => {
9
+ if (index === null || !Boolean(parent))
10
+ return;
11
+ const attrs = child.attributes.reduce((acc, entry) => {
12
+ let attr = entry.value;
13
+ if (attr && typeof attr === "object") {
14
+ attr = `{${attr.value}}`;
15
+ } else if (attr && entry.type === "mdxJsxExpressionAttribute") {
16
+ attr = `{${attr}}`;
17
+ } else if (attr === null) {
18
+ attr = "";
19
+ } else if (typeof attr === "string") {
20
+ attr = `"${attr}"`;
50
21
  }
51
- child.children.splice(0, 0, {
52
- type: "raw",
53
- value: `
22
+ if (!entry.name) {
23
+ return acc + ` ${attr}`;
24
+ }
25
+ return acc + ` ${entry.name}${attr ? "=" : ""}${attr}`;
26
+ }, "");
27
+ if (child.children.length === 0) {
28
+ child.type = "raw";
29
+ child.value = `<${child.name}${attrs} />`;
30
+ return;
31
+ }
32
+ const openingTag = {
33
+ type: "raw",
34
+ value: `
54
35
  <${child.name}${attrs}>`
55
- });
56
- child.children.push({
57
- type: "raw",
58
- value: `</${child.name}>
36
+ };
37
+ const closingTag = {
38
+ type: "raw",
39
+ value: `</${child.name}>
59
40
  `
60
- });
61
- return __spreadProps(__spreadValues({}, child), {
62
- type: "element",
63
- tagName: `Fragment`
64
- });
65
- }
66
- return child;
41
+ };
42
+ parent.children.splice(index, 1, openingTag, ...child.children, closingTag);
67
43
  });
68
44
  };
69
45
  }
@@ -1,3 +1,3 @@
1
1
  import type { ShikiConfig } from './types.js';
2
- declare const remarkShiki: ({ langs, theme, wrap }: ShikiConfig, scopedClassName?: string | null | undefined) => Promise<() => (tree: any) => void>;
2
+ declare const remarkShiki: ({ langs, theme, wrap }: ShikiConfig, scopedClassName?: string | null) => Promise<() => (tree: any) => void>;
3
3
  export default remarkShiki;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@astrojs/markdown-remark",
3
- "version": "0.10.1",
3
+ "version": "0.10.2",
4
4
  "type": "module",
5
5
  "author": "withastro",
6
6
  "license": "MIT",
@@ -5,6 +5,11 @@ export default function rehypeEscape(): any {
5
5
  return visit(node, 'element', (el) => {
6
6
  if (el.tagName === 'code' || el.tagName === 'pre') {
7
7
  el.properties['is:raw'] = true;
8
+ // Visit all raw children and escape HTML tags to prevent Markdown code
9
+ // like "This is a `<script>` tag" from actually opening a script tag
10
+ visit(el, 'raw', (raw) => {
11
+ raw.value = raw.value.replace(/</g, '&lt;').replace(/>/g, '&gt;');
12
+ });
8
13
  }
9
14
  return el;
10
15
  });
package/src/rehype-jsx.ts CHANGED
@@ -1,51 +1,48 @@
1
- import { map } from 'unist-util-map';
1
+ import { visit } from 'unist-util-visit';
2
2
 
3
- const MDX_ELEMENTS = new Set(['mdxJsxFlowElement', 'mdxJsxTextElement']);
3
+ const MDX_ELEMENTS = ['mdxJsxFlowElement', 'mdxJsxTextElement'];
4
4
  export default function rehypeJsx(): any {
5
5
  return function (node: any): any {
6
- return map(node, (child: any) => {
7
- if (child.type === 'element') {
8
- return { ...child, tagName: `${child.tagName}` };
9
- }
10
- if (MDX_ELEMENTS.has(child.type)) {
11
- const attrs = child.attributes.reduce((acc: any[], entry: any) => {
12
- let attr = entry.value;
13
- if (attr && typeof attr === 'object') {
14
- attr = `{${attr.value}}`;
15
- } else if (attr && entry.type === 'mdxJsxExpressionAttribute') {
16
- attr = `{${attr}}`;
17
- } else if (attr === null) {
18
- attr = '';
19
- } else if (typeof attr === 'string') {
20
- attr = `"${attr}"`;
21
- }
22
- if (!entry.name) {
23
- return acc + ` ${attr}`;
24
- }
25
- return acc + ` ${entry.name}${attr ? '=' : ''}${attr}`;
26
- }, '');
6
+ visit(node, 'element', (child: any) => {
7
+ child.tagName = `${child.tagName}`;
8
+ });
9
+ visit(node, MDX_ELEMENTS, (child: any, index: number | null, parent: any) => {
10
+ if (index === null || !Boolean(parent)) return;
27
11
 
28
- if (child.children.length === 0) {
29
- return {
30
- type: 'raw',
31
- value: `<${child.name}${attrs} />`,
32
- };
12
+ const attrs = child.attributes.reduce((acc: any[], entry: any) => {
13
+ let attr = entry.value;
14
+ if (attr && typeof attr === 'object') {
15
+ attr = `{${attr.value}}`;
16
+ } else if (attr && entry.type === 'mdxJsxExpressionAttribute') {
17
+ attr = `{${attr}}`;
18
+ } else if (attr === null) {
19
+ attr = '';
20
+ } else if (typeof attr === 'string') {
21
+ attr = `"${attr}"`;
22
+ }
23
+ if (!entry.name) {
24
+ return acc + ` ${attr}`;
33
25
  }
34
- child.children.splice(0, 0, {
35
- type: 'raw',
36
- value: `\n<${child.name}${attrs}>`,
37
- });
38
- child.children.push({
39
- type: 'raw',
40
- value: `</${child.name}>\n`,
41
- });
42
- return {
43
- ...child,
44
- type: 'element',
45
- tagName: `Fragment`,
46
- };
26
+ return acc + ` ${entry.name}${attr ? '=' : ''}${attr}`;
27
+ }, '');
28
+
29
+ if (child.children.length === 0) {
30
+ child.type = 'raw';
31
+ child.value = `<${child.name}${attrs} />`;
32
+ return;
47
33
  }
48
- return child;
34
+
35
+ // Replace the current child node with its children
36
+ // wrapped by raw opening and closing tags
37
+ const openingTag = {
38
+ type: 'raw',
39
+ value: `\n<${child.name}${attrs}>`,
40
+ };
41
+ const closingTag = {
42
+ type: 'raw',
43
+ value: `</${child.name}>\n`,
44
+ };
45
+ parent.children.splice(index, 1, openingTag, ...child.children, closingTag);
49
46
  });
50
47
  };
51
48
  }
@@ -49,9 +49,18 @@ describe('components', () => {
49
49
  it('should normalize children', async () => {
50
50
  const { code } = await renderMarkdown(`<Component bool={true}>Hello world!</Component>`, {});
51
51
 
52
+ chai.expect(code).to.equal(`\n<Component bool={true}>Hello world!</Component>\n`);
53
+ });
54
+
55
+ it('should be able to nest components', async () => {
56
+ const { code } = await renderMarkdown(
57
+ `<Component bool={true}><Component>Hello world!</Component></Component>`,
58
+ {}
59
+ );
60
+
52
61
  chai
53
62
  .expect(code)
54
- .to.equal(`<Fragment>\n<Component bool={true}>Hello world!</Component>\n</Fragment>`);
63
+ .to.equal(`\n<Component bool={true}>\n<Component>Hello world!</Component>\n</Component>\n`);
55
64
  });
56
65
 
57
66
  it('should allow markdown without many spaces', async () => {
@@ -62,10 +71,6 @@ describe('components', () => {
62
71
  {}
63
72
  );
64
73
 
65
- chai
66
- .expect(code)
67
- .to.equal(
68
- `<Fragment>\n<Component><h1 id="hello-world">Hello world!</h1></Component>\n</Fragment>`
69
- );
74
+ chai.expect(code).to.equal(`\n<Component><h1 id="hello-world">Hello world!</h1></Component>\n`);
70
75
  });
71
76
  });
@@ -11,7 +11,7 @@ describe('expressions', () => {
11
11
  it('should be able to serialize expression inside component', async () => {
12
12
  const { code } = await renderMarkdown(`<Component>{a}</Component>`, {});
13
13
 
14
- chai.expect(code).to.equal(`<Fragment>\n<Component>{a}</Component>\n</Fragment>`);
14
+ chai.expect(code).to.equal(`\n<Component>{a}</Component>\n`);
15
15
  });
16
16
 
17
17
  it('should be able to serialize expression inside markdown', async () => {