-
Notifications
You must be signed in to change notification settings - Fork 0
/
mod.ts
131 lines (120 loc) · 3.18 KB
/
mod.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import {
Aleph,
compile,
CompileOptions,
LoadInput,
LoadOutput,
Plugin,
ResolveResult,
util,
} from "./deps.ts";
export type ResolverOptions = {
/** Rewrite the page path
* ```ts
* import mdx from "https://deno.land/x/aleph_plugin_mdx@$VERSION/mod.ts";
* import type { Config } from "https://deno.land/x/aleph@v0.3.0-beta.19/types.d.ts";
* export default <Config> {
* plugins: [mdx({
* rewritePagePath(path) {
* return path.replaceAll("_", "-");
* },
* })],
* };
* ```
*/
rewritePagePath: (path: string) => string | undefined;
};
export type LoaderOptions =
& CompileOptions
& Partial<{
/** Define props to page components.
* ```ts
* import mdx from "https://deno.land/x/aleph_plugin_mdx@$VERSION/mod.ts";
* import type { Config } from "https://deno.land/x/aleph@v0.3.0-beta.19/types.d.ts";
* export default <Config> {
* plugins: [
* mdx({
* pageProps: { nav: [{ path: "/" }, { path: "/docs" }] },
* }),
* ],
* };
* ```
*/
pageProps: Record<string | number, unknown>;
}>;
export type Options =
& LoaderOptions
& Partial<ResolverOptions>;
const pattern = /\.(mdx)$/i;
const INDEX_PATH = "/index";
const PAGES_PATH = "/pages";
const NAME = "mdx-loader";
export function mdxResolver(
specifier: string,
options?: Partial<ResolverOptions>,
): ResolveResult {
const pagePath = util.trimPrefix(
specifier.replace(pattern, ""),
PAGES_PATH,
);
const isIndex = pagePath.endsWith(INDEX_PATH);
const path = isIndex
? (() => {
const trimmed = util.trimSuffix(pagePath, INDEX_PATH);
return trimmed ? trimmed : "/";
})()
: pagePath;
const _path = options?.rewritePagePath?.(path) ?? path;
return {
asPage: { path: _path, isIndex },
};
}
export async function mdxLoader(
{ specifier }: LoadInput,
aleph: Aleph,
options?: LoaderOptions,
): Promise<LoadOutput> {
const { content } = await aleph.fetchModule(specifier);
const { framework } = aleph.config;
const source = new TextDecoder().decode(content);
const { value } = await compile(source, options);
if (framework !== "react") {
throw new Error(
`${NAME}: don't support framework '${framework}'`,
);
}
const isPage = specifier.startsWith(PAGES_PATH);
const props = Object.entries(isPage ? options?.pageProps ?? {} : {}).map((
[key, value],
) => `MDXContent.${key} = ${JSON.stringify(value)};`);
return {
code: [
value.toString(),
...props,
].filter(Boolean).join("\n"),
};
}
/** Plugin for mdx
* ```ts
* // aleph.config.ts
* import mdx from "https://deno.land/x/aleph_plugin_mdx@$VERSION/mod.ts"
* import type { Config } from "https://deno.land/x/aleph@v0.3.0-beta.19/types.d.ts"
*
* export default <Config> {
* plugins: [mdx()],
* };
* ```
*/
export default function (options?: Options): Plugin {
return {
name: NAME,
setup(aleph) {
aleph.onResolve(
/\/pages\/(.+)\.mdx$/i,
(specifier) =>
mdxResolver(specifier, { rewritePagePath: options?.rewritePagePath }),
);
aleph.onLoad(pattern, (input) => mdxLoader(input, aleph, options));
},
};
}