Skip to content

Commit

Permalink
Support markdown cell
Browse files Browse the repository at this point in the history
  • Loading branch information
trungleduc committed Nov 26, 2024
1 parent 105b7ba commit cbdd5fb
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 14 deletions.
9 changes: 6 additions & 3 deletions packages/base/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,19 @@
"watch": "tsc -w"
},
"dependencies": {
"@jupyter/ydoc": "^3.0.0",
"@jupyterlab/apputils": "^4.0.0",
"@jupyterlab/cells": "^4.0.0",
"@jupyterlab/codemirror": "^4.0.0",
"@jupyterlab/docregistry": "^4.0.0",
"@jupyterlab/nbformat": "^4.0.0",
"@jupyterlab/notebook": "^4.0.0",
"@jupyterlab/cells": "^4.0.0",
"@jupyterlab/rendermime": "^4.0.0",
"@jupyterlab/ui-components": "^4.0.0",
"@jupyterlab/nbformat": "^4.0.0",
"@lumino/coreutils": "^2.0.0",
"@lumino/disposable": "^2.0.0",
"@lumino/signaling": "^2.0.0",
"@lumino/widgets": "^2.0.0",
"@lumino/coreutils": "^2.0.0",
"typestyle": "^2.4.0"
},
"devDependencies": {
Expand Down
71 changes: 65 additions & 6 deletions packages/base/src/suggestionsPanel/cellWidget.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,53 @@
import { Cell, CodeCell, CodeCellModel } from '@jupyterlab/cells';
import {
CodeMirrorEditorFactory,
EditorLanguageRegistry
} from '@jupyterlab/codemirror';
import { ICell } from '@jupyterlab/nbformat';
import {
RenderMimeRegistry,
standardRendererFactories as initialFactories
} from '@jupyterlab/rendermime';
import { Panel } from '@lumino/widgets';
import { suggestionCellStyle } from './style';
import {
CodeMirrorEditorFactory,
EditorExtensionRegistry,
EditorLanguageRegistry,
EditorThemeRegistry,
ybinding
} from '@jupyterlab/codemirror';
import { IYText } from '@jupyter/ydoc';

export class CellWidget extends Panel {
constructor(options: CellWidget.IOptions) {
super(options);
const { cellModel } = options;
this.addClass(suggestionCellStyle);

const editorExtensions = () => {
const themes = new EditorThemeRegistry();
EditorThemeRegistry.getDefaultThemes().forEach(theme => {
themes.addTheme(theme);
});
const registry = new EditorExtensionRegistry();

EditorExtensionRegistry.getDefaultExtensions({ themes }).forEach(
extensionFactory => {
registry.addExtension(extensionFactory);
}
);
registry.addExtension({
name: 'shared-model-binding',
factory: options => {
const sharedModel = options.model.sharedModel as IYText;
return EditorExtensionRegistry.createImmutableExtension(
ybinding({
ytext: sharedModel.ysource,
undoManager: sharedModel.undoManager ?? undefined
})
);
}
});
return registry;
};

const rendermime = new RenderMimeRegistry({ initialFactories });
const languages = new EditorLanguageRegistry();
EditorLanguageRegistry.getDefaultLanguages()
Expand All @@ -24,18 +57,44 @@ export class CellWidget extends Panel {
.forEach(language => {
languages.addLanguage(language);
});

languages.addLanguage({
name: 'ipythongfm',
mime: 'text/x-ipythongfm',
load: async () => {
const m = await import('@codemirror/lang-markdown');
return m.markdown({
codeLanguages: (info: string) => languages.findBest(info) as any
});
}
});
const factoryService = new CodeMirrorEditorFactory({
extensions: editorExtensions(),
languages
});
const model = new CodeCellModel();
let mimeType = 'text/plain';
if (cellModel.cell_type === 'code') {
//TODO Detect correct kernel language
mimeType = 'text/x-ipython';
} else if (cellModel.cell_type === 'markdown') {
mimeType = 'text/x-ipythongfm';
}
model.mimeType = mimeType;
model.sharedModel.setSource(options.cellModel.source as string);
const cellWidget = new CodeCell({
contentFactory: new Cell.ContentFactory({
editorFactory: factoryService.newInlineEditor.bind(factoryService)
}),
rendermime,
model
});
model,
editorConfig: {
lineNumbers: false,
lineWrap: false,
matchBrackets: true,
tabFocusable: false
}
}).initializeState();
this.addWidget(cellWidget);
}
}
Expand Down
16 changes: 11 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -490,10 +490,16 @@ __metadata:
version: 0.0.0-use.local
resolution: "@jupyter/jupyter-suggestions-base@workspace:packages/base"
dependencies:
"@jupyter/ydoc": ^3.0.0
"@jupyterlab/apputils": ^4.0.0
"@jupyterlab/cells": ^4.0.0
"@jupyterlab/codemirror": ^4.0.0
"@jupyterlab/docregistry": ^4.0.0
"@jupyterlab/nbformat": ^4.0.0
"@jupyterlab/notebook": ^4.0.0
"@jupyterlab/rendermime": ^4.0.0
"@jupyterlab/ui-components": ^4.0.0
"@lumino/coreutils": ^2.0.0
"@lumino/disposable": ^2.0.0
"@lumino/signaling": ^2.0.0
"@lumino/widgets": ^2.0.0
Expand Down Expand Up @@ -695,7 +701,7 @@ __metadata:
languageName: node
linkType: hard

"@jupyterlab/cells@npm:^4.3.1":
"@jupyterlab/cells@npm:^4.0.0, @jupyterlab/cells@npm:^4.3.1":
version: 4.3.1
resolution: "@jupyterlab/cells@npm:4.3.1"
dependencies:
Expand Down Expand Up @@ -755,7 +761,7 @@ __metadata:
languageName: node
linkType: hard

"@jupyterlab/codemirror@npm:^4.3.1":
"@jupyterlab/codemirror@npm:^4.0.0, @jupyterlab/codemirror@npm:^4.3.1":
version: 4.3.1
resolution: "@jupyterlab/codemirror@npm:4.3.1"
dependencies:
Expand Down Expand Up @@ -932,7 +938,7 @@ __metadata:
languageName: node
linkType: hard

"@jupyterlab/nbformat@npm:^3.0.0 || ^4.0.0-alpha.21 || ^4.0.0, @jupyterlab/nbformat@npm:^4.3.1":
"@jupyterlab/nbformat@npm:^3.0.0 || ^4.0.0-alpha.21 || ^4.0.0, @jupyterlab/nbformat@npm:^4.0.0, @jupyterlab/nbformat@npm:^4.3.1":
version: 4.3.1
resolution: "@jupyterlab/nbformat@npm:4.3.1"
dependencies:
Expand Down Expand Up @@ -1024,7 +1030,7 @@ __metadata:
languageName: node
linkType: hard

"@jupyterlab/rendermime@npm:^4.3.1":
"@jupyterlab/rendermime@npm:^4.0.0, @jupyterlab/rendermime@npm:^4.3.1":
version: 4.3.1
resolution: "@jupyterlab/rendermime@npm:4.3.1"
dependencies:
Expand Down Expand Up @@ -1461,7 +1467,7 @@ __metadata:
languageName: node
linkType: hard

"@lumino/coreutils@npm:^1.11.0 || ^2.0.0, @lumino/coreutils@npm:^1.11.0 || ^2.2.0, @lumino/coreutils@npm:^2.2.0":
"@lumino/coreutils@npm:^1.11.0 || ^2.0.0, @lumino/coreutils@npm:^1.11.0 || ^2.2.0, @lumino/coreutils@npm:^2.0.0, @lumino/coreutils@npm:^2.2.0":
version: 2.2.0
resolution: "@lumino/coreutils@npm:2.2.0"
dependencies:
Expand Down

0 comments on commit cbdd5fb

Please sign in to comment.