forked from shishkin/astro-asciidoc
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.ts
More file actions
108 lines (97 loc) · 3.23 KB
/
index.ts
File metadata and controls
108 lines (97 loc) · 3.23 KB
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
import type { ProcessorOptions } from "@asciidoctor/core";
import type { AstroIntegration } from "astro";
import type { ViteDevServer } from "vite";
import AsciidocConverter from "./asciidoctor.js";
import type { InitOptions } from "./worker.js";
type InternalHookParams = Parameters<
NonNullable<AstroIntegration["hooks"]["astro:config:setup"]>
>[0] & {
addPageExtension(ext: string): void;
};
/**
* Options for AsciiDoc conversion.
*/
export interface Options extends InitOptions {
/**
* Options passed to Asciidoctor document load and document convert.
*/
options?: ProcessorOptions;
}
export default function asciidoc(opts?: Options): AstroIntegration {
const asciidocFileExt = ".adoc";
const { options: documentOptions, highlighters } = opts ?? {};
const converter = new AsciidocConverter({
highlighters,
});
let server: ViteDevServer;
function watchIncludes(file: string, includes: string[]) {
server.watcher.on("change", async (f) => {
if (!includes.includes(f)) return;
const m = server.moduleGraph.getModuleById(file);
m && (await server.reloadModule(m));
});
server.watcher.add(includes);
}
return {
name: "asciidoc",
hooks: {
"astro:config:setup": (params) => {
const { addPageExtension, addRenderer, updateConfig, addWatchFile } =
params as InternalHookParams;
addRenderer({ name: "astro:mdx", serverEntrypoint: "@astrojs/mdx/server.js" });
addPageExtension(asciidocFileExt);
updateConfig({
vite: {
plugins: [
{
name: "vite-plugin-astro-asciidoc",
configureServer(s) {
server = s as ViteDevServer;
},
async transform(_code, id) {
if (!id.endsWith(asciidocFileExt)) return;
const doc = await converter.convert({
file: id,
options: documentOptions,
});
watchIncludes(id, doc.includes);
return {
code: `import { Fragment, jsx as h } from "astro/jsx-runtime";
${doc.layout ? `import Layout from ${JSON.stringify(doc.layout)};` : ""}
export const file = ${JSON.stringify(id)};
export const title = ${JSON.stringify(doc.frontmatter.title)};
export const frontmatter = ${JSON.stringify(doc.frontmatter)};
export const headings = ${JSON.stringify(doc.headings)};
export async function getHeadings() { return headings; }
export async function Content() {
const content = h(Fragment, { "set:html": ${JSON.stringify(doc.html)} });
${
doc.layout
? `return h(Layout, { title, headings, frontmatter, children: content });`
: `return content;`
}
}
export default Content;`,
meta: {
vite: {
lang: "ts",
},
},
map: null,
};
},
},
],
},
});
addWatchFile(new URL(import.meta.url));
},
"astro:server:done": async () => {
await converter.terminate();
},
"astro:build:done": async () => {
await converter.terminate();
},
},
};
}