@@ -5,7 +5,7 @@ import {fileURLToPath} from "node:url";
5
5
import type { TemplateLiteral } from "acorn" ;
6
6
import { JSDOM } from "jsdom" ;
7
7
import type { PluginOption , IndexHtmlTransformContext } from "vite" ;
8
- import type { Cell } from "../lib/notebook.js" ;
8
+ import type { Cell , Notebook } from "../lib/notebook.js" ;
9
9
import { deserialize } from "../lib/serialize.js" ;
10
10
import { Sourcemap } from "../javascript/sourcemap.js" ;
11
11
import { transpile } from "../javascript/transpile.js" ;
@@ -25,14 +25,17 @@ export interface ObservableOptions {
25
25
template ?: string ;
26
26
/** A function which performs a per-page transformation of the template HTML. */
27
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 > ;
28
30
}
29
31
30
32
export function observable ( {
31
33
window = new JSDOM ( ) . window ,
32
34
parser = new window . DOMParser ( ) ,
33
35
serializer = new window . XMLSerializer ( ) ,
34
36
template = fileURLToPath ( import . meta. resolve ( "../templates/default.html" ) ) ,
35
- transformTemplate = undefined
37
+ transformTemplate = undefined ,
38
+ transformNotebook = undefined
36
39
} : ObservableOptions = { } ) : PluginOption {
37
40
return {
38
41
name : "observable" ,
@@ -47,7 +50,10 @@ export function observable({
47
50
transformIndexHtml : {
48
51
order : "pre" ,
49
52
async handler ( input , context ) {
50
- const notebook = deserialize ( input , { parser} ) ;
53
+ let notebook = deserialize ( input , { parser} ) ;
54
+ if ( transformNotebook !== undefined ) {
55
+ notebook = await transformNotebook ( notebook , context ) ;
56
+ }
51
57
let tsource = await readFile ( template , "utf-8" ) ;
52
58
if ( transformTemplate !== undefined ) {
53
59
tsource = await transformTemplate ( tsource , context ) ;
0 commit comments