@astrojs/markdown-remark 0.9.2 → 0.10.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.
- package/CHANGELOG.md +20 -0
- package/LICENSE +61 -0
- package/dist/index.d.ts +4 -10
- package/dist/index.js +30 -10
- package/dist/mdast-util-mdxish.d.ts +2 -0
- package/dist/mdast-util-mdxish.js +14 -0
- package/dist/rehype-collect-headers.d.ts +3 -3
- package/dist/rehype-collect-headers.js +29 -7
- package/dist/rehype-expressions.js +26 -1
- package/dist/rehype-jsx.js +33 -10
- package/dist/remark-mark-and-unravel.d.ts +17 -0
- package/dist/remark-mark-and-unravel.js +45 -0
- package/dist/remark-mdxish.d.ts +1 -0
- package/dist/remark-mdxish.js +15 -0
- package/dist/remark-shiki.js +1 -1
- package/dist/types.d.ts +25 -9
- package/package.json +20 -14
- package/src/index.ts +32 -11
- package/src/mdast-util-mdxish.ts +12 -0
- package/src/rehype-collect-headers.ts +39 -12
- package/src/rehype-expressions.ts +7 -1
- package/src/rehype-jsx.ts +32 -10
- package/src/remark-mark-and-unravel.ts +72 -0
- package/src/remark-mdxish.ts +15 -0
- package/src/remark-shiki.ts +1 -1
- package/src/types.ts +28 -9
- package/test/components.test.js +71 -0
- package/test/expressions.test.js +51 -0
- package/test/plugins.test.js +26 -0
- package/astrojs-markdown-remark-0.9.2.tgz +0 -0
- package/dist/remark-expressions.d.ts +0 -2
- package/dist/remark-expressions.js +0 -25
- package/dist/remark-jsx.d.ts +0 -2
- package/dist/remark-jsx.js +0 -30
- package/dist/remark-slug.d.ts +0 -10
- package/dist/remark-slug.js +0 -23
- package/src/remark-expressions.ts +0 -25
- package/src/remark-jsx.ts +0 -31
- package/src/remark-slug.ts +0 -32
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,25 @@
|
|
|
1
1
|
# @astrojs/markdown-remark
|
|
2
2
|
|
|
3
|
+
## 0.10.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- [#3410](https://github.com/withastro/astro/pull/3410) [`cfae9760`](https://github.com/withastro/astro/commit/cfae9760b252052b6189e96398b819a4337634a8) Thanks [@natemoo-re](https://github.com/natemoo-re)! - Significantally more stable behavior for "Markdown + Components" usage, which now handles component serialization much more similarly to MDX. Also supports switching between Components and Markdown without extra newlines, removes wrapping `<p>` tags from standalone components, and improves JSX expression handling.
|
|
8
|
+
|
|
9
|
+
## 0.9.4
|
|
10
|
+
|
|
11
|
+
### Patch Changes
|
|
12
|
+
|
|
13
|
+
- [#3275](https://github.com/withastro/astro/pull/3275) [`8f8f05c1`](https://github.com/withastro/astro/commit/8f8f05c1b99d073a43af3020ba3922ea2d5b466d) Thanks [@matthewp](https://github.com/matthewp)! - Fixes regression in passing JS args to islands
|
|
14
|
+
|
|
15
|
+
## 0.9.3
|
|
16
|
+
|
|
17
|
+
### Patch Changes
|
|
18
|
+
|
|
19
|
+
- [#3234](https://github.com/withastro/astro/pull/3234) [`de123b28`](https://github.com/withastro/astro/commit/de123b28b3ff398b800cb598f20326ca85a0fb60) Thanks [@JuanM04](https://github.com/JuanM04)! - Removed `rehype-slug` in favor of our own implementation. The behavior of the slugging should remain the same
|
|
20
|
+
|
|
21
|
+
* [#3234](https://github.com/withastro/astro/pull/3234) [`de123b28`](https://github.com/withastro/astro/commit/de123b28b3ff398b800cb598f20326ca85a0fb60) Thanks [@JuanM04](https://github.com/JuanM04)! - Moved some type from `astro` to `@astrojs/markdown-remark`
|
|
22
|
+
|
|
3
23
|
## 0.9.2
|
|
4
24
|
|
|
5
25
|
### Patch Changes
|
package/LICENSE
ADDED
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2021 Fred K. Schott
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
"""
|
|
25
|
+
This license applies to parts of the `packages/create-astro` and `packages/astro` subdirectories originating from the https://github.com/sveltejs/kit repository:
|
|
26
|
+
|
|
27
|
+
Copyright (c) 2020 [these people](https://github.com/sveltejs/kit/graphs/contributors)
|
|
28
|
+
|
|
29
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
30
|
+
|
|
31
|
+
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
|
|
32
|
+
|
|
33
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
34
|
+
"""
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
"""
|
|
38
|
+
This license applies to parts of the `packages/create-astro` and `packages/astro` subdirectories originating from the https://github.com/vitejs/vite repository:
|
|
39
|
+
|
|
40
|
+
MIT License
|
|
41
|
+
|
|
42
|
+
Copyright (c) 2019-present, Yuxi (Evan) You and Vite contributors
|
|
43
|
+
|
|
44
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
45
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
46
|
+
in the Software without restriction, including without limitation the rights
|
|
47
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
48
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
49
|
+
furnished to do so, subject to the following conditions:
|
|
50
|
+
|
|
51
|
+
The above copyright notice and this permission notice shall be included in all
|
|
52
|
+
copies or substantial portions of the Software.
|
|
53
|
+
|
|
54
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
55
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
56
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
57
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
58
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
59
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
60
|
+
SOFTWARE.
|
|
61
|
+
"""
|
package/dist/index.d.ts
CHANGED
|
@@ -1,13 +1,7 @@
|
|
|
1
|
-
import type { MarkdownRenderingOptions } from './types';
|
|
1
|
+
import type { MarkdownRenderingOptions, MarkdownRenderingResult } from './types';
|
|
2
2
|
export * from './types.js';
|
|
3
3
|
export declare const DEFAULT_REMARK_PLUGINS: string[];
|
|
4
|
-
export declare const DEFAULT_REHYPE_PLUGINS:
|
|
4
|
+
export declare const DEFAULT_REHYPE_PLUGINS: never[];
|
|
5
|
+
export declare function slug(value: string): string;
|
|
5
6
|
/** Shared utility for rendering markdown */
|
|
6
|
-
export declare function renderMarkdown(content: string, opts
|
|
7
|
-
metadata: {
|
|
8
|
-
headers: any[];
|
|
9
|
-
source: string;
|
|
10
|
-
html: string;
|
|
11
|
-
};
|
|
12
|
-
code: string;
|
|
13
|
-
}>;
|
|
7
|
+
export declare function renderMarkdown(content: string, opts?: MarkdownRenderingOptions): Promise<MarkdownRenderingResult>;
|
package/dist/index.js
CHANGED
|
@@ -1,9 +1,9 @@
|
|
|
1
1
|
import createCollectHeaders from "./rehype-collect-headers.js";
|
|
2
2
|
import scopedStyles from "./remark-scoped-styles.js";
|
|
3
|
-
import { remarkExpressions, loadRemarkExpressions } from "./remark-expressions.js";
|
|
4
3
|
import rehypeExpressions from "./rehype-expressions.js";
|
|
5
4
|
import rehypeIslands from "./rehype-islands.js";
|
|
6
|
-
import
|
|
5
|
+
import remarkMdxish from "./remark-mdxish.js";
|
|
6
|
+
import remarkMarkAndUnravel from "./remark-mark-and-unravel.js";
|
|
7
7
|
import rehypeJsx from "./rehype-jsx.js";
|
|
8
8
|
import rehypeEscape from "./rehype-escape.js";
|
|
9
9
|
import remarkPrism from "./remark-prism.js";
|
|
@@ -15,17 +15,30 @@ import markdown from "remark-parse";
|
|
|
15
15
|
import markdownToHtml from "remark-rehype";
|
|
16
16
|
import rehypeStringify from "rehype-stringify";
|
|
17
17
|
import rehypeRaw from "rehype-raw";
|
|
18
|
+
import Slugger from "github-slugger";
|
|
19
|
+
import { VFile } from "vfile";
|
|
18
20
|
export * from "./types.js";
|
|
19
21
|
const DEFAULT_REMARK_PLUGINS = ["remark-gfm", "remark-smartypants"];
|
|
20
|
-
const DEFAULT_REHYPE_PLUGINS = [
|
|
21
|
-
|
|
22
|
+
const DEFAULT_REHYPE_PLUGINS = [];
|
|
23
|
+
const slugger = new Slugger();
|
|
24
|
+
function slug(value) {
|
|
25
|
+
return slugger.slug(value);
|
|
26
|
+
}
|
|
27
|
+
async function renderMarkdown(content, opts = {}) {
|
|
22
28
|
var _a;
|
|
23
|
-
let {
|
|
29
|
+
let {
|
|
30
|
+
fileURL,
|
|
31
|
+
mode = "mdx",
|
|
32
|
+
syntaxHighlight = "shiki",
|
|
33
|
+
shikiConfig = {},
|
|
34
|
+
remarkPlugins = [],
|
|
35
|
+
rehypePlugins = []
|
|
36
|
+
} = opts;
|
|
37
|
+
const input = new VFile({ value: content, path: fileURL });
|
|
24
38
|
const scopedClassName = (_a = opts.$) == null ? void 0 : _a.scopedClassName;
|
|
25
39
|
const isMDX = mode === "mdx";
|
|
26
40
|
const { headers, rehypeCollectHeaders } = createCollectHeaders();
|
|
27
|
-
|
|
28
|
-
let parser = unified().use(markdown).use(isMDX ? [remarkJsx, remarkExpressions] : []).use([remarkUnwrap]);
|
|
41
|
+
let parser = unified().use(markdown).use(isMDX ? [remarkMdxish, remarkMarkAndUnravel] : []).use([remarkUnwrap]);
|
|
29
42
|
if (remarkPlugins.length === 0 && rehypePlugins.length === 0) {
|
|
30
43
|
remarkPlugins = [...DEFAULT_REMARK_PLUGINS];
|
|
31
44
|
rehypePlugins = [...DEFAULT_REHYPE_PLUGINS];
|
|
@@ -48,7 +61,13 @@ async function renderMarkdown(content, opts) {
|
|
|
48
61
|
markdownToHtml,
|
|
49
62
|
{
|
|
50
63
|
allowDangerousHtml: true,
|
|
51
|
-
passThrough: [
|
|
64
|
+
passThrough: [
|
|
65
|
+
"raw",
|
|
66
|
+
"mdxFlowExpression",
|
|
67
|
+
"mdxJsxFlowElement",
|
|
68
|
+
"mdxJsxTextElement",
|
|
69
|
+
"mdxTextExpression"
|
|
70
|
+
]
|
|
52
71
|
}
|
|
53
72
|
]
|
|
54
73
|
]);
|
|
@@ -58,7 +77,7 @@ async function renderMarkdown(content, opts) {
|
|
|
58
77
|
parser.use(isMDX ? [rehypeJsx, rehypeExpressions] : [rehypeRaw]).use(rehypeEscape).use(rehypeIslands);
|
|
59
78
|
let result;
|
|
60
79
|
try {
|
|
61
|
-
const vfile = await parser.use([rehypeCollectHeaders]).use(rehypeStringify, { allowDangerousHtml: true }).process(
|
|
80
|
+
const vfile = await parser.use([rehypeCollectHeaders]).use(rehypeStringify, { allowDangerousHtml: true }).process(input);
|
|
62
81
|
result = vfile.toString();
|
|
63
82
|
} catch (err) {
|
|
64
83
|
console.error(err);
|
|
@@ -72,5 +91,6 @@ async function renderMarkdown(content, opts) {
|
|
|
72
91
|
export {
|
|
73
92
|
DEFAULT_REHYPE_PLUGINS,
|
|
74
93
|
DEFAULT_REMARK_PLUGINS,
|
|
75
|
-
renderMarkdown
|
|
94
|
+
renderMarkdown,
|
|
95
|
+
slug
|
|
76
96
|
};
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { mdxExpressionFromMarkdown, mdxExpressionToMarkdown } from "mdast-util-mdx-expression";
|
|
2
|
+
import { mdxJsxFromMarkdown, mdxJsxToMarkdown } from "mdast-util-mdx-jsx";
|
|
3
|
+
function mdxFromMarkdown() {
|
|
4
|
+
return [mdxExpressionFromMarkdown, mdxJsxFromMarkdown];
|
|
5
|
+
}
|
|
6
|
+
function mdxToMarkdown() {
|
|
7
|
+
return {
|
|
8
|
+
extensions: [mdxExpressionToMarkdown, mdxJsxToMarkdown]
|
|
9
|
+
};
|
|
10
|
+
}
|
|
11
|
+
export {
|
|
12
|
+
mdxFromMarkdown,
|
|
13
|
+
mdxToMarkdown
|
|
14
|
+
};
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
|
|
1
|
+
import type { MarkdownHeader, RehypePlugin } from './types.js';
|
|
2
2
|
export default function createCollectHeaders(): {
|
|
3
|
-
headers:
|
|
4
|
-
rehypeCollectHeaders: () =>
|
|
3
|
+
headers: MarkdownHeader[];
|
|
4
|
+
rehypeCollectHeaders: () => ReturnType<RehypePlugin>;
|
|
5
5
|
};
|
|
@@ -1,11 +1,11 @@
|
|
|
1
1
|
import { visit } from "unist-util-visit";
|
|
2
|
-
import
|
|
2
|
+
import Slugger from "github-slugger";
|
|
3
3
|
function createCollectHeaders() {
|
|
4
4
|
const headers = [];
|
|
5
|
+
const slugger = new Slugger();
|
|
5
6
|
function rehypeCollectHeaders() {
|
|
6
7
|
return function(tree) {
|
|
7
8
|
visit(tree, (node) => {
|
|
8
|
-
var _a;
|
|
9
9
|
if (node.type !== "element")
|
|
10
10
|
return;
|
|
11
11
|
const { tagName } = node;
|
|
@@ -15,14 +15,36 @@ function createCollectHeaders() {
|
|
|
15
15
|
if (!level)
|
|
16
16
|
return;
|
|
17
17
|
const depth = Number.parseInt(level);
|
|
18
|
+
let raw = "";
|
|
18
19
|
let text = "";
|
|
19
|
-
|
|
20
|
-
|
|
20
|
+
let isJSX = false;
|
|
21
|
+
visit(node, (child) => {
|
|
22
|
+
if (child.type === "element") {
|
|
23
|
+
return;
|
|
24
|
+
}
|
|
25
|
+
if (child.type === "raw") {
|
|
26
|
+
if (child.value.startsWith("\n<") || child.value.endsWith(">\n")) {
|
|
27
|
+
raw += child.value.replace(/^\n|\n$/g, "");
|
|
28
|
+
return;
|
|
29
|
+
}
|
|
30
|
+
}
|
|
31
|
+
if (child.type === "text" || child.type === "raw") {
|
|
32
|
+
raw += child.value;
|
|
33
|
+
text += child.value;
|
|
34
|
+
isJSX = isJSX || child.value.includes("{");
|
|
35
|
+
}
|
|
21
36
|
});
|
|
22
|
-
let slug = ((_a = node == null ? void 0 : node.properties) == null ? void 0 : _a.id) || slugger.slug(text);
|
|
23
37
|
node.properties = node.properties || {};
|
|
24
|
-
node.properties.id
|
|
25
|
-
|
|
38
|
+
if (typeof node.properties.id !== "string") {
|
|
39
|
+
if (isJSX) {
|
|
40
|
+
node.properties.id = `$$slug(\`${text.replace(/\{/g, "${")}\`)`;
|
|
41
|
+
node.type = "raw";
|
|
42
|
+
node.value = `<${node.tagName} id={${node.properties.id}}>${raw}</${node.tagName}>`;
|
|
43
|
+
} else {
|
|
44
|
+
node.properties.id = slugger.slug(text);
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
headers.push({ depth, slug: node.properties.id, text });
|
|
26
48
|
});
|
|
27
49
|
};
|
|
28
50
|
}
|
|
@@ -1,9 +1,34 @@
|
|
|
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));
|
|
1
20
|
import { map } from "unist-util-map";
|
|
2
21
|
function rehypeExpressions() {
|
|
3
22
|
return function(node) {
|
|
4
23
|
return map(node, (child) => {
|
|
24
|
+
if (child.type === "text") {
|
|
25
|
+
return __spreadProps(__spreadValues({}, child), { type: "raw" });
|
|
26
|
+
}
|
|
5
27
|
if (child.type === "mdxTextExpression") {
|
|
6
|
-
return { type: "
|
|
28
|
+
return { type: "raw", value: `{${child.value}}` };
|
|
29
|
+
}
|
|
30
|
+
if (child.type === "mdxFlowExpression") {
|
|
31
|
+
return { type: "raw", value: `{${child.value}}` };
|
|
7
32
|
}
|
|
8
33
|
return child;
|
|
9
34
|
});
|
package/dist/rehype-jsx.js
CHANGED
|
@@ -26,18 +26,41 @@ function rehypeJsx() {
|
|
|
26
26
|
return __spreadProps(__spreadValues({}, child), { tagName: `${child.tagName}` });
|
|
27
27
|
}
|
|
28
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
|
+
};
|
|
50
|
+
}
|
|
51
|
+
child.children.splice(0, 0, {
|
|
52
|
+
type: "raw",
|
|
53
|
+
value: `
|
|
54
|
+
<${child.name}${attrs}>`
|
|
55
|
+
});
|
|
56
|
+
child.children.push({
|
|
57
|
+
type: "raw",
|
|
58
|
+
value: `</${child.name}>
|
|
59
|
+
`
|
|
60
|
+
});
|
|
29
61
|
return __spreadProps(__spreadValues({}, child), {
|
|
30
62
|
type: "element",
|
|
31
|
-
tagName:
|
|
32
|
-
properties: child.attributes.reduce((acc, entry) => {
|
|
33
|
-
let attr = entry.value;
|
|
34
|
-
if (attr && typeof attr === "object") {
|
|
35
|
-
attr = `{${attr.value}}`;
|
|
36
|
-
} else if (attr === null) {
|
|
37
|
-
attr = `{true}`;
|
|
38
|
-
}
|
|
39
|
-
return Object.assign(acc, { [entry.name]: attr });
|
|
40
|
-
}, {})
|
|
63
|
+
tagName: `Fragment`
|
|
41
64
|
});
|
|
42
65
|
}
|
|
43
66
|
return child;
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @typedef {import('mdast').Root} Root
|
|
3
|
+
* @typedef {import('mdast').Content} Content
|
|
4
|
+
* @typedef {Root|Content} Node
|
|
5
|
+
* @typedef {Extract<Node, import('unist').Parent>} Parent
|
|
6
|
+
*
|
|
7
|
+
* @typedef {import('remark-mdx')} DoNotTouchAsThisImportItIncludesMdxInTree
|
|
8
|
+
*/
|
|
9
|
+
/**
|
|
10
|
+
* A tiny plugin that unravels `<p><h1>x</h1></p>` but also
|
|
11
|
+
* `<p><Component /></p>` (so it has no knowledge of “HTML”).
|
|
12
|
+
* It also marks JSX as being explicitly JSX, so when a user passes a `h1`
|
|
13
|
+
* component, it is used for `# heading` but not for `<h1>heading</h1>`.
|
|
14
|
+
*
|
|
15
|
+
* @type {import('unified').Plugin<Array<void>, Root>}
|
|
16
|
+
*/
|
|
17
|
+
export default function remarkMarkAndUnravel(): (tree: any) => void;
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
import { visit } from "unist-util-visit";
|
|
2
|
+
function remarkMarkAndUnravel() {
|
|
3
|
+
return (tree) => {
|
|
4
|
+
visit(tree, (node, index, parent_) => {
|
|
5
|
+
const parent = parent_;
|
|
6
|
+
let offset = -1;
|
|
7
|
+
let all = true;
|
|
8
|
+
let oneOrMore;
|
|
9
|
+
if (parent && typeof index === "number" && node.type === "paragraph") {
|
|
10
|
+
const children = node.children;
|
|
11
|
+
while (++offset < children.length) {
|
|
12
|
+
const child = children[offset];
|
|
13
|
+
if (child.type === "mdxJsxTextElement" || child.type === "mdxTextExpression") {
|
|
14
|
+
oneOrMore = true;
|
|
15
|
+
} else if (child.type === "text" && /^[\t\r\n ]+$/.test(String(child.value))) {
|
|
16
|
+
} else {
|
|
17
|
+
all = false;
|
|
18
|
+
break;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
if (all && oneOrMore) {
|
|
22
|
+
offset = -1;
|
|
23
|
+
while (++offset < children.length) {
|
|
24
|
+
const child = children[offset];
|
|
25
|
+
if (child.type === "mdxJsxTextElement") {
|
|
26
|
+
child.type = "mdxJsxFlowElement";
|
|
27
|
+
}
|
|
28
|
+
if (child.type === "mdxTextExpression") {
|
|
29
|
+
child.type = "mdxFlowExpression";
|
|
30
|
+
}
|
|
31
|
+
}
|
|
32
|
+
parent.children.splice(index, 1, ...children);
|
|
33
|
+
return index;
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
if (node.type === "mdxJsxFlowElement" || node.type === "mdxJsxTextElement") {
|
|
37
|
+
const data = node.data || (node.data = {});
|
|
38
|
+
data._mdxExplicitJsx = true;
|
|
39
|
+
}
|
|
40
|
+
});
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
export {
|
|
44
|
+
remarkMarkAndUnravel as default
|
|
45
|
+
};
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export default function remarkMdxish(this: any, options?: {}): void;
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { mdxjs } from "micromark-extension-mdxjs";
|
|
2
|
+
import { mdxFromMarkdown, mdxToMarkdown } from "./mdast-util-mdxish.js";
|
|
3
|
+
function remarkMdxish(options = {}) {
|
|
4
|
+
const data = this.data();
|
|
5
|
+
add("micromarkExtensions", mdxjs(options));
|
|
6
|
+
add("fromMarkdownExtensions", mdxFromMarkdown());
|
|
7
|
+
add("toMarkdownExtensions", mdxToMarkdown());
|
|
8
|
+
function add(field, value) {
|
|
9
|
+
const list = data[field] ? data[field] : data[field] = [];
|
|
10
|
+
list.push(value);
|
|
11
|
+
}
|
|
12
|
+
}
|
|
13
|
+
export {
|
|
14
|
+
remarkMdxish as default
|
|
15
|
+
};
|
package/dist/remark-shiki.js
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { getHighlighter } from "shiki";
|
|
2
2
|
import { visit } from "unist-util-visit";
|
|
3
3
|
const highlighterCacheAsync = /* @__PURE__ */ new Map();
|
|
4
|
-
const remarkShiki = async ({ langs, theme, wrap }, scopedClassName) => {
|
|
4
|
+
const remarkShiki = async ({ langs = [], theme = "github-dark", wrap = false }, scopedClassName) => {
|
|
5
5
|
const cacheID = typeof theme === "string" ? theme : theme.name;
|
|
6
6
|
let highlighterAsync = highlighterCacheAsync.get(cacheID);
|
|
7
7
|
if (!highlighterAsync) {
|
package/dist/types.d.ts
CHANGED
|
@@ -8,21 +8,37 @@ export declare type RemarkPlugins = (string | [string, any] | RemarkPlugin | [Re
|
|
|
8
8
|
export declare type RehypePlugin<PluginParameters extends any[] = any[]> = unified.Plugin<PluginParameters, hast.Root>;
|
|
9
9
|
export declare type RehypePlugins = (string | [string, any] | RehypePlugin | [RehypePlugin, any])[];
|
|
10
10
|
export interface ShikiConfig {
|
|
11
|
-
langs
|
|
12
|
-
theme
|
|
13
|
-
wrap
|
|
11
|
+
langs?: ILanguageRegistration[];
|
|
12
|
+
theme?: Theme | IThemeRegistration;
|
|
13
|
+
wrap?: boolean | null;
|
|
14
14
|
}
|
|
15
15
|
export interface AstroMarkdownOptions {
|
|
16
|
-
mode
|
|
17
|
-
drafts
|
|
18
|
-
syntaxHighlight
|
|
19
|
-
shikiConfig
|
|
20
|
-
remarkPlugins
|
|
21
|
-
rehypePlugins
|
|
16
|
+
mode?: 'md' | 'mdx';
|
|
17
|
+
drafts?: boolean;
|
|
18
|
+
syntaxHighlight?: 'shiki' | 'prism' | false;
|
|
19
|
+
shikiConfig?: ShikiConfig;
|
|
20
|
+
remarkPlugins?: RemarkPlugins;
|
|
21
|
+
rehypePlugins?: RehypePlugins;
|
|
22
22
|
}
|
|
23
23
|
export interface MarkdownRenderingOptions extends AstroMarkdownOptions {
|
|
24
|
+
/** @internal */
|
|
25
|
+
fileURL?: URL;
|
|
24
26
|
/** @internal */
|
|
25
27
|
$?: {
|
|
26
28
|
scopedClassName: string | null;
|
|
27
29
|
};
|
|
28
30
|
}
|
|
31
|
+
export interface MarkdownHeader {
|
|
32
|
+
depth: number;
|
|
33
|
+
slug: string;
|
|
34
|
+
text: string;
|
|
35
|
+
}
|
|
36
|
+
export interface MarkdownMetadata {
|
|
37
|
+
headers: MarkdownHeader[];
|
|
38
|
+
source: string;
|
|
39
|
+
html: string;
|
|
40
|
+
}
|
|
41
|
+
export interface MarkdownRenderingResult {
|
|
42
|
+
metadata: MarkdownMetadata;
|
|
43
|
+
code: string;
|
|
44
|
+
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@astrojs/markdown-remark",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.10.0",
|
|
4
4
|
"type": "module",
|
|
5
5
|
"author": "withastro",
|
|
6
6
|
"license": "MIT",
|
|
@@ -15,13 +15,6 @@
|
|
|
15
15
|
"exports": {
|
|
16
16
|
".": "./dist/index.js"
|
|
17
17
|
},
|
|
18
|
-
"scripts": {
|
|
19
|
-
"prepublish": "pnpm build",
|
|
20
|
-
"build": "astro-scripts build \"src/**/*.ts\" && tsc -p tsconfig.json",
|
|
21
|
-
"build:ci": "astro-scripts build \"src/**/*.ts\"",
|
|
22
|
-
"postbuild": "astro-scripts copy \"src/**/*.js\"",
|
|
23
|
-
"dev": "astro-scripts dev \"src/**/*.ts\""
|
|
24
|
-
},
|
|
25
18
|
"dependencies": {
|
|
26
19
|
"@astrojs/prism": "^0.4.1",
|
|
27
20
|
"assert": "^2.0.0",
|
|
@@ -30,9 +23,9 @@
|
|
|
30
23
|
"mdast-util-mdx-jsx": "^1.2.0",
|
|
31
24
|
"mdast-util-to-string": "^3.1.0",
|
|
32
25
|
"micromark-extension-mdx-jsx": "^1.0.3",
|
|
33
|
-
"
|
|
26
|
+
"micromark-extension-mdxjs": "^1.0.0",
|
|
27
|
+
"prismjs": "^1.28.0",
|
|
34
28
|
"rehype-raw": "^6.1.1",
|
|
35
|
-
"rehype-slug": "^5.0.1",
|
|
36
29
|
"rehype-stringify": "^9.0.3",
|
|
37
30
|
"remark-gfm": "^3.0.1",
|
|
38
31
|
"remark-parse": "^10.0.1",
|
|
@@ -40,15 +33,28 @@
|
|
|
40
33
|
"remark-smartypants": "^2.0.0",
|
|
41
34
|
"shiki": "^0.10.1",
|
|
42
35
|
"unified": "^10.1.2",
|
|
43
|
-
"unist-util-map": "^3.
|
|
44
|
-
"unist-util-visit": "^4.1.0"
|
|
36
|
+
"unist-util-map": "^3.1.1",
|
|
37
|
+
"unist-util-visit": "^4.1.0",
|
|
38
|
+
"vfile": "^5.3.2"
|
|
45
39
|
},
|
|
46
40
|
"devDependencies": {
|
|
41
|
+
"@types/chai": "^4.3.1",
|
|
47
42
|
"@types/github-slugger": "^1.3.0",
|
|
48
43
|
"@types/hast": "^2.3.4",
|
|
49
44
|
"@types/mdast": "^3.0.10",
|
|
45
|
+
"@types/mocha": "^9.1.1",
|
|
50
46
|
"@types/prismjs": "^1.26.0",
|
|
51
47
|
"@types/unist": "^2.0.6",
|
|
52
|
-
"astro-scripts": "
|
|
48
|
+
"astro-scripts": "0.0.4",
|
|
49
|
+
"chai": "^4.3.6",
|
|
50
|
+
"mocha": "^9.2.2"
|
|
51
|
+
},
|
|
52
|
+
"scripts": {
|
|
53
|
+
"prepublish": "pnpm build",
|
|
54
|
+
"build": "astro-scripts build \"src/**/*.ts\" && tsc -p tsconfig.json",
|
|
55
|
+
"build:ci": "astro-scripts build \"src/**/*.ts\"",
|
|
56
|
+
"postbuild": "astro-scripts copy \"src/**/*.js\"",
|
|
57
|
+
"dev": "astro-scripts dev \"src/**/*.ts\"",
|
|
58
|
+
"test": "mocha --exit --timeout 20000"
|
|
53
59
|
}
|
|
54
|
-
}
|
|
60
|
+
}
|
package/src/index.ts
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import type { MarkdownRenderingOptions } from './types';
|
|
1
|
+
import type { MarkdownRenderingOptions, MarkdownRenderingResult } from './types';
|
|
2
2
|
|
|
3
3
|
import createCollectHeaders from './rehype-collect-headers.js';
|
|
4
4
|
import scopedStyles from './remark-scoped-styles.js';
|
|
5
|
-
import { remarkExpressions, loadRemarkExpressions } from './remark-expressions.js';
|
|
6
5
|
import rehypeExpressions from './rehype-expressions.js';
|
|
7
6
|
import rehypeIslands from './rehype-islands.js';
|
|
8
|
-
import
|
|
7
|
+
import remarkMdxish from './remark-mdxish.js';
|
|
8
|
+
import remarkMarkAndUnravel from './remark-mark-and-unravel.js';
|
|
9
9
|
import rehypeJsx from './rehype-jsx.js';
|
|
10
10
|
import rehypeEscape from './rehype-escape.js';
|
|
11
11
|
import remarkPrism from './remark-prism.js';
|
|
@@ -18,25 +18,40 @@ import markdown from 'remark-parse';
|
|
|
18
18
|
import markdownToHtml from 'remark-rehype';
|
|
19
19
|
import rehypeStringify from 'rehype-stringify';
|
|
20
20
|
import rehypeRaw from 'rehype-raw';
|
|
21
|
+
import Slugger from 'github-slugger';
|
|
22
|
+
import { VFile } from 'vfile';
|
|
21
23
|
|
|
22
24
|
export * from './types.js';
|
|
23
25
|
|
|
24
26
|
export const DEFAULT_REMARK_PLUGINS = ['remark-gfm', 'remark-smartypants'];
|
|
27
|
+
export const DEFAULT_REHYPE_PLUGINS = [];
|
|
25
28
|
|
|
26
|
-
|
|
29
|
+
const slugger = new Slugger();
|
|
30
|
+
export function slug(value: string): string {
|
|
31
|
+
return slugger.slug(value);
|
|
32
|
+
}
|
|
27
33
|
|
|
28
34
|
/** Shared utility for rendering markdown */
|
|
29
|
-
export async function renderMarkdown(
|
|
30
|
-
|
|
35
|
+
export async function renderMarkdown(
|
|
36
|
+
content: string,
|
|
37
|
+
opts: MarkdownRenderingOptions = {}
|
|
38
|
+
): Promise<MarkdownRenderingResult> {
|
|
39
|
+
let {
|
|
40
|
+
fileURL,
|
|
41
|
+
mode = 'mdx',
|
|
42
|
+
syntaxHighlight = 'shiki',
|
|
43
|
+
shikiConfig = {},
|
|
44
|
+
remarkPlugins = [],
|
|
45
|
+
rehypePlugins = [],
|
|
46
|
+
} = opts;
|
|
47
|
+
const input = new VFile({ value: content, path: fileURL });
|
|
31
48
|
const scopedClassName = opts.$?.scopedClassName;
|
|
32
49
|
const isMDX = mode === 'mdx';
|
|
33
50
|
const { headers, rehypeCollectHeaders } = createCollectHeaders();
|
|
34
51
|
|
|
35
|
-
await Promise.all([loadRemarkExpressions(), loadRemarkJsx()]); // Vite bug: dynamically import() these because of CJS interop (this will cache)
|
|
36
|
-
|
|
37
52
|
let parser = unified()
|
|
38
53
|
.use(markdown)
|
|
39
|
-
.use(isMDX ? [
|
|
54
|
+
.use(isMDX ? [remarkMdxish, remarkMarkAndUnravel] : [])
|
|
40
55
|
.use([remarkUnwrap]);
|
|
41
56
|
|
|
42
57
|
if (remarkPlugins.length === 0 && rehypePlugins.length === 0) {
|
|
@@ -66,7 +81,13 @@ export async function renderMarkdown(content: string, opts: MarkdownRenderingOpt
|
|
|
66
81
|
markdownToHtml as any,
|
|
67
82
|
{
|
|
68
83
|
allowDangerousHtml: true,
|
|
69
|
-
passThrough: [
|
|
84
|
+
passThrough: [
|
|
85
|
+
'raw',
|
|
86
|
+
'mdxFlowExpression',
|
|
87
|
+
'mdxJsxFlowElement',
|
|
88
|
+
'mdxJsxTextElement',
|
|
89
|
+
'mdxTextExpression',
|
|
90
|
+
],
|
|
70
91
|
},
|
|
71
92
|
],
|
|
72
93
|
]);
|
|
@@ -85,7 +106,7 @@ export async function renderMarkdown(content: string, opts: MarkdownRenderingOpt
|
|
|
85
106
|
const vfile = await parser
|
|
86
107
|
.use([rehypeCollectHeaders])
|
|
87
108
|
.use(rehypeStringify, { allowDangerousHtml: true })
|
|
88
|
-
.process(
|
|
109
|
+
.process(input);
|
|
89
110
|
result = vfile.toString();
|
|
90
111
|
} catch (err) {
|
|
91
112
|
console.error(err);
|