@@ -9,6 +9,7 @@ import { resourcePath } from "../resources.ts";
99import { getNamedLifetime , ObjectWithLifetime } from "../lifetimes.ts" ;
1010
1111import {
12+ cleanEmptyJupyterAssets ,
1213 jupyterAssets ,
1314 jupyterFromFile ,
1415 jupyterToMarkdown ,
@@ -44,7 +45,7 @@ import {
4445import { globalTempContext } from "../temp.ts" ;
4546import { isAbsolute } from "path/mod.ts" ;
4647import { partitionMarkdown } from "../pandoc/pandoc-partition.ts" ;
47- import { safeExistsSync } from "../path.ts" ;
48+ import { removeIfEmptyDir , safeExistsSync } from "../path.ts" ;
4849import { basename } from "path/mod.ts" ;
4950
5051export interface JupyterNotebookAddress {
@@ -148,82 +149,82 @@ export async function replaceNotebookPlaceholders(
148149 flags : RenderFlags ,
149150 markdown : string ,
150151) {
152+ // Assets
153+ const assets = jupyterAssets (
154+ input ,
155+ to ,
156+ ) ;
151157 let match = kPlaceholderRegex . exec ( markdown ) ;
152- if ( match ) {
153- // Assets
154- const assets = jupyterAssets (
155- input ,
156- to ,
157- ) ;
158-
159- let includes ;
160- while ( match ) {
161- // Parse the address and if this is a notebook
162- // then proceed with the replacement
163- const nbAddressStr = match [ 1 ] ;
164- const nbAddress = parseNotebookAddress ( nbAddressStr ) ;
165- if ( nbAddress ) {
166- // If a list of outputs are provided, resolve that range
167- const outputsStr = match [ 2 ] ;
168- const nbOutputs = outputsStr ? resolveRange ( outputsStr ) : undefined ;
169-
170- // If cell options are provided, resolve those
171- const placeholderStr = match [ 3 ] ;
172- const nbOptions = placeholderStr
173- ? placeholderToOptions ( placeholderStr )
174- : { } ;
175-
176- // Compute appropriate includes based upon the note
177- // dependendencies
178- const notebookIncludes = ( ) => {
179- const nbPath = resolveNbPath ( input , nbAddress . path ) ;
180- if ( safeExistsSync ( nbPath ) ) {
181- const notebook = jupyterFromFile ( nbPath ) ;
182- const dependencies = isHtmlOutput ( context . format . pandoc )
183- ? extractJupyterWidgetDependencies ( notebook )
184- : undefined ;
185- if ( dependencies ) {
186- const tempDir = globalTempContext ( ) . createDir ( ) ;
187- return includesForJupyterWidgetDependencies (
188- [ dependencies ] ,
189- tempDir ,
190- ) ;
191- } else {
192- return undefined ;
193- }
194- } else {
195- const notebookName = basename ( nbPath ) ;
196- throw new Error (
197- `Unable to embed content from notebook '${ notebookName } '\nThe file ${ nbPath } doesn't exist or cannot be read.` ,
158+ let includes ;
159+ while ( match ) {
160+ // Parse the address and if this is a notebook
161+ // then proceed with the replacement
162+ const nbAddressStr = match [ 1 ] ;
163+ const nbAddress = parseNotebookAddress ( nbAddressStr ) ;
164+ if ( nbAddress ) {
165+ // If a list of outputs are provided, resolve that range
166+ const outputsStr = match [ 2 ] ;
167+ const nbOutputs = outputsStr ? resolveRange ( outputsStr ) : undefined ;
168+
169+ // If cell options are provided, resolve those
170+ const placeholderStr = match [ 3 ] ;
171+ const nbOptions = placeholderStr
172+ ? placeholderToOptions ( placeholderStr )
173+ : { } ;
174+
175+ // Compute appropriate includes based upon the note
176+ // dependendencies
177+ const notebookIncludes = ( ) => {
178+ const nbPath = resolveNbPath ( input , nbAddress . path ) ;
179+ if ( safeExistsSync ( nbPath ) ) {
180+ const notebook = jupyterFromFile ( nbPath ) ;
181+ const dependencies = isHtmlOutput ( context . format . pandoc )
182+ ? extractJupyterWidgetDependencies ( notebook )
183+ : undefined ;
184+ if ( dependencies ) {
185+ const tempDir = globalTempContext ( ) . createDir ( ) ;
186+ return includesForJupyterWidgetDependencies (
187+ [ dependencies ] ,
188+ tempDir ,
198189 ) ;
190+ } else {
191+ return undefined ;
199192 }
200- } ;
201- includes = notebookIncludes ( ) ;
202-
203- // Render the notebook markdown
204- const nbMarkdown = await notebookMarkdown (
205- nbAddress ,
206- assets ,
207- context ,
208- flags ,
209- nbOptions ,
210- nbOutputs ,
211- ) ;
193+ } else {
194+ const notebookName = basename ( nbPath ) ;
195+ throw new Error (
196+ `Unable to embed content from notebook '${ notebookName } '\nThe file ${ nbPath } doesn't exist or cannot be read.` ,
197+ ) ;
198+ }
199+ } ;
200+ includes = notebookIncludes ( ) ;
212201
213- // Replace the placeholders with the rendered markdown
214- markdown = markdown . replaceAll ( match [ 0 ] , nbMarkdown ) ;
215- }
216- match = kPlaceholderRegex . exec ( markdown ) ;
202+ // Render the notebook markdown
203+ const nbMarkdown = await notebookMarkdown (
204+ nbAddress ,
205+ assets ,
206+ context ,
207+ flags ,
208+ nbOptions ,
209+ nbOutputs ,
210+ ) ;
211+
212+ // Replace the placeholders with the rendered markdown
213+ markdown = markdown . replaceAll ( match [ 0 ] , nbMarkdown ) ;
217214 }
218- kPlaceholderRegex . lastIndex = 0 ;
219- return {
220- includes,
221- markdown,
222- supporting : [ join ( assets . base_dir , assets . supporting_dir ) ] ,
223- } ;
224- } else {
225- return undefined ;
215+ match = kPlaceholderRegex . exec ( markdown ) ;
226216 }
217+ kPlaceholderRegex . lastIndex = 0 ;
218+ const cleaned = cleanEmptyJupyterAssets ( assets ) ;
219+ const supporting = cleaned
220+ ? [ ]
221+ : [ join ( assets . base_dir , assets . supporting_dir ) ] ;
222+
223+ return {
224+ includes,
225+ markdown,
226+ supporting : supporting ,
227+ } ;
227228}
228229
229230function resolveNbPath ( input : string , path : string ) {
0 commit comments