@@ -14,6 +14,30 @@ import {collectAssets} from "../runtime/stdlib/assets.js";
14
14
import { highlight } from "../runtime/stdlib/highlight.js" ;
15
15
import { MarkdownRenderer } from "../runtime/stdlib/md.js" ;
16
16
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
+
17
41
export interface ObservableOptions {
18
42
/** The global window, for the default parser and serializer implementations. */
19
43
window ?: Pick < typeof globalThis , "DOMParser" | "XMLSerializer" > ;
@@ -23,19 +47,19 @@ export interface ObservableOptions {
23
47
serializer ?: XMLSerializer ;
24
48
/** The path to the page template; defaults to the default template. */
25
49
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 ;
30
54
}
31
55
32
56
export function observable ( {
33
57
window = new JSDOM ( ) . window ,
34
58
parser = new window . DOMParser ( ) ,
35
59
serializer = new window . XMLSerializer ( ) ,
36
60
template = fileURLToPath ( import . meta. resolve ( "../templates/default.html" ) ) ,
37
- transformTemplate = undefined ,
38
- transformNotebook = undefined
61
+ transformTemplate = ( template ) => template ,
62
+ transformNotebook = ( notebook ) => notebook
39
63
} : ObservableOptions = { } ) : PluginOption {
40
64
return {
41
65
name : "observable" ,
@@ -50,15 +74,9 @@ export function observable({
50
74
transformIndexHtml : {
51
75
order : "pre" ,
52
76
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" ) ;
62
80
const statics = new Set < Cell > ( ) ;
63
81
const assets = new Set < string > ( ) ;
64
82
const md = MarkdownRenderer ( { document} ) ;
0 commit comments