@astrojs/mdx 0.4.0 → 0.5.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/.turbo/turbo-build.log +5 -5
- package/CHANGELOG.md +17 -0
- package/README.md +35 -2
- package/dist/index.js +30 -24
- package/dist/rehype-collect-headings.d.ts +6 -0
- package/dist/rehype-collect-headings.js +47 -0
- package/dist/remark-prism.js +2 -34
- package/dist/utils.d.ts +3 -0
- package/dist/utils.js +20 -1
- package/package.json +7 -4
- package/src/index.ts +35 -30
- package/src/rehype-collect-headings.ts +50 -0
- package/src/remark-prism.ts +2 -44
- package/src/utils.ts +25 -0
- package/test/fixtures/mdx-get-headings/src/pages/pages.json.js +11 -0
- package/test/fixtures/mdx-get-headings/src/pages/test-with-jsx-expressions.mdx +8 -0
- package/test/fixtures/mdx-get-headings/src/pages/test.mdx +9 -0
- package/test/fixtures/mdx-page/astro.config.ts +5 -0
- package/test/fixtures/mdx-page/node_modules/.bin/astro +17 -0
- package/test/fixtures/mdx-page/package.json +7 -0
- package/test/fixtures/mdx-rehype-plugins/src/pages/reading-time.json.js +7 -0
- package/test/fixtures/mdx-rehype-plugins/src/pages/space-ipsum.mdx +25 -0
- package/test/mdx-get-headings.test.js +60 -0
- package/test/mdx-page.test.js +0 -1
- package/test/mdx-rehype-plugins.test.js +81 -0
package/.turbo/turbo-build.log
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
[
|
|
2
|
-
[
|
|
3
|
-
[
|
|
4
|
-
[
|
|
5
|
-
[
|
|
1
|
+
[34m@astrojs/mdx:build: [0mcache hit, replaying output [2m87ac7debf5e14016[0m
|
|
2
|
+
[34m@astrojs/mdx:build: [0m
|
|
3
|
+
[34m@astrojs/mdx:build: [0m> @astrojs/mdx@0.5.0 build /home/runner/work/astro/astro/packages/integrations/mdx
|
|
4
|
+
[34m@astrojs/mdx:build: [0m> astro-scripts build "src/**/*.ts" && tsc
|
|
5
|
+
[34m@astrojs/mdx:build: [0m
|
package/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,22 @@
|
|
|
1
1
|
# @astrojs/mdx
|
|
2
2
|
|
|
3
|
+
## 0.5.0
|
|
4
|
+
|
|
5
|
+
### Minor Changes
|
|
6
|
+
|
|
7
|
+
- [#4095](https://github.com/withastro/astro/pull/4095) [`40ef43a59`](https://github.com/withastro/astro/commit/40ef43a59b08a1a8fbcd9f4a53745a9636a4fbb9) Thanks [@bholmesdev](https://github.com/bholmesdev)! - Add IDs to MDX headings and expose via getHeadings() export
|
|
8
|
+
|
|
9
|
+
* [#4114](https://github.com/withastro/astro/pull/4114) [`64432bcb8`](https://github.com/withastro/astro/commit/64432bcb873efd0e4297c00fc9583a1fe516dfe7) Thanks [@Princesseuh](https://github.com/Princesseuh)! - Refactor `@astrojs/mdx` and `@astrojs/markdown-remark` to use `@astrojs/prism` instead of duplicating the code
|
|
10
|
+
|
|
11
|
+
### Patch Changes
|
|
12
|
+
|
|
13
|
+
- [#4112](https://github.com/withastro/astro/pull/4112) [`e33fc9bc4`](https://github.com/withastro/astro/commit/e33fc9bc46ff0a30013deb6dc76e545e70cc3a3e) Thanks [@matthewp](https://github.com/matthewp)! - Fix MDX working with a ts config file
|
|
14
|
+
|
|
15
|
+
* [#4049](https://github.com/withastro/astro/pull/4049) [`b60cc0538`](https://github.com/withastro/astro/commit/b60cc0538bc5c68dd411117780d20d892530789d) Thanks [@natemoo-re](https://github.com/natemoo-re)! - Improve `injectScript` handling for non-Astro pages
|
|
16
|
+
|
|
17
|
+
* Updated dependencies [[`64432bcb8`](https://github.com/withastro/astro/commit/64432bcb873efd0e4297c00fc9583a1fe516dfe7)]:
|
|
18
|
+
- @astrojs/prism@0.7.0
|
|
19
|
+
|
|
3
20
|
## 0.4.0
|
|
4
21
|
|
|
5
22
|
### Minor Changes
|
package/README.md
CHANGED
|
@@ -103,6 +103,24 @@ const posts = await Astro.glob('./*.mdx');
|
|
|
103
103
|
|
|
104
104
|
See [the official "how MDX works" guide](https://mdxjs.com/docs/using-mdx/#how-mdx-works) for more on MDX variables.
|
|
105
105
|
|
|
106
|
+
### Exported properties
|
|
107
|
+
|
|
108
|
+
Alongside your [MDX variable exports](#variables), we generate a few helpful exports as well. These are accessible when importing an MDX file via `import` statements or [`Astro.glob`](https://docs.astro.build/en/reference/api-reference/#astroglob).
|
|
109
|
+
|
|
110
|
+
#### `file`
|
|
111
|
+
|
|
112
|
+
The absolute path to the MDX file (e.g. `home/user/projects/.../file.md`).
|
|
113
|
+
|
|
114
|
+
#### `url`
|
|
115
|
+
|
|
116
|
+
The browser-ready URL for MDX files under `src/pages/`. For example, `src/pages/en/about.mdx` will provide a `url` of `/en/about/`. For MDX files outside of `src/pages`, `url` will be `undefined`.
|
|
117
|
+
|
|
118
|
+
#### `getHeadings()`
|
|
119
|
+
|
|
120
|
+
**Returns:** `{ depth: number; slug: string; text: string }[]`
|
|
121
|
+
|
|
122
|
+
A function that returns an array of all headings (i.e. `h1 -> h6` elements) in the MDX file. Each heading’s `slug` corresponds to the generated ID for a given heading and can be used for anchor links.
|
|
123
|
+
|
|
106
124
|
### Frontmatter
|
|
107
125
|
|
|
108
126
|
Astro also supports YAML-based frontmatter out-of-the-box using the [remark-mdx-frontmatter](https://github.com/remcohaszing/remark-mdx-frontmatter) plugin. By default, all variables declared in a frontmatter fence (`---`) will be accessible via the `frontmatter` export. See the `frontmatterOptions` configuration to customize this behavior.
|
|
@@ -279,11 +297,26 @@ export default {
|
|
|
279
297
|
<details>
|
|
280
298
|
<summary><strong>rehypePlugins</strong></summary>
|
|
281
299
|
|
|
282
|
-
**Default plugins:**
|
|
300
|
+
**Default plugins:** [`collect-headings`](https://github.com/withastro/astro/blob/main/packages/integrations/mdx/src/rehype-collect-headings.ts)
|
|
283
301
|
|
|
284
302
|
[Rehype plugins](https://github.com/rehypejs/rehype/blob/main/doc/plugins.md) allow you to transform the HTML that your Markdown generates. We recommend checking the [Remark plugin](https://github.com/remarkjs/remark/blob/main/doc/plugins.md) catalog first _before_ considering rehype plugins, since most users want to transform their Markdown syntax instead. If HTML transforms are what you need, we encourage you to browse [awesome-rehype](https://github.com/rehypejs/awesome-rehype) for a full curated list of plugins!
|
|
285
303
|
|
|
286
|
-
|
|
304
|
+
We apply our own [`collect-headings`](https://github.com/withastro/astro/blob/main/packages/integrations/mdx/src/rehype-collect-headings.ts) plugin by default. This applies IDs to all headings (i.e. `h1 -> h6`) in your MDX files to [link to headings via anchor tags](https://developer.mozilla.org/en-US/docs/Web/HTML/Element/a#linking_to_an_element_on_the_same_page).
|
|
305
|
+
|
|
306
|
+
To apply rehype plugins _while preserving_ Astro's default plugins, use a nested `extends` object like so:
|
|
307
|
+
|
|
308
|
+
```js
|
|
309
|
+
// astro.config.mjs
|
|
310
|
+
import rehypeMinifyHtml from 'rehype-minify';
|
|
311
|
+
|
|
312
|
+
export default {
|
|
313
|
+
integrations: [mdx({
|
|
314
|
+
rehypePlugins: { extends: [rehypeMinifyHtml] },
|
|
315
|
+
})],
|
|
316
|
+
}
|
|
317
|
+
```
|
|
318
|
+
|
|
319
|
+
To apply plugins _without_ Astro's defaults, you can apply a plain array:
|
|
287
320
|
|
|
288
321
|
```js
|
|
289
322
|
// astro.config.mjs
|
package/dist/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { nodeTypes } from "@mdx-js/mdx";
|
|
1
|
+
import { compile as mdxCompile, nodeTypes } from "@mdx-js/mdx";
|
|
2
2
|
import mdxPlugin from "@mdx-js/rollup";
|
|
3
3
|
import { parse as parseESM } from "es-module-lexer";
|
|
4
4
|
import rehypeRaw from "rehype-raw";
|
|
@@ -7,9 +7,12 @@ import remarkGfm from "remark-gfm";
|
|
|
7
7
|
import remarkMdxFrontmatter from "remark-mdx-frontmatter";
|
|
8
8
|
import remarkShikiTwoslash from "remark-shiki-twoslash";
|
|
9
9
|
import remarkSmartypants from "remark-smartypants";
|
|
10
|
+
import { VFile } from "vfile";
|
|
11
|
+
import rehypeCollectHeadings from "./rehype-collect-headings.js";
|
|
10
12
|
import remarkPrism from "./remark-prism.js";
|
|
11
13
|
import { getFileInfo, getFrontmatter } from "./utils.js";
|
|
12
14
|
const DEFAULT_REMARK_PLUGINS = [remarkGfm, remarkSmartypants];
|
|
15
|
+
const DEFAULT_REHYPE_PLUGINS = [rehypeCollectHeadings];
|
|
13
16
|
function handleExtends(config, defaults = []) {
|
|
14
17
|
if (Array.isArray(config))
|
|
15
18
|
return config;
|
|
@@ -22,10 +25,10 @@ function mdx(mdxOptions = {}) {
|
|
|
22
25
|
"astro:config:setup": ({ updateConfig, config, addPageExtension, command }) => {
|
|
23
26
|
addPageExtension(".mdx");
|
|
24
27
|
let remarkPlugins = handleExtends(mdxOptions.remarkPlugins, DEFAULT_REMARK_PLUGINS);
|
|
25
|
-
let rehypePlugins = handleExtends(mdxOptions.rehypePlugins);
|
|
28
|
+
let rehypePlugins = handleExtends(mdxOptions.rehypePlugins, DEFAULT_REHYPE_PLUGINS);
|
|
26
29
|
if (config.markdown.syntaxHighlight === "shiki") {
|
|
27
30
|
remarkPlugins.push([
|
|
28
|
-
remarkShikiTwoslash.default,
|
|
31
|
+
remarkShikiTwoslash.default ?? remarkShikiTwoslash,
|
|
29
32
|
config.markdown.shikiConfig
|
|
30
33
|
]);
|
|
31
34
|
rehypePlugins.push([rehypeRaw, { passThrough: nodeTypes }]);
|
|
@@ -42,51 +45,54 @@ function mdx(mdxOptions = {}) {
|
|
|
42
45
|
...mdxOptions.frontmatterOptions
|
|
43
46
|
}
|
|
44
47
|
]);
|
|
45
|
-
const
|
|
48
|
+
const mdxPluginOpts = {
|
|
46
49
|
remarkPlugins,
|
|
47
50
|
rehypePlugins,
|
|
48
51
|
jsx: true,
|
|
49
52
|
jsxImportSource: "astro",
|
|
50
53
|
format: "mdx",
|
|
51
54
|
mdExtensions: []
|
|
52
|
-
}
|
|
55
|
+
};
|
|
53
56
|
updateConfig({
|
|
54
57
|
vite: {
|
|
55
58
|
plugins: [
|
|
56
59
|
{
|
|
57
60
|
enforce: "pre",
|
|
58
|
-
...
|
|
61
|
+
...mdxPlugin(mdxPluginOpts),
|
|
59
62
|
async transform(code, id) {
|
|
60
|
-
var _a
|
|
61
|
-
if (!id.endsWith("
|
|
63
|
+
var _a;
|
|
64
|
+
if (!id.endsWith("mdx"))
|
|
62
65
|
return;
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
if (frontmatter.layout) {
|
|
69
|
-
const { layout, ...content } = frontmatter;
|
|
70
|
-
code += `
|
|
66
|
+
if (!((_a = mdxOptions.frontmatterOptions) == null ? void 0 : _a.parsers)) {
|
|
67
|
+
const frontmatter = getFrontmatter(code, id);
|
|
68
|
+
if (frontmatter.layout) {
|
|
69
|
+
const { layout, ...content } = frontmatter;
|
|
70
|
+
code += `
|
|
71
71
|
export default async function({ children }) {
|
|
72
72
|
const Layout = (await import(${JSON.stringify(
|
|
73
|
-
|
|
74
|
-
|
|
73
|
+
frontmatter.layout
|
|
74
|
+
)})).default;
|
|
75
75
|
return <Layout content={${JSON.stringify(
|
|
76
|
-
|
|
77
|
-
|
|
76
|
+
content
|
|
77
|
+
)}}>{children}</Layout> }`;
|
|
78
|
+
}
|
|
78
79
|
}
|
|
79
|
-
|
|
80
|
+
const compiled = await mdxCompile(
|
|
81
|
+
new VFile({ value: code, path: id }),
|
|
82
|
+
mdxPluginOpts
|
|
83
|
+
);
|
|
84
|
+
return {
|
|
85
|
+
code: String(compiled.value),
|
|
86
|
+
map: compiled.map
|
|
87
|
+
};
|
|
80
88
|
}
|
|
81
89
|
},
|
|
82
90
|
{
|
|
83
|
-
name: "@astrojs/mdx",
|
|
91
|
+
name: "@astrojs/mdx-postprocess",
|
|
84
92
|
transform(code, id) {
|
|
85
93
|
if (!id.endsWith(".mdx"))
|
|
86
94
|
return;
|
|
87
95
|
const [, moduleExports] = parseESM(code);
|
|
88
|
-
code += `
|
|
89
|
-
import "${"astro:scripts/page-ssr.js"}";`;
|
|
90
96
|
const { fileUrl, fileId } = getFileInfo(id, config);
|
|
91
97
|
if (!moduleExports.includes("url")) {
|
|
92
98
|
code += `
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
import Slugger from "github-slugger";
|
|
2
|
+
import { visit } from "unist-util-visit";
|
|
3
|
+
import { jsToTreeNode } from "./utils.js";
|
|
4
|
+
function rehypeCollectHeadings() {
|
|
5
|
+
const slugger = new Slugger();
|
|
6
|
+
return function(tree) {
|
|
7
|
+
const headings = [];
|
|
8
|
+
visit(tree, (node) => {
|
|
9
|
+
if (node.type !== "element")
|
|
10
|
+
return;
|
|
11
|
+
const { tagName } = node;
|
|
12
|
+
if (tagName[0] !== "h")
|
|
13
|
+
return;
|
|
14
|
+
const [_, level] = tagName.match(/h([0-6])/) ?? [];
|
|
15
|
+
if (!level)
|
|
16
|
+
return;
|
|
17
|
+
const depth = Number.parseInt(level);
|
|
18
|
+
let text = "";
|
|
19
|
+
visit(node, (child, __, parent) => {
|
|
20
|
+
if (child.type === "element" || parent == null) {
|
|
21
|
+
return;
|
|
22
|
+
}
|
|
23
|
+
if (child.type === "raw" && child.value.match(/^\n?<.*>\n?$/)) {
|
|
24
|
+
return;
|
|
25
|
+
}
|
|
26
|
+
if ((/* @__PURE__ */ new Set(["text", "raw", "mdxTextExpression"])).has(child.type)) {
|
|
27
|
+
text += child.value;
|
|
28
|
+
}
|
|
29
|
+
});
|
|
30
|
+
node.properties = node.properties || {};
|
|
31
|
+
if (typeof node.properties.id !== "string") {
|
|
32
|
+
let slug = slugger.slug(text);
|
|
33
|
+
if (slug.endsWith("-")) {
|
|
34
|
+
slug = slug.slice(0, -1);
|
|
35
|
+
}
|
|
36
|
+
node.properties.id = slug;
|
|
37
|
+
}
|
|
38
|
+
headings.push({ depth, slug: node.properties.id, text });
|
|
39
|
+
});
|
|
40
|
+
tree.children.unshift(
|
|
41
|
+
jsToTreeNode(`export function getHeadings() { return ${JSON.stringify(headings)} }`)
|
|
42
|
+
);
|
|
43
|
+
};
|
|
44
|
+
}
|
|
45
|
+
export {
|
|
46
|
+
rehypeCollectHeadings as default
|
|
47
|
+
};
|
package/dist/remark-prism.js
CHANGED
|
@@ -1,42 +1,10 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import Prism from "prismjs";
|
|
3
|
-
import loadLanguages from "prismjs/components/index.js";
|
|
1
|
+
import { runHighlighterWithAstro } from "@astrojs/prism/dist/highlighter";
|
|
4
2
|
import { visit } from "unist-util-visit";
|
|
5
|
-
const languageMap = /* @__PURE__ */ new Map([["ts", "typescript"]]);
|
|
6
|
-
function runHighlighter(lang, code) {
|
|
7
|
-
let classLanguage = `language-${lang}`;
|
|
8
|
-
if (lang == null) {
|
|
9
|
-
lang = "plaintext";
|
|
10
|
-
}
|
|
11
|
-
const ensureLoaded = (language) => {
|
|
12
|
-
if (language && !Prism.languages[language]) {
|
|
13
|
-
loadLanguages([language]);
|
|
14
|
-
}
|
|
15
|
-
};
|
|
16
|
-
if (languageMap.has(lang)) {
|
|
17
|
-
ensureLoaded(languageMap.get(lang));
|
|
18
|
-
} else if (lang === "astro") {
|
|
19
|
-
ensureLoaded("typescript");
|
|
20
|
-
addAstro(Prism);
|
|
21
|
-
} else {
|
|
22
|
-
ensureLoaded("markup-templating");
|
|
23
|
-
ensureLoaded(lang);
|
|
24
|
-
}
|
|
25
|
-
if (lang && !Prism.languages[lang]) {
|
|
26
|
-
console.warn(`Unable to load the language: ${lang}`);
|
|
27
|
-
}
|
|
28
|
-
const grammar = Prism.languages[lang];
|
|
29
|
-
let html = code;
|
|
30
|
-
if (grammar) {
|
|
31
|
-
html = Prism.highlight(code, grammar, lang);
|
|
32
|
-
}
|
|
33
|
-
return { classLanguage, html };
|
|
34
|
-
}
|
|
35
3
|
function remarkPrism() {
|
|
36
4
|
return (tree) => visit(tree, "code", (node) => {
|
|
37
5
|
let { lang, value } = node;
|
|
38
6
|
node.type = "html";
|
|
39
|
-
let { html, classLanguage } =
|
|
7
|
+
let { html, classLanguage } = runHighlighterWithAstro(lang, value);
|
|
40
8
|
let classes = [classLanguage];
|
|
41
9
|
node.value = `<pre class="${classes.join(
|
|
42
10
|
" "
|
package/dist/utils.d.ts
CHANGED
|
@@ -1,4 +1,6 @@
|
|
|
1
|
+
import type { Options as AcornOpts } from 'acorn';
|
|
1
2
|
import type { AstroConfig } from 'astro';
|
|
3
|
+
import type { MdxjsEsm } from 'mdast-util-mdx';
|
|
2
4
|
interface FileInfo {
|
|
3
5
|
fileId: string;
|
|
4
6
|
fileUrl: string;
|
|
@@ -12,4 +14,5 @@ export declare function getFileInfo(id: string, config: AstroConfig): FileInfo;
|
|
|
12
14
|
export declare function getFrontmatter(code: string, id: string): {
|
|
13
15
|
[key: string]: any;
|
|
14
16
|
};
|
|
17
|
+
export declare function jsToTreeNode(jsString: string, acornOpts?: AcornOpts): MdxjsEsm;
|
|
15
18
|
export {};
|
package/dist/utils.js
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { parse } from "acorn";
|
|
1
2
|
import matter from "gray-matter";
|
|
2
3
|
function appendForwardSlash(path) {
|
|
3
4
|
return path.endsWith("/") ? path : path + "/";
|
|
@@ -41,7 +42,25 @@ function getFrontmatter(code, id) {
|
|
|
41
42
|
}
|
|
42
43
|
}
|
|
43
44
|
}
|
|
45
|
+
function jsToTreeNode(jsString, acornOpts = {
|
|
46
|
+
ecmaVersion: "latest",
|
|
47
|
+
sourceType: "module"
|
|
48
|
+
}) {
|
|
49
|
+
return {
|
|
50
|
+
type: "mdxjsEsm",
|
|
51
|
+
value: "",
|
|
52
|
+
data: {
|
|
53
|
+
estree: {
|
|
54
|
+
body: [],
|
|
55
|
+
...parse(jsString, acornOpts),
|
|
56
|
+
type: "Program",
|
|
57
|
+
sourceType: "module"
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
};
|
|
61
|
+
}
|
|
44
62
|
export {
|
|
45
63
|
getFileInfo,
|
|
46
|
-
getFrontmatter
|
|
64
|
+
getFrontmatter,
|
|
65
|
+
jsToTreeNode
|
|
47
66
|
};
|
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.
|
|
4
|
+
"version": "0.5.0",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"types": "./dist/index.d.ts",
|
|
7
7
|
"author": "withastro",
|
|
@@ -24,12 +24,13 @@
|
|
|
24
24
|
"./package.json": "./package.json"
|
|
25
25
|
},
|
|
26
26
|
"dependencies": {
|
|
27
|
-
"@astrojs/prism": "^0.
|
|
27
|
+
"@astrojs/prism": "^0.7.0",
|
|
28
28
|
"@mdx-js/mdx": "^2.1.2",
|
|
29
29
|
"@mdx-js/rollup": "^2.1.1",
|
|
30
|
+
"acorn": "^8.8.0",
|
|
30
31
|
"es-module-lexer": "^0.10.5",
|
|
32
|
+
"github-slugger": "^1.4.0",
|
|
31
33
|
"gray-matter": "^4.0.3",
|
|
32
|
-
"prismjs": "^1.28.0",
|
|
33
34
|
"rehype-raw": "^6.1.1",
|
|
34
35
|
"remark-frontmatter": "^4.0.1",
|
|
35
36
|
"remark-gfm": "^3.0.1",
|
|
@@ -43,11 +44,13 @@
|
|
|
43
44
|
"@types/chai": "^4.3.1",
|
|
44
45
|
"@types/mocha": "^9.1.1",
|
|
45
46
|
"@types/yargs-parser": "^21.0.0",
|
|
46
|
-
"astro": "1.0.0-rc.
|
|
47
|
+
"astro": "1.0.0-rc.4",
|
|
47
48
|
"astro-scripts": "0.0.6",
|
|
48
49
|
"chai": "^4.3.6",
|
|
49
50
|
"linkedom": "^0.14.12",
|
|
51
|
+
"mdast-util-to-string": "^3.1.0",
|
|
50
52
|
"mocha": "^9.2.2",
|
|
53
|
+
"reading-time": "^1.5.0",
|
|
51
54
|
"remark-toc": "^8.0.1"
|
|
52
55
|
},
|
|
53
56
|
"engines": {
|
package/src/index.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { nodeTypes } from '@mdx-js/mdx';
|
|
1
|
+
import { compile as mdxCompile, nodeTypes } from '@mdx-js/mdx';
|
|
2
2
|
import mdxPlugin, { Options as MdxRollupPluginOptions } from '@mdx-js/rollup';
|
|
3
3
|
import type { AstroIntegration } from 'astro';
|
|
4
4
|
import { parse as parseESM } from 'es-module-lexer';
|
|
@@ -9,7 +9,9 @@ import type { RemarkMdxFrontmatterOptions } from 'remark-mdx-frontmatter';
|
|
|
9
9
|
import remarkMdxFrontmatter from 'remark-mdx-frontmatter';
|
|
10
10
|
import remarkShikiTwoslash from 'remark-shiki-twoslash';
|
|
11
11
|
import remarkSmartypants from 'remark-smartypants';
|
|
12
|
+
import { VFile } from 'vfile';
|
|
12
13
|
import type { Plugin as VitePlugin } from 'vite';
|
|
14
|
+
import rehypeCollectHeadings from './rehype-collect-headings.js';
|
|
13
15
|
import remarkPrism from './remark-prism.js';
|
|
14
16
|
import { getFileInfo, getFrontmatter } from './utils.js';
|
|
15
17
|
|
|
@@ -27,6 +29,7 @@ type MdxOptions = {
|
|
|
27
29
|
};
|
|
28
30
|
|
|
29
31
|
const DEFAULT_REMARK_PLUGINS = [remarkGfm, remarkSmartypants];
|
|
32
|
+
const DEFAULT_REHYPE_PLUGINS = [rehypeCollectHeadings];
|
|
30
33
|
|
|
31
34
|
function handleExtends<T>(config: WithExtends<T[] | undefined>, defaults: T[] = []): T[] {
|
|
32
35
|
if (Array.isArray(config)) return config;
|
|
@@ -41,7 +44,7 @@ export default function mdx(mdxOptions: MdxOptions = {}): AstroIntegration {
|
|
|
41
44
|
'astro:config:setup': ({ updateConfig, config, addPageExtension, command }: any) => {
|
|
42
45
|
addPageExtension('.mdx');
|
|
43
46
|
let remarkPlugins = handleExtends(mdxOptions.remarkPlugins, DEFAULT_REMARK_PLUGINS);
|
|
44
|
-
let rehypePlugins = handleExtends(mdxOptions.rehypePlugins);
|
|
47
|
+
let rehypePlugins = handleExtends(mdxOptions.rehypePlugins, DEFAULT_REHYPE_PLUGINS);
|
|
45
48
|
|
|
46
49
|
if (config.markdown.syntaxHighlight === 'shiki') {
|
|
47
50
|
remarkPlugins.push([
|
|
@@ -49,7 +52,7 @@ export default function mdx(mdxOptions: MdxOptions = {}): AstroIntegration {
|
|
|
49
52
|
// Workarounds tried:
|
|
50
53
|
// - "import * as remarkShikiTwoslash"
|
|
51
54
|
// - "import { default as remarkShikiTwoslash }"
|
|
52
|
-
(remarkShikiTwoslash as any).default,
|
|
55
|
+
(remarkShikiTwoslash as any).default ?? remarkShikiTwoslash,
|
|
53
56
|
config.markdown.shikiConfig,
|
|
54
57
|
]);
|
|
55
58
|
rehypePlugins.push([rehypeRaw, { passThrough: nodeTypes }]);
|
|
@@ -69,7 +72,7 @@ export default function mdx(mdxOptions: MdxOptions = {}): AstroIntegration {
|
|
|
69
72
|
},
|
|
70
73
|
]);
|
|
71
74
|
|
|
72
|
-
const
|
|
75
|
+
const mdxPluginOpts: MdxRollupPluginOptions = {
|
|
73
76
|
remarkPlugins,
|
|
74
77
|
rehypePlugins,
|
|
75
78
|
jsx: true,
|
|
@@ -77,49 +80,51 @@ export default function mdx(mdxOptions: MdxOptions = {}): AstroIntegration {
|
|
|
77
80
|
// Note: disable `.md` support
|
|
78
81
|
format: 'mdx',
|
|
79
82
|
mdExtensions: [],
|
|
80
|
-
}
|
|
83
|
+
};
|
|
81
84
|
|
|
82
85
|
updateConfig({
|
|
83
86
|
vite: {
|
|
84
87
|
plugins: [
|
|
85
88
|
{
|
|
86
89
|
enforce: 'pre',
|
|
87
|
-
...
|
|
88
|
-
// Override transform to
|
|
89
|
-
|
|
90
|
-
|
|
90
|
+
...mdxPlugin(mdxPluginOpts),
|
|
91
|
+
// Override transform to alter code before MDX compilation
|
|
92
|
+
// ex. inject layouts
|
|
93
|
+
async transform(code, id) {
|
|
94
|
+
if (!id.endsWith('mdx')) return;
|
|
91
95
|
|
|
92
|
-
const mdxPluginTransform = configuredMdxPlugin.transform?.bind(this);
|
|
93
96
|
// If user overrides our default YAML parser,
|
|
94
97
|
// do not attempt to parse the `layout` via gray-matter
|
|
95
|
-
if (mdxOptions.frontmatterOptions?.parsers) {
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
)}}>{children}</Layout> }`;
|
|
98
|
+
if (!mdxOptions.frontmatterOptions?.parsers) {
|
|
99
|
+
const frontmatter = getFrontmatter(code, id);
|
|
100
|
+
if (frontmatter.layout) {
|
|
101
|
+
const { layout, ...content } = frontmatter;
|
|
102
|
+
code += `\nexport default async function({ children }) {\nconst Layout = (await import(${JSON.stringify(
|
|
103
|
+
frontmatter.layout
|
|
104
|
+
)})).default;\nreturn <Layout content={${JSON.stringify(
|
|
105
|
+
content
|
|
106
|
+
)}}>{children}</Layout> }`;
|
|
107
|
+
}
|
|
106
108
|
}
|
|
107
|
-
|
|
109
|
+
|
|
110
|
+
const compiled = await mdxCompile(
|
|
111
|
+
new VFile({ value: code, path: id }),
|
|
112
|
+
mdxPluginOpts
|
|
113
|
+
);
|
|
114
|
+
|
|
115
|
+
return {
|
|
116
|
+
code: String(compiled.value),
|
|
117
|
+
map: compiled.map,
|
|
118
|
+
};
|
|
108
119
|
},
|
|
109
120
|
},
|
|
110
121
|
{
|
|
111
|
-
name: '@astrojs/mdx',
|
|
122
|
+
name: '@astrojs/mdx-postprocess',
|
|
123
|
+
// These transforms must happen *after* JSX runtime transformations
|
|
112
124
|
transform(code, id) {
|
|
113
125
|
if (!id.endsWith('.mdx')) return;
|
|
114
126
|
const [, moduleExports] = parseESM(code);
|
|
115
127
|
|
|
116
|
-
// This adds support for injected "page-ssr" scripts in MDX files.
|
|
117
|
-
// TODO: This should only be happening on page entrypoints, not all imported MDX.
|
|
118
|
-
// TODO: This code is copy-pasted across all Astro/Vite plugins that deal with page
|
|
119
|
-
// entrypoints (.astro, .md, .mdx). This should be handled in some centralized place,
|
|
120
|
-
// or otherwise refactored to not require copy-paste handling logic.
|
|
121
|
-
code += `\nimport "${'astro:scripts/page-ssr.js'}";`;
|
|
122
|
-
|
|
123
128
|
const { fileUrl, fileId } = getFileInfo(id, config);
|
|
124
129
|
if (!moduleExports.includes('url')) {
|
|
125
130
|
code += `\nexport const url = ${JSON.stringify(fileUrl)};`;
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import Slugger from 'github-slugger';
|
|
2
|
+
import { visit } from 'unist-util-visit';
|
|
3
|
+
import { jsToTreeNode } from './utils.js';
|
|
4
|
+
|
|
5
|
+
export interface MarkdownHeading {
|
|
6
|
+
depth: number;
|
|
7
|
+
slug: string;
|
|
8
|
+
text: string;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export default function rehypeCollectHeadings() {
|
|
12
|
+
const slugger = new Slugger();
|
|
13
|
+
return function (tree: any) {
|
|
14
|
+
const headings: MarkdownHeading[] = [];
|
|
15
|
+
visit(tree, (node) => {
|
|
16
|
+
if (node.type !== 'element') return;
|
|
17
|
+
const { tagName } = node;
|
|
18
|
+
if (tagName[0] !== 'h') return;
|
|
19
|
+
const [_, level] = tagName.match(/h([0-6])/) ?? [];
|
|
20
|
+
if (!level) return;
|
|
21
|
+
const depth = Number.parseInt(level);
|
|
22
|
+
|
|
23
|
+
let text = '';
|
|
24
|
+
visit(node, (child, __, parent) => {
|
|
25
|
+
if (child.type === 'element' || parent == null) {
|
|
26
|
+
return;
|
|
27
|
+
}
|
|
28
|
+
if (child.type === 'raw' && child.value.match(/^\n?<.*>\n?$/)) {
|
|
29
|
+
return;
|
|
30
|
+
}
|
|
31
|
+
if (new Set(['text', 'raw', 'mdxTextExpression']).has(child.type)) {
|
|
32
|
+
text += child.value;
|
|
33
|
+
}
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
node.properties = node.properties || {};
|
|
37
|
+
if (typeof node.properties.id !== 'string') {
|
|
38
|
+
let slug = slugger.slug(text);
|
|
39
|
+
if (slug.endsWith('-')) {
|
|
40
|
+
slug = slug.slice(0, -1);
|
|
41
|
+
}
|
|
42
|
+
node.properties.id = slug;
|
|
43
|
+
}
|
|
44
|
+
headings.push({ depth, slug: node.properties.id, text });
|
|
45
|
+
});
|
|
46
|
+
tree.children.unshift(
|
|
47
|
+
jsToTreeNode(`export function getHeadings() { return ${JSON.stringify(headings)} }`)
|
|
48
|
+
);
|
|
49
|
+
};
|
|
50
|
+
}
|
package/src/remark-prism.ts
CHANGED
|
@@ -1,48 +1,6 @@
|
|
|
1
|
-
|
|
2
|
-
import { addAstro } from '@astrojs/prism/internal';
|
|
3
|
-
import Prism from 'prismjs';
|
|
4
|
-
import loadLanguages from 'prismjs/components/index.js';
|
|
1
|
+
import { runHighlighterWithAstro } from '@astrojs/prism/dist/highlighter';
|
|
5
2
|
import { visit } from 'unist-util-visit';
|
|
6
3
|
|
|
7
|
-
const languageMap = new Map([['ts', 'typescript']]);
|
|
8
|
-
|
|
9
|
-
function runHighlighter(lang: string, code: string) {
|
|
10
|
-
let classLanguage = `language-${lang}`;
|
|
11
|
-
|
|
12
|
-
if (lang == null) {
|
|
13
|
-
lang = 'plaintext';
|
|
14
|
-
}
|
|
15
|
-
|
|
16
|
-
const ensureLoaded = (language: string) => {
|
|
17
|
-
if (language && !Prism.languages[language]) {
|
|
18
|
-
loadLanguages([language]);
|
|
19
|
-
}
|
|
20
|
-
};
|
|
21
|
-
|
|
22
|
-
if (languageMap.has(lang)) {
|
|
23
|
-
ensureLoaded(languageMap.get(lang)!);
|
|
24
|
-
} else if (lang === 'astro') {
|
|
25
|
-
ensureLoaded('typescript');
|
|
26
|
-
addAstro(Prism);
|
|
27
|
-
} else {
|
|
28
|
-
ensureLoaded('markup-templating'); // Prism expects this to exist for a number of other langs
|
|
29
|
-
ensureLoaded(lang);
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
if (lang && !Prism.languages[lang]) {
|
|
33
|
-
// eslint-disable-next-line no-console
|
|
34
|
-
console.warn(`Unable to load the language: ${lang}`);
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
const grammar = Prism.languages[lang];
|
|
38
|
-
let html = code;
|
|
39
|
-
if (grammar) {
|
|
40
|
-
html = Prism.highlight(code, grammar, lang);
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
return { classLanguage, html };
|
|
44
|
-
}
|
|
45
|
-
|
|
46
4
|
/** */
|
|
47
5
|
export default function remarkPrism() {
|
|
48
6
|
return (tree: any) =>
|
|
@@ -50,7 +8,7 @@ export default function remarkPrism() {
|
|
|
50
8
|
let { lang, value } = node;
|
|
51
9
|
node.type = 'html';
|
|
52
10
|
|
|
53
|
-
let { html, classLanguage } =
|
|
11
|
+
let { html, classLanguage } = runHighlighterWithAstro(lang, value);
|
|
54
12
|
let classes = [classLanguage];
|
|
55
13
|
node.value = `<pre class="${classes.join(
|
|
56
14
|
' '
|
package/src/utils.ts
CHANGED
|
@@ -1,4 +1,8 @@
|
|
|
1
|
+
import type { Options as AcornOpts } from 'acorn';
|
|
2
|
+
import { parse } from 'acorn';
|
|
1
3
|
import type { AstroConfig, SSRError } from 'astro';
|
|
4
|
+
import type { MdxjsEsm } from 'mdast-util-mdx';
|
|
5
|
+
|
|
2
6
|
import matter from 'gray-matter';
|
|
3
7
|
|
|
4
8
|
function appendForwardSlash(path: string) {
|
|
@@ -58,3 +62,24 @@ export function getFrontmatter(code: string, id: string) {
|
|
|
58
62
|
}
|
|
59
63
|
}
|
|
60
64
|
}
|
|
65
|
+
|
|
66
|
+
export function jsToTreeNode(
|
|
67
|
+
jsString: string,
|
|
68
|
+
acornOpts: AcornOpts = {
|
|
69
|
+
ecmaVersion: 'latest',
|
|
70
|
+
sourceType: 'module',
|
|
71
|
+
}
|
|
72
|
+
): MdxjsEsm {
|
|
73
|
+
return {
|
|
74
|
+
type: 'mdxjsEsm',
|
|
75
|
+
value: '',
|
|
76
|
+
data: {
|
|
77
|
+
estree: {
|
|
78
|
+
body: [],
|
|
79
|
+
...parse(jsString, acornOpts),
|
|
80
|
+
type: 'Program',
|
|
81
|
+
sourceType: 'module',
|
|
82
|
+
},
|
|
83
|
+
},
|
|
84
|
+
};
|
|
85
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
export async function get() {
|
|
2
|
+
const mdxPages = await import.meta.glob('./*.mdx', { eager: true });
|
|
3
|
+
|
|
4
|
+
return {
|
|
5
|
+
body: JSON.stringify({
|
|
6
|
+
headingsByPage: Object.fromEntries(
|
|
7
|
+
Object.entries(mdxPages ?? {}).map(([k, v]) => [k, v?.getHeadings()])
|
|
8
|
+
),
|
|
9
|
+
}),
|
|
10
|
+
}
|
|
11
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
#!/bin/sh
|
|
2
|
+
basedir=$(dirname "$(echo "$0" | sed -e 's,\\,/,g')")
|
|
3
|
+
|
|
4
|
+
case `uname` in
|
|
5
|
+
*CYGWIN*) basedir=`cygpath -w "$basedir"`;;
|
|
6
|
+
esac
|
|
7
|
+
|
|
8
|
+
if [ -z "$NODE_PATH" ]; then
|
|
9
|
+
export NODE_PATH="/home/runner/work/astro/astro/node_modules/.pnpm/node_modules"
|
|
10
|
+
else
|
|
11
|
+
export NODE_PATH="$NODE_PATH:/home/runner/work/astro/astro/node_modules/.pnpm/node_modules"
|
|
12
|
+
fi
|
|
13
|
+
if [ -x "$basedir/node" ]; then
|
|
14
|
+
exec "$basedir/node" "$basedir/../../../../../../../astro/astro.js" "$@"
|
|
15
|
+
else
|
|
16
|
+
exec node "$basedir/../../../../../../../astro/astro.js" "$@"
|
|
17
|
+
fi
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
# Space ipsum
|
|
2
|
+
|
|
3
|
+
For those who have seen the Earth from space, and for the hundreds and perhaps thousands more who will, the experience most certainly changes your perspective. The things that we share in our world are far more valuable than those which divide us.
|
|
4
|
+
|
|
5
|
+
It suddenly struck me that that tiny pea, pretty and blue, was the Earth. I put up my thumb and shut one eye, and my thumb blotted out the planet Earth. I didn’t feel like a giant. I felt very, very small.
|
|
6
|
+
|
|
7
|
+
Science has not yet mastered prophecy. We predict too much for the next year and yet far too little for the next 10.
|
|
8
|
+
|
|
9
|
+
## Section 2
|
|
10
|
+
|
|
11
|
+
We choose to go to the moon in this decade and do the other things, not because they are easy, but because they are hard, because that goal will serve to organize and measure the best of our energies and skills, because that challenge is one that we are willing to accept, one we are unwilling to postpone, and one which we intend to win.
|
|
12
|
+
|
|
13
|
+
There can be no thought of finishing for ‘aiming for the stars.’ Both figuratively and literally, it is a task to occupy the generations. And no matter how much progress one makes, there is always the thrill of just beginning.
|
|
14
|
+
|
|
15
|
+
As I stand out here in the wonders of the unknown at Hadley, I sort of realize there’s a fundamental truth to our nature, Man must explore . . . and this is exploration at its greatest.
|
|
16
|
+
|
|
17
|
+
## Section 3
|
|
18
|
+
|
|
19
|
+
Never in all their history have men been able truly to conceive of the world as one: a single sphere, a globe, having the qualities of a globe, a round earth in which all the directions eventually meet, in which there is no center because every point, or none, is center — an equal earth which all men occupy as equals. The airman’s earth, if free men make it, will be truly round: a globe in practice, not in theory.
|
|
20
|
+
|
|
21
|
+
To be the first to enter the cosmos, to engage, single-handed, in an unprecedented duel with nature—could one dream of anything more?
|
|
22
|
+
|
|
23
|
+
There can be no thought of finishing for ‘aiming for the stars.’ Both figuratively and literally, it is a task to occupy the generations. And no matter how much progress one makes, there is always the thrill of just beginning.
|
|
24
|
+
|
|
25
|
+
We are all connected; To each other, biologically. To the earth, chemically. To the rest of the universe atomically.
|
|
@@ -0,0 +1,60 @@
|
|
|
1
|
+
import mdx from '@astrojs/mdx';
|
|
2
|
+
|
|
3
|
+
import { expect } from 'chai';
|
|
4
|
+
import { parseHTML } from 'linkedom';
|
|
5
|
+
import { loadFixture } from '../../../astro/test/test-utils.js';
|
|
6
|
+
|
|
7
|
+
describe('MDX getHeadings', () => {
|
|
8
|
+
let fixture;
|
|
9
|
+
|
|
10
|
+
before(async () => {
|
|
11
|
+
fixture = await loadFixture({
|
|
12
|
+
root: new URL('./fixtures/mdx-get-headings/', import.meta.url),
|
|
13
|
+
integrations: [mdx()],
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
await fixture.build();
|
|
17
|
+
});
|
|
18
|
+
|
|
19
|
+
it('adds anchor IDs to headings', async () => {
|
|
20
|
+
const html = await fixture.readFile('/test/index.html');
|
|
21
|
+
const { document } = parseHTML(html);
|
|
22
|
+
|
|
23
|
+
const h2Ids = document.querySelectorAll('h2').map((el) => el?.id);
|
|
24
|
+
const h3Ids = document.querySelectorAll('h3').map((el) => el?.id);
|
|
25
|
+
expect(document.querySelector('h1').id).to.equal('heading-test');
|
|
26
|
+
expect(h2Ids).to.contain('section-1');
|
|
27
|
+
expect(h2Ids).to.contain('section-2');
|
|
28
|
+
expect(h3Ids).to.contain('subsection-1');
|
|
29
|
+
expect(h3Ids).to.contain('subsection-2');
|
|
30
|
+
});
|
|
31
|
+
|
|
32
|
+
it('generates correct getHeadings() export', async () => {
|
|
33
|
+
const { headingsByPage } = JSON.parse(await fixture.readFile('/pages.json'));
|
|
34
|
+
// TODO: make this a snapshot test :)
|
|
35
|
+
expect(JSON.stringify(headingsByPage['./test.mdx'])).to.equal(
|
|
36
|
+
JSON.stringify([
|
|
37
|
+
{ depth: 1, slug: 'heading-test', text: 'Heading test' },
|
|
38
|
+
{ depth: 2, slug: 'section-1', text: 'Section 1' },
|
|
39
|
+
{ depth: 3, slug: 'subsection-1', text: 'Subsection 1' },
|
|
40
|
+
{ depth: 3, slug: 'subsection-2', text: 'Subsection 2' },
|
|
41
|
+
{ depth: 2, slug: 'section-2', text: 'Section 2' },
|
|
42
|
+
])
|
|
43
|
+
);
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
it('generates correct getHeadings() export for JSX expressions', async () => {
|
|
47
|
+
const { headingsByPage } = JSON.parse(await fixture.readFile('/pages.json'));
|
|
48
|
+
expect(JSON.stringify(headingsByPage['./test-with-jsx-expressions.mdx'])).to.equal(
|
|
49
|
+
JSON.stringify([
|
|
50
|
+
{
|
|
51
|
+
depth: 1,
|
|
52
|
+
slug: 'heading-test-with-jsx-expressions',
|
|
53
|
+
text: 'Heading test with JSX expressions',
|
|
54
|
+
},
|
|
55
|
+
{ depth: 2, slug: 'h2title', text: 'h2Title' },
|
|
56
|
+
{ depth: 3, slug: 'h3title', text: 'h3Title' },
|
|
57
|
+
])
|
|
58
|
+
);
|
|
59
|
+
});
|
|
60
|
+
});
|
package/test/mdx-page.test.js
CHANGED
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import mdx from '@astrojs/mdx';
|
|
2
|
+
import { jsToTreeNode } from '../dist/utils.js';
|
|
3
|
+
|
|
4
|
+
import { expect } from 'chai';
|
|
5
|
+
import { parseHTML } from 'linkedom';
|
|
6
|
+
import getReadingTime from 'reading-time';
|
|
7
|
+
import { toString } from 'mdast-util-to-string';
|
|
8
|
+
|
|
9
|
+
import { loadFixture } from '../../../astro/test/test-utils.js';
|
|
10
|
+
|
|
11
|
+
export function rehypeReadingTime() {
|
|
12
|
+
return function (tree) {
|
|
13
|
+
const readingTime = getReadingTime(toString(tree));
|
|
14
|
+
tree.children.unshift(
|
|
15
|
+
jsToTreeNode(`export const readingTime = ${JSON.stringify(readingTime)}`)
|
|
16
|
+
);
|
|
17
|
+
};
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
const FIXTURE_ROOT = new URL('./fixtures/mdx-rehype-plugins/', import.meta.url);
|
|
21
|
+
|
|
22
|
+
describe('MDX rehype plugins', () => {
|
|
23
|
+
describe('without "extends"', () => {
|
|
24
|
+
let fixture;
|
|
25
|
+
before(async () => {
|
|
26
|
+
fixture = await loadFixture({
|
|
27
|
+
root: FIXTURE_ROOT,
|
|
28
|
+
integrations: [
|
|
29
|
+
mdx({
|
|
30
|
+
rehypePlugins: [rehypeReadingTime],
|
|
31
|
+
}),
|
|
32
|
+
],
|
|
33
|
+
});
|
|
34
|
+
await fixture.build();
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
it('removes default getHeadings', async () => {
|
|
38
|
+
const html = await fixture.readFile('/space-ipsum/index.html');
|
|
39
|
+
const { document } = parseHTML(html);
|
|
40
|
+
|
|
41
|
+
const headings = [...document.querySelectorAll('h1, h2')];
|
|
42
|
+
expect(headings.length).to.be.greaterThan(0);
|
|
43
|
+
for (const heading of headings) {
|
|
44
|
+
expect(heading.id).to.be.empty;
|
|
45
|
+
}
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
it('supports custom rehype plugins - reading time', async () => {
|
|
49
|
+
const readingTime = JSON.parse(await fixture.readFile('/reading-time.json'));
|
|
50
|
+
|
|
51
|
+
expect(readingTime).to.not.be.null;
|
|
52
|
+
expect(readingTime.text).to.match(/^\d+ min read/);
|
|
53
|
+
});
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
describe('with "extends"', () => {
|
|
57
|
+
let fixture;
|
|
58
|
+
before(async () => {
|
|
59
|
+
fixture = await loadFixture({
|
|
60
|
+
root: FIXTURE_ROOT,
|
|
61
|
+
integrations: [
|
|
62
|
+
mdx({
|
|
63
|
+
rehypePlugins: { extends: [rehypeReadingTime] },
|
|
64
|
+
}),
|
|
65
|
+
],
|
|
66
|
+
});
|
|
67
|
+
await fixture.build();
|
|
68
|
+
});
|
|
69
|
+
|
|
70
|
+
it('preserves default getHeadings', async () => {
|
|
71
|
+
const html = await fixture.readFile('/space-ipsum/index.html');
|
|
72
|
+
const { document } = parseHTML(html);
|
|
73
|
+
|
|
74
|
+
const headings = [...document.querySelectorAll('h1, h2')];
|
|
75
|
+
expect(headings.length).to.be.greaterThan(0);
|
|
76
|
+
for (const heading of headings) {
|
|
77
|
+
expect(heading.id).to.not.be.empty;
|
|
78
|
+
}
|
|
79
|
+
});
|
|
80
|
+
});
|
|
81
|
+
});
|