Skip to content

Commit a0016ff

Browse files
committed
Provide a back link to the notebook
1 parent edad1e9 commit a0016ff

File tree

3 files changed

+26
-9
lines changed

3 files changed

+26
-9
lines changed

src/core/handlers/embed.ts

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,21 +27,20 @@ interface EmbedHandler {
2727
): Promise<{
2828
handled: boolean;
2929
markdownFragments: EitherString[];
30+
resources?: string[];
3031
}>;
3132
}
3233

3334
const kHandlers: EmbedHandler[] = [
3435
{
3536
name: "Jupyter Notebook Embed",
36-
handle(filename: string, handlerContext: LanguageCellHandlerContext) {
37+
handle(filename: string, _handlerContext: LanguageCellHandlerContext) {
3738
const markdownFragments: EitherString[] = [];
3839

3940
// Resolve the filename into a path
40-
const path = handlerContext.resolvePath(filename);
41-
42-
const notebookAddress = parseNotebookAddress(path);
41+
const notebookAddress = parseNotebookAddress(filename);
4342
if (notebookAddress) {
44-
const placeHolder = notebookMarkdownPlaceholder(path, {
43+
const placeHolder = notebookMarkdownPlaceholder(filename, {
4544
echo: false,
4645
warning: false,
4746
asis: true,
@@ -51,6 +50,9 @@ const kHandlers: EmbedHandler[] = [
5150
return Promise.resolve({
5251
handled: true,
5352
markdownFragments,
53+
resources: [
54+
notebookAddress.path,
55+
],
5456
});
5557
} else {
5658
return Promise.resolve({
@@ -87,6 +89,12 @@ const embedHandler: LanguageHandler = {
8789
const result = await handler.handle(filename, handlerContext);
8890
if (result.handled) {
8991
textFragments.push(...result.markdownFragments);
92+
if (result.resources) {
93+
result.resources.forEach((res) => {
94+
handlerContext.addResource(res);
95+
});
96+
}
97+
9098
break;
9199
}
92100
}

src/core/handlers/types.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ export interface LanguageCellHandlerContext {
118118
resources?: [string, string][];
119119
}): Promise<string[]>;
120120

121-
addResource: (name: string, contents: string) => void;
121+
addResource: (fileName: string) => void;
122122
addInclude: (content: string, where: PandocIncludeType) => void;
123123
addHtmlDependency: (
124124
dep: FormatDependency,

src/core/jupyter/jupyter-embed.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,15 @@ async function notebookMarkdown(
185185
options,
186186
);
187187

188+
const notebookMarkdown = (cells: JupyterCellOutput[]) => {
189+
const markdown = ["", `:::{notebook="${nbAddress.path}"}`];
190+
markdown.push("");
191+
markdown.push(cells.map((cell) => cell.markdown).join(""));
192+
markdown.push("");
193+
markdown.push(":::");
194+
return markdown.join("\n");
195+
};
196+
188197
if (nbAddress.ids) {
189198
// If cellIds are present, filter the notebook to only include
190199
// those cells (cellIds can eiher be an explicitly set cellId, a label in the
@@ -199,7 +208,7 @@ async function notebookMarkdown(
199208
return cell;
200209
}
201210
});
202-
return theCells.map((cell) => cell.markdown).join("");
211+
return notebookMarkdown(theCells);
203212
} else if (nbAddress.indexes) {
204213
// Filter and sort based upon cell indexes
205214
const theCells = nbAddress.indexes.map((idx) => {
@@ -210,11 +219,11 @@ async function notebookMarkdown(
210219
}
211220
return cellOutputs[idx];
212221
});
213-
return theCells.map((cell) => cell.markdown).join("");
222+
return notebookMarkdown(theCells);
214223
} else {
215224
// Return all the cell outputs as there is no addtional
216225
// specification of cells
217-
return cellOutputs.map((cell) => cell.markdown).join("");
226+
return notebookMarkdown(cellOutputs);
218227
}
219228
}
220229

0 commit comments

Comments
 (0)