@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 +6 -0
- package/dist/rehype-escape.js +3 -0
- package/dist/rehype-jsx.js +37 -61
- package/dist/remark-shiki.d.ts +1 -1
- package/package.json +1 -1
- package/src/rehype-escape.ts +5 -0
- package/src/rehype-jsx.ts +39 -42
- package/test/components.test.js +11 -6
- package/test/expressions.test.js +1 -1
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
|
package/dist/rehype-escape.js
CHANGED
|
@@ -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, "<").replace(/>/g, ">");
|
|
9
|
+
});
|
|
7
10
|
}
|
|
8
11
|
return el;
|
|
9
12
|
});
|
package/dist/rehype-jsx.js
CHANGED
|
@@ -1,69 +1,45 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
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
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
if (
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
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
|
-
|
|
52
|
-
|
|
53
|
-
|
|
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
|
-
|
|
57
|
-
|
|
58
|
-
|
|
36
|
+
};
|
|
37
|
+
const closingTag = {
|
|
38
|
+
type: "raw",
|
|
39
|
+
value: `</${child.name}>
|
|
59
40
|
`
|
|
60
|
-
|
|
61
|
-
|
|
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
|
}
|
package/dist/remark-shiki.d.ts
CHANGED
|
@@ -1,3 +1,3 @@
|
|
|
1
1
|
import type { ShikiConfig } from './types.js';
|
|
2
|
-
declare const remarkShiki: ({ langs, theme, wrap }: ShikiConfig, scopedClassName?: string | null
|
|
2
|
+
declare const remarkShiki: ({ langs, theme, wrap }: ShikiConfig, scopedClassName?: string | null) => Promise<() => (tree: any) => void>;
|
|
3
3
|
export default remarkShiki;
|
package/package.json
CHANGED
package/src/rehype-escape.ts
CHANGED
|
@@ -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, '<').replace(/>/g, '>');
|
|
12
|
+
});
|
|
8
13
|
}
|
|
9
14
|
return el;
|
|
10
15
|
});
|
package/src/rehype-jsx.ts
CHANGED
|
@@ -1,51 +1,48 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { visit } from 'unist-util-visit';
|
|
2
2
|
|
|
3
|
-
const MDX_ELEMENTS =
|
|
3
|
+
const MDX_ELEMENTS = ['mdxJsxFlowElement', 'mdxJsxTextElement'];
|
|
4
4
|
export default function rehypeJsx(): any {
|
|
5
5
|
return function (node: any): any {
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
if (
|
|
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
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
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
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
child.
|
|
39
|
-
|
|
40
|
-
|
|
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
|
-
|
|
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
|
}
|
package/test/components.test.js
CHANGED
|
@@ -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(
|
|
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
|
});
|
package/test/expressions.test.js
CHANGED
|
@@ -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(
|
|
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 () => {
|