Skip to content

Commit 3258068

Browse files
committed
Correct notebook rendering cleanup
Notebook view rendering was interfering with project rendering since it was inheriting the project context. Switch to a lower level render and manage supporting files myself.
1 parent 909449e commit 3258068

File tree

4 files changed

+36
-20
lines changed

4 files changed

+36
-20
lines changed

src/command/render/render.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,11 @@ export async function renderPandoc(
121121
executeResult.markdown,
122122
);
123123

124+
if (notebookResult.supporting) {
125+
executeResult.supporting = executeResult.supporting || [];
126+
executeResult.supporting.push(notebookResult.supporting);
127+
}
128+
124129
// Map notebook includes to pandoc includes
125130
const pandocIncludes: PandocIncludes = {
126131
[kIncludeAfterBody]: notebookResult.includes?.afterBody
@@ -284,11 +289,6 @@ export async function renderPandoc(
284289
supporting.push(...htmlPostProcessResult.supporting);
285290
}
286291

287-
if (notebookResult.supporting) {
288-
supporting = supporting || [];
289-
supporting.push(...notebookResult.supporting);
290-
}
291-
292292
withTiming("render-cleanup", () =>
293293
renderCleanup(
294294
context.target.input,

src/core/jupyter/jupyter-embed.ts

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -149,14 +149,17 @@ export async function replaceNotebookPlaceholders(
149149
flags: RenderFlags,
150150
markdown: string,
151151
) {
152-
// Assets
153-
const assets = jupyterAssets(
154-
input,
155-
to,
156-
);
157152
let match = kPlaceholderRegex.exec(markdown);
153+
let assets;
158154
let includes;
159155
while (match) {
156+
if (!assets) {
157+
assets = jupyterAssets(
158+
context.target.source,
159+
to,
160+
);
161+
}
162+
160163
// Parse the address and if this is a notebook
161164
// then proceed with the replacement
162165
const nbAddressStr = match[1];
@@ -215,15 +218,13 @@ export async function replaceNotebookPlaceholders(
215218
match = kPlaceholderRegex.exec(markdown);
216219
}
217220
kPlaceholderRegex.lastIndex = 0;
218-
const cleaned = cleanEmptyJupyterAssets(assets);
219-
const supporting = cleaned
220-
? []
221-
: [join(assets.base_dir, assets.supporting_dir)];
222-
221+
const supporting = assets
222+
? join(assets.base_dir, assets.supporting_dir)
223+
: undefined;
223224
return {
224225
includes,
225226
markdown,
226-
supporting: supporting,
227+
supporting,
227228
};
228229
}
229230

src/format/html/format-html-bootstrap.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,7 @@ function bootstrapHtmlPostprocessor(
246246
): Promise<HtmlPostProcessResult> => {
247247
// Resources used in this post processor
248248
const resources: string[] = [];
249+
const supporting: string[] = [];
249250

250251
// use display-7 style for title
251252
const title = doc.querySelector("header > .title");
@@ -334,7 +335,16 @@ function bootstrapHtmlPostprocessor(
334335

335336
// Look for included / embedded notebooks and include those
336337
if (format.render[kNotebookLinks] !== false) {
337-
await processNotebookEmbeds(input, doc, format, resources, services);
338+
const notebookPreviews = await processNotebookEmbeds(
339+
input,
340+
doc,
341+
format,
342+
resources,
343+
services,
344+
);
345+
if (notebookPreviews && notebookPreviews.length > 0) {
346+
supporting.push(...notebookPreviews);
347+
}
338348
}
339349

340350
// default treatment for computational tables
@@ -421,7 +431,7 @@ function bootstrapHtmlPostprocessor(
421431
}
422432

423433
// no resource refs
424-
return Promise.resolve({ resources, supporting: [] });
434+
return Promise.resolve({ resources, supporting });
425435
};
426436
}
427437

src/format/html/format-html-notebook.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,9 @@ import {
2626
HtmlPostProcessResult,
2727
RenderServices,
2828
} from "../../command/render/types.ts";
29-
import { render } from "../../command/render/render-shared.ts";
3029

3130
import { basename, dirname, join, relative } from "path/mod.ts";
31+
import { renderFiles } from "../../command/render/render-files.ts";
3232

3333
interface NotebookView {
3434
title: string;
@@ -226,6 +226,11 @@ export async function processNotebookEmbeds(
226226
if (nbViewConfig) {
227227
nbViewConfig.unused(linkedNotebooks);
228228
}
229+
230+
const inputDir = dirname(input);
231+
return nbPaths.map((nbPath) => {
232+
return join(inputDir, nbPath.href);
233+
});
229234
}
230235
}
231236

@@ -288,7 +293,7 @@ async function renderHtmlView(
288293

289294
// Render the notebook and update the path
290295
const nbPreviewFile = `${filename}.html`;
291-
await render(nbAbsPath, {
296+
await renderFiles([{ path: nbAbsPath }], {
292297
services,
293298
flags: {
294299
metadata: {

0 commit comments

Comments
 (0)