@@ -5,7 +5,7 @@ import {fileURLToPath} from "node:url";
55import type { TemplateLiteral } from "acorn" ;
66import { JSDOM } from "jsdom" ;
77import type { PluginOption , IndexHtmlTransformContext } from "vite" ;
8- import type { Cell } from "../lib/notebook.js" ;
8+ import type { Cell , Notebook } from "../lib/notebook.js" ;
99import { deserialize } from "../lib/serialize.js" ;
1010import { Sourcemap } from "../javascript/sourcemap.js" ;
1111import { transpile } from "../javascript/transpile.js" ;
@@ -25,14 +25,17 @@ export interface ObservableOptions {
2525 template ?: string ;
2626 /** A function which performs a per-page transformation of the template HTML. */
2727 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 > ;
2830}
2931
3032export function observable ( {
3133 window = new JSDOM ( ) . window ,
3234 parser = new window . DOMParser ( ) ,
3335 serializer = new window . XMLSerializer ( ) ,
3436 template = fileURLToPath ( import . meta. resolve ( "../templates/default.html" ) ) ,
35- transformTemplate = undefined
37+ transformTemplate = undefined ,
38+ transformNotebook = undefined
3639} : ObservableOptions = { } ) : PluginOption {
3740 return {
3841 name : "observable" ,
@@ -47,7 +50,10 @@ export function observable({
4750 transformIndexHtml : {
4851 order : "pre" ,
4952 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+ }
5157 let tsource = await readFile ( template , "utf-8" ) ;
5258 if ( transformTemplate !== undefined ) {
5359 tsource = await transformTemplate ( tsource , context ) ;
0 commit comments