Skip to content

Commit 0b461d6

Browse files
committed
default identity transforms
1 parent b3b9809 commit 0b461d6

File tree

1 file changed

+33
-15
lines changed

1 file changed

+33
-15
lines changed

src/vite/observable.ts

Lines changed: 33 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,30 @@ import {collectAssets} from "../runtime/stdlib/assets.js";
1414
import {highlight} from "../runtime/stdlib/highlight.js";
1515
import {MarkdownRenderer} from "../runtime/stdlib/md.js";
1616

17+
/**
18+
* A function which performs a per-page transformation of the template HTML.
19+
*
20+
* @param source The source of the template (typically HTML).
21+
* @param context The Vite plugin context.
22+
* @returns The transformed template source HTML.
23+
*/
24+
export type TemplateTransform = (
25+
source: string,
26+
context: IndexHtmlTransformContext
27+
) => string | Promise<string>;
28+
29+
/**
30+
* A function which transforms the parsed notebook.
31+
*
32+
* @param notebook The current (parsed) notebook.
33+
* @param context The Vite plugin context.
34+
* @returns The transformed notebook.
35+
*/
36+
export type NotebookTransform = (
37+
notebook: Notebook,
38+
context: IndexHtmlTransformContext
39+
) => Notebook | Promise<Notebook>;
40+
1741
export interface ObservableOptions {
1842
/** The global window, for the default parser and serializer implementations. */
1943
window?: Pick<typeof globalThis, "DOMParser" | "XMLSerializer">;
@@ -23,19 +47,19 @@ export interface ObservableOptions {
2347
serializer?: XMLSerializer;
2448
/** The path to the page template; defaults to the default template. */
2549
template?: string;
26-
/** A function which performs a per-page transformation of the template HTML. */
27-
transformTemplate?: (template: string, context: IndexHtmlTransformContext) => string | Promise<string>;
28-
/** A function which transforms the parsed notebook. */
29-
transformNotebook?: (notebook: Notebook, context: IndexHtmlTransformContext) => Notebook | Promise<Notebook>;
50+
/** An optional function which transforms the template HTML for the current page. */
51+
transformTemplate?: TemplateTransform;
52+
/** An optional function which transforms the notebook for the current page. */
53+
transformNotebook?: NotebookTransform;
3054
}
3155

3256
export function observable({
3357
window = new JSDOM().window,
3458
parser = new window.DOMParser(),
3559
serializer = new window.XMLSerializer(),
3660
template = fileURLToPath(import.meta.resolve("../templates/default.html")),
37-
transformTemplate = undefined,
38-
transformNotebook = undefined
61+
transformTemplate = (template) => template,
62+
transformNotebook = (notebook) => notebook
3963
}: ObservableOptions = {}): PluginOption {
4064
return {
4165
name: "observable",
@@ -50,15 +74,9 @@ export function observable({
5074
transformIndexHtml: {
5175
order: "pre",
5276
async handler(input, context) {
53-
let notebook = deserialize(input, {parser});
54-
if (transformNotebook !== undefined) {
55-
notebook = await transformNotebook(notebook, context);
56-
}
57-
let tsource = await readFile(template, "utf-8");
58-
if (transformTemplate !== undefined) {
59-
tsource = await transformTemplate(tsource, context);
60-
}
61-
const document = parser.parseFromString(tsource, "text/html");
77+
const notebook = await transformNotebook(deserialize(input, {parser}), context);
78+
const templateHtml = await transformTemplate(await readFile(template, "utf-8"), context);
79+
const document = parser.parseFromString(templateHtml, "text/html");
6280
const statics = new Set<Cell>();
6381
const assets = new Set<string>();
6482
const md = MarkdownRenderer({document});

0 commit comments

Comments
 (0)