-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmdx.ts
58 lines (51 loc) · 1.78 KB
/
mdx.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
import { CompileOptions } from "@mdx-js/mdx";
import { createFormatAwareProcessors, FormatAwareProcessors } from "@mdx-js/mdx/internal-create-format-aware-processors";
import { createFilter, FilterPattern } from "@rollup/pluginutils";
import { SourceMapGenerator } from "source-map";
import { VFile } from "vfile";
import { Plugin } from "vinxi";
type ApplicableOptions = Omit<CompileOptions, "SourceMapGenerator">;
interface ExtraOptions {
exclude?: FilterPattern | null | undefined;
include?: FilterPattern | null | undefined;
}
type Options = ApplicableOptions & ExtraOptions;
/**
* Plugin to compile MDX w/ rollup.
*/
export function rollup(options?: Readonly<Options> | null | undefined): Plugin {
const { exclude, include, ...rest } = options || {};
let formatAwareProcessors: FormatAwareProcessors;
const filter = createFilter(include, exclude);
return {
name: "@mdx-js/rollup",
enforce: "pre",
config(config, env) {
formatAwareProcessors = createFormatAwareProcessors({
SourceMapGenerator,
development: env.mode === "development",
...rest,
});
},
async transform(value, path) {
if (!formatAwareProcessors) {
formatAwareProcessors = createFormatAwareProcessors({
SourceMapGenerator,
...rest,
});
}
// Strip the queryparameters that are part of the file extension
path = path.split("?")[0];
const file = new VFile({ path, value });
if (file.extname && filter(file.path) && formatAwareProcessors.extnames.includes(file.extname)) {
const compiled = await formatAwareProcessors.process(file);
let code = String(compiled.value);
const result = {
code,
map: compiled.map,
};
return result;
}
},
};
}