Skip to content

Commit 030d918

Browse files
agoose77rowanc1
authored andcommitted
wip: bib manager
1 parent 45155d5 commit 030d918

File tree

3 files changed

+112
-1
lines changed

3 files changed

+112
-1
lines changed

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
"@myst-theme/diagrams": "^0.3.3",
6868
"@myst-theme/frontmatter": "^0.3.3",
6969
"@myst-theme/providers": "^0.3.3",
70+
"citation-js-utils": "^1.0.0",
7071
"katex": "^0.15.2",
7172
"myst-ext-card": "^1.0.0",
7273
"myst-ext-exercise": "^1.0.0",

src/bibliography.ts

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
import { Token } from '@lumino/coreutils';
2+
import { getCitations, CitationRenderer } from 'citation-js-utils';
3+
import { Contents, ContentsManager } from '@jupyterlab/services';
4+
import { ISignal, Signal } from '@lumino/signaling';
5+
6+
export interface IBibliographyManager {
7+
getBibliography(): CitationRenderer | null;
8+
9+
changed: ISignal<this, CitationRenderer | null>;
10+
}
11+
12+
export const IBibliographyManager = new Token<IBibliographyManager>(
13+
'jupyterlab-myst:IBibliographyManager'
14+
);
15+
16+
export class BibliographyManager implements IBibliographyManager {
17+
private _renderer: CitationRenderer | null;
18+
private _changed = new Signal<this, CitationRenderer | null>(this);
19+
20+
get changed(): ISignal<this, CitationRenderer | null> {
21+
return this._changed;
22+
}
23+
24+
getBibliography(): CitationRenderer | null {
25+
return this._renderer;
26+
}
27+
28+
constructor(contents: Contents.IManager, bibFile: string) {
29+
this._renderer = null;
30+
31+
contents
32+
.get(bibFile)
33+
.then(async model => {
34+
this._renderer = await getCitations(model.content);
35+
this._changed.emit(this._renderer);
36+
})
37+
.catch();
38+
39+
// Handle changes
40+
contents.fileChanged.connect(async (_, change) => {
41+
// On create
42+
if (change.type === 'new') {
43+
const path = (change.newValue as Contents.IModel).path;
44+
// Add model to record registry
45+
if (path === bibFile) {
46+
const model = await contents.get(path);
47+
this._renderer = await getCitations(model.content);
48+
this._changed.emit(this._renderer);
49+
}
50+
}
51+
// On rename
52+
else if (change.type === 'rename') {
53+
// Remove by path
54+
const oldPath = (change.oldValue as Contents.IModel).path;
55+
// Add under new path!
56+
const newPath = (change.newValue as Contents.IModel).path;
57+
// Add model to record registry
58+
if (newPath === bibFile) {
59+
const model = await contents.get(newPath);
60+
this._renderer = await getCitations(model.content);
61+
this._changed.emit(this._renderer);
62+
} else if (oldPath === bibFile) {
63+
this._renderer = null;
64+
this._changed.emit(this._renderer);
65+
}
66+
}
67+
// On delete
68+
else if (change.type === 'delete') {
69+
const path = (change.oldValue as Contents.IModel).path;
70+
// Add model to record registry
71+
if (path === bibFile) {
72+
this._renderer = null;
73+
this._changed.emit(this._renderer);
74+
}
75+
}
76+
// On save
77+
else {
78+
const path = (change.newValue as Contents.IModel).path;
79+
// Add model to record registry
80+
// Add model to record registry
81+
if (path === bibFile) {
82+
const model = await contents.get(path);
83+
this._renderer = await getCitations(model.content);
84+
this._changed.emit(this._renderer);
85+
}
86+
}
87+
});
88+
}
89+
}

src/index.ts

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ import {
1414
} from '@jupyterlab/notebook';
1515
import { Cell } from '@jupyterlab/cells';
1616
import { MySTContentFactory } from './MySTContentFactory';
17+
import { IBibliographyManager, BibliographyManager } from './bibliography';
18+
1719
import { IRenderMimeRegistry } from '@jupyterlab/rendermime';
1820

1921
import { notebookCellExecuted } from './actions';
@@ -73,4 +75,23 @@ const mimeRendererPlugin: JupyterFrontEndPlugin<void> = {
7375
}
7476
};
7577

76-
export default [plugin, executorPlugin, mimeRendererPlugin];
78+
const bibPlugin: JupyterFrontEndPlugin<IBibliographyManager> = {
79+
id: 'jupyterlab-myst:bibliography',
80+
requires: [],
81+
provides: IBibliographyManager,
82+
autoStart: true,
83+
activate: (app: JupyterFrontEnd) => {
84+
console.log('Using jupyterlab-myst:bibliography');
85+
86+
const manager = new BibliographyManager(
87+
app.serviceManager.contents,
88+
'bibliography.bib'
89+
);
90+
manager.changed.connect((manager, renderer) => {
91+
console.log(renderer, 'CHANGE');
92+
});
93+
return manager;
94+
}
95+
};
96+
97+
export default [plugin, executorPlugin, mimeRendererPlugin, bibPlugin];

0 commit comments

Comments
 (0)