Skip to content

Commit 66c90ed

Browse files
committed
Don’t create jupyter assets unless necessary
1 parent 9463176 commit 66c90ed

File tree

2 files changed

+73
-78
lines changed

2 files changed

+73
-78
lines changed

src/command/render/render.ts

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -123,10 +123,10 @@ export async function renderPandoc(
123123

124124
// Map notebook includes to pandoc includes
125125
const pandocIncludes: PandocIncludes = {
126-
[kIncludeAfterBody]: notebookResult.includes?.afterBody
126+
[kIncludeAfterBody]: notebookResult && notebookResult.includes?.afterBody
127127
? [notebookResult.includes?.afterBody]
128128
: undefined,
129-
[kIncludeInHeader]: notebookResult.includes?.inHeader
129+
[kIncludeInHeader]: notebookResult && notebookResult.includes?.inHeader
130130
? [notebookResult.includes?.inHeader]
131131
: undefined,
132132
};
@@ -139,7 +139,7 @@ export async function renderPandoc(
139139

140140
// pandoc options
141141
const pandocOptions: PandocOptions = {
142-
markdown: notebookResult.markdown,
142+
markdown: notebookResult ? notebookResult.markdown : executeResult.markdown,
143143
source: context.target.source,
144144
output: recipe.output,
145145
keepYaml: recipe.keepYaml,
@@ -282,15 +282,6 @@ export async function renderPandoc(
282282
supporting.push(...htmlPostProcessResult.supporting);
283283
}
284284

285-
// Add notebook embed results
286-
if (
287-
notebookResult.supporting &&
288-
notebookResult.supporting.length > 0
289-
) {
290-
supporting = supporting || [];
291-
supporting.push(...notebookResult.supporting);
292-
}
293-
294285
withTiming("render-cleanup", () =>
295286
renderCleanup(
296287
context.target.input,

src/core/jupyter/jupyter-embed.ts

Lines changed: 70 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -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

225229
function resolveNbPath(input: string, path: string) {

0 commit comments

Comments
 (0)