44 * Copyright (C) 2020-2023 Posit Software, PBC
55 */
66
7- import { basename , dirname } from "../../deno_ral/path.ts" ;
7+ import { basename , dirname , join } from "../../deno_ral/path.ts" ;
88import { formatResourcePath , pandocBinaryPath } from "../../core/resources.ts" ;
99import { execProcess } from "../../core/process.ts" ;
10+ import { parseHtml } from "../deno-dom.ts" ;
11+ import { Element , HTMLDocument } from "deno_dom/deno-dom-wasm-noinit.ts" ;
12+ import { esbuildCompile } from "../esbuild.ts" ;
13+ import { asDataUrl } from "../data-url.ts" ;
14+
15+ const bundleModules = async ( dom : HTMLDocument , workingDir : string ) => {
16+ const modules = dom . querySelectorAll ( "script[type='module']" ) ;
17+ for ( const module of modules ) {
18+ const src = ( module as Element ) . getAttribute ( "src" ) ;
19+ if ( src ) {
20+ const srcName = join ( workingDir , src ) ;
21+ const srcDir = dirname ( srcName ) ;
22+ const jsSource = await esbuildCompile (
23+ Deno . readTextFileSync ( srcName ) ,
24+ srcDir ,
25+ [ ] ,
26+ "esm" ,
27+ ) ;
28+ ( module as Element ) . setAttribute (
29+ "src" ,
30+ asDataUrl ( jsSource ! , "application/javascript" ) ,
31+ ) ;
32+ }
33+ }
34+ } ;
1035
1136export const pandocIngestSelfContainedContent = async (
1237 file : string ,
@@ -23,9 +48,12 @@ export const pandocIngestSelfContainedContent = async (
2348
2449 // The raw html contents
2550 const contents = Deno . readTextFileSync ( file ) ;
51+ const dom = await parseHtml ( contents ) ;
52+ await bundleModules ( dom , workingDir ) ;
53+
2654 const input : string [ ] = [ ] ;
2755 input . push ( "````````{=html}" ) ;
28- input . push ( contents ) ;
56+ input . push ( dom . documentElement ! . outerHTML ) ;
2957 input . push ( "````````" ) ;
3058
3159 // Run pandoc to suck in dependencies
0 commit comments