Skip to content

Commit 45009b3

Browse files
committed
match cell ids to labels as well
1 parent 009c228 commit 45009b3

File tree

1 file changed

+39
-10
lines changed

1 file changed

+39
-10
lines changed

src/core/handlers/include-notebook.ts

Lines changed: 39 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,11 @@ import { resourcePath } from "../resources.ts";
99

1010
import { languages } from "./base.ts";
1111

12-
import { jupyterFromFile, jupyterToMarkdown } from "../jupyter/jupyter.ts";
12+
import {
13+
jupyterCellWithOptions,
14+
jupyterFromFile,
15+
jupyterToMarkdown,
16+
} from "../jupyter/jupyter.ts";
1317

1418
import {
1519
kFigDpi,
@@ -33,7 +37,6 @@ import {
3337
} from "../jupyter/types.ts";
3438

3539
import { dirname, extname } from "path/mod.ts";
36-
import { Cell } from "https://deno.land/x/[email protected]/table/cell.ts";
3740

3841
export interface NotebookAddress {
3942
path: string;
@@ -49,6 +52,15 @@ const resolveCellIds = (hash?: string) => {
4952
}
5053
};
5154

55+
// tag specified in yaml
56+
// label in yaml
57+
// notebook.ipynb#cellid1
58+
// notebook.ipynb#cellid1
59+
// notebook.ipynb#cellid1,cellid2,cellid3
60+
// notebook.ipynb[0]
61+
// notebook.ipynb[0,1]
62+
// notebook.ipynb[0-2]
63+
5264
// If the path is a notebook path, then process it separately.
5365
export function parseNotebookPath(path: string) {
5466
const hasHash = path.indexOf("#") !== -1;
@@ -66,6 +78,8 @@ export function parseNotebookPath(path: string) {
6678
}
6779
}
6880

81+
const kLabel = "label";
82+
6983
export function notebookForAddress(
7084
nbInclude: NotebookAddress,
7185
filter?: (cell: JupyterCell) => JupyterCell,
@@ -75,7 +89,8 @@ export function notebookForAddress(
7589
const cells: JupyterCell[] = [];
7690

7791
// If cellIds are present, filter the notebook to only include
78-
// those cells
92+
// those cells (cellIds can eiher be an explicitly set cellId, a label in the
93+
// cell metadata, or a tag on a cell that matches an id)
7994
if (nbInclude.cellIds) {
8095
for (const cell of nb.cells) {
8196
// cellId can either by a literal cell Id, or a tag with that value
@@ -84,15 +99,29 @@ export function notebookForAddress(
8499
// It's an ID
85100
cells.push(cell);
86101
} else {
87-
// Check tags
88-
const hasTag = cell.metadata.tags
89-
? cell.metadata.tags.find((tag) =>
90-
nbInclude.cellIds?.includes(tag)
91-
) !==
92-
undefined
102+
// Check for label in options
103+
const cellWithOptions = jupyterCellWithOptions(
104+
nb.metadata.kernelspec.language.toLowerCase(),
105+
cell,
106+
);
107+
const hasLabel = cellWithOptions.options[kLabel]
108+
? nbInclude.cellIds.includes(cellWithOptions.options[kLabel])
93109
: false;
94-
if (hasTag) {
110+
111+
if (hasLabel) {
112+
// It matches a label
95113
cells.push(cell);
114+
} else {
115+
// Check tags
116+
const hasTag = cell.metadata.tags
117+
? cell.metadata.tags.find((tag) =>
118+
nbInclude.cellIds?.includes(tag)
119+
) !==
120+
undefined
121+
: false;
122+
if (hasTag) {
123+
cells.push(cell);
124+
}
96125
}
97126
}
98127
}

0 commit comments

Comments
 (0)