Skip to content

Commit 473a9e0

Browse files
committed
jupyter labels/refs -- do initial global scan of document on first load
1 parent 7acc989 commit 473a9e0

File tree

1 file changed

+32
-3
lines changed

1 file changed

+32
-3
lines changed

src/packages/jupyter/redux/actions.ts

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ export abstract class JupyterActions extends Actions<JupyterStoreState> {
7474
public _complete_request?: number;
7575
public store: JupyterStore;
7676
public syncdb: SyncDB;
77-
private labels: { [label: string]: { tag: string; id: string } } = {};
77+
private labels?: { [label: string]: { tag: string; id: string } };
7878

7979
public _init(
8080
project_id: string,
@@ -381,7 +381,7 @@ export abstract class JupyterActions extends Actions<JupyterStoreState> {
381381
// Might throw a CellWriteProtectedException
382382
public set_cell_input(id: string, input: string, save = true): void {
383383
if (!this.store) return;
384-
if (this.store.getIn(["cells", id, "input"]) == input) {
384+
if (this.store.getIn(["this.st", id, "input"]) == input) {
385385
// nothing changed. Note, I tested doing the above check using
386386
// both this.syncdb and this.store, and this.store is orders of magnitude faster.
387387
return;
@@ -2729,12 +2729,41 @@ export abstract class JupyterActions extends Actions<JupyterStoreState> {
27292729
};
27302730

27312731
processRenderedMarkdown = ({ value, id }: { value: string; id: string }) => {
2732-
const labels = this.labels;
27332732
const labelRegExp = /\s*\\label\{.*?\}\s*/g;
2733+
if (this.labels == null) {
2734+
const labels = (this.labels = {});
2735+
// do initial full document scan
2736+
if (this.store == null) {
2737+
return;
2738+
}
2739+
const cells = this.store.get("cells");
2740+
if (cells == null) {
2741+
return;
2742+
}
2743+
let n = 0;
2744+
for (const id of this.store.get_cell_ids_list()) {
2745+
const cell = cells.get(id);
2746+
if (cell?.get("cell_type") == "markdown") {
2747+
const value = cell.get("input") ?? "";
2748+
value.replace(labelRegExp, (labelContent) => {
2749+
const label = extractLabel(labelContent);
2750+
n += 1;
2751+
labels[label] = { tag: `${n}`, id };
2752+
});
2753+
}
2754+
}
2755+
}
2756+
const labels = this.labels;
2757+
if (labels == null) {
2758+
throw Error("bug");
2759+
}
27342760
const noLabels = value.replace(labelRegExp, (labelContent) => {
27352761
const label = extractLabel(labelContent);
27362762
if (labels[label] == null) {
27372763
labels[label] = { tag: `${misc.len(labels) + 1}`, id };
2764+
} else {
2765+
// in case it moved to a different cell due to cut/paste
2766+
labels[label].id = id;
27382767
}
27392768
return `\\tag{${labels[label].tag}}`;
27402769
});

0 commit comments

Comments
 (0)