@@ -148,78 +148,82 @@ export async function replaceNotebookPlaceholders(
148148 flags : RenderFlags ,
149149 markdown : string ,
150150) {
151- // Assets
152- const assets = jupyterAssets (
153- input ,
154- to ,
155- ) ;
156-
157151 let match = kPlaceholderRegex . exec ( markdown ) ;
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 ,
189- ) ;
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+ }
190194 } else {
191- return undefined ;
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.` ,
198+ ) ;
192199 }
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 ( ) ;
201-
202- // Render the notebook markdown
203- const nbMarkdown = await notebookMarkdown (
204- nbAddress ,
205- assets ,
206- context ,
207- flags ,
208- nbOptions ,
209- nbOutputs ,
210- ) ;
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+ ) ;
211212
212- // Replace the placeholders with the rendered markdown
213- markdown = markdown . replaceAll ( match [ 0 ] , nbMarkdown ) ;
213+ // Replace the placeholders with the rendered markdown
214+ markdown = markdown . replaceAll ( match [ 0 ] , nbMarkdown ) ;
215+ }
216+ match = kPlaceholderRegex . exec ( markdown ) ;
214217 }
215- match = kPlaceholderRegex . exec ( markdown ) ;
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 ;
216226 }
217- kPlaceholderRegex . lastIndex = 0 ;
218- return {
219- includes,
220- markdown,
221- supporting : [ join ( assets . base_dir , assets . supporting_dir ) ] ,
222- } ;
223227}
224228
225229function resolveNbPath ( input : string , path : string ) {
0 commit comments