Skip to content

Commit d86012a

Browse files
committed
Inject widget dependencies if they are present
1 parent 4054e25 commit d86012a

File tree

2 files changed

+47
-3
lines changed

2 files changed

+47
-3
lines changed

src/command/render/render.ts

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ import {
5151
} from "../../core/timing.ts";
5252
import { filesDirMediabagDir } from "./render-paths.ts";
5353
import { replaceNotebookPlaceholders } from "../../core/jupyter/jupyter-embed.ts";
54+
import { kIncludeAfterBody, kIncludeInHeader } from "../../config/constants.ts";
5455

5556
export async function renderPandoc(
5657
file: ExecutedFile,
@@ -105,17 +106,33 @@ export async function renderPandoc(
105106
ensureDirSync(join(dirname(context.target.source), mediabagDir));
106107

107108
// Process any placeholder for notebooks that have been injected
108-
const markdown = await replaceNotebookPlaceholders(
109+
const notebookResult = await replaceNotebookPlaceholders(
109110
format.pandoc.to || "html",
110111
context.target.source,
111112
context,
112113
context.options.flags || {},
113114
executeResult.markdown,
114115
);
115116

117+
// Map notebook includes to pandoc includes
118+
const pandocIncludes: PandocIncludes = {
119+
[kIncludeAfterBody]: notebookResult.includes?.afterBody
120+
? [notebookResult.includes?.afterBody]
121+
: undefined,
122+
[kIncludeInHeader]: notebookResult.includes?.inHeader
123+
? [notebookResult.includes?.inHeader]
124+
: undefined,
125+
};
126+
127+
// Inject dependencies
128+
format.pandoc = mergePandocIncludes(
129+
format.pandoc,
130+
pandocIncludes,
131+
);
132+
116133
// pandoc options
117134
const pandocOptions: PandocOptions = {
118-
markdown,
135+
markdown: notebookResult.markdown,
119136
source: context.target.source,
120137
output: recipe.output,
121138
keepYaml: recipe.keepYaml,

src/core/jupyter/jupyter-embed.ts

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import {
2323
} from "../../config/constants.ts";
2424
import {
2525
isHtmlCompatible,
26+
isHtmlOutput,
2627
isIpynbOutput,
2728
isLatexOutput,
2829
isMarkdownOutput,
@@ -34,6 +35,11 @@ import { JupyterAssets, JupyterCellOutput } from "../jupyter/types.ts";
3435

3536
import { dirname, extname } from "path/mod.ts";
3637
import { languages } from "../handlers/base.ts";
38+
import {
39+
extractJupyterWidgetDependencies,
40+
includesForJupyterWidgetDependencies,
41+
} from "./widgets.ts";
42+
import { globalTempContext } from "../temp.ts";
3743

3844
export interface JupyterNotebookAddress {
3945
path: string;
@@ -133,6 +139,7 @@ export async function replaceNotebookPlaceholders(
133139
markdown: string,
134140
) {
135141
let match = kPlaceholderRegex.exec(markdown);
142+
let includes;
136143
while (match) {
137144
// Find and parse the placeholders
138145
const nbPath = match[1];
@@ -150,6 +157,23 @@ export async function replaceNotebookPlaceholders(
150157
to,
151158
);
152159

160+
const notebookIncludes = () => {
161+
const notebook = jupyterFromFile(nbAddress.path);
162+
const dependencies = isHtmlOutput(context.format.pandoc)
163+
? extractJupyterWidgetDependencies(notebook)
164+
: undefined;
165+
if (dependencies) {
166+
const tempDir = globalTempContext().createDir();
167+
return includesForJupyterWidgetDependencies(
168+
[dependencies],
169+
tempDir,
170+
);
171+
} else {
172+
return undefined;
173+
}
174+
};
175+
includes = notebookIncludes();
176+
153177
// Render the notebook markdown
154178
const nbMarkdown = await notebookMarkdown(
155179
nbAddress,
@@ -166,7 +190,10 @@ export async function replaceNotebookPlaceholders(
166190
match = kPlaceholderRegex.exec(markdown);
167191
}
168192
kPlaceholderRegex.lastIndex = 0;
169-
return markdown;
193+
return {
194+
includes,
195+
markdown,
196+
};
170197
}
171198

172199
async function notebookMarkdown(

0 commit comments

Comments
 (0)