Skip to content

Commit 87e2d0c

Browse files
committed
Add proper syntax highlighting for notebook
1 parent 0cdbebe commit 87e2d0c

4 files changed

Lines changed: 61 additions & 6 deletions

File tree

wasm/notebook/src/index.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ import './style.css';
33
// https://github.com/codemirror/codemirror
44
import CodeMirror from 'codemirror';
55
import 'codemirror/mode/python/python';
6+
import 'codemirror/mode/markdown/markdown';
7+
import 'codemirror/mode/stex/stex';
68
import 'codemirror/addon/comment/comment';
79
import 'codemirror/lib/codemirror.css';
810

@@ -55,7 +57,7 @@ const editor = CodeMirror.fromTextArea(document.getElementById('code'), {
5557
},
5658
},
5759
lineNumbers: true,
58-
mode: 'text/x-python',
60+
mode: 'text/x-notebook',
5961
indentUnit: 4,
6062
autofocus: true,
6163
lineWrapping: true,
@@ -86,6 +88,9 @@ function parseCodeFromEditor() {
8688
// so far have py for python, md for markdown and math for math ;p
8789
let content = chunk.chunkContent;
8890
switch (chunk.chunkType) {
91+
// by default assume this is python code
92+
// so users don't have to type py manually
93+
case '':
8994
case 'py':
9095
runPython(content);
9196
break;
@@ -99,9 +104,7 @@ function parseCodeFromEditor() {
99104
notebook.innerHTML += renderMath(content, false);
100105
break;
101106
default:
102-
// by default assume this is python code
103-
// so users don't have to type py manually
104-
runPython(code);
107+
// do nothing when we see an unknown chunk for now
105108
}
106109
});
107110
}

wasm/notebook/src/parser.js

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import CodeMirror from 'codemirror';
2+
13
// The parser is from Mozilla's iodide project:
24
// https://github.com/iodide-project/iodide/blob/master/src/editor/iomd-tools/iomd-parser.js
35

@@ -84,3 +86,52 @@ export function iomdParser(fullIomd) {
8486
pushChunk(iomdLines.length);
8587
return chunks;
8688
}
89+
90+
CodeMirror.defineMode('notebook', function (config, _parserConfig) {
91+
const nullMode = CodeMirror.getMode(config, 'text/plain');
92+
const python = CodeMirror.getMode(config, 'python');
93+
const markdown = CodeMirror.getMode(config, 'markdown');
94+
const latex = CodeMirror.getMode(config, 'text/x-latex');
95+
const modeMap = {
96+
py: python,
97+
md: markdown,
98+
math: latex,
99+
'math-inline': latex,
100+
};
101+
return {
102+
startState() {
103+
return {
104+
mode: python,
105+
modeState: python.startState(),
106+
chunkStart: false,
107+
};
108+
},
109+
token(stream, state) {
110+
if (stream.sol() && stream.match('%%')) {
111+
stream.eatSpace();
112+
state.chunkStart = true;
113+
return 'keyword';
114+
}
115+
if (state.chunkStart) {
116+
const m = stream.match(/[\w\-]+/);
117+
const name = m && m[0];
118+
const mode = (state.mode = modeMap[name] || nullMode);
119+
state.modeState = mode.startState ? mode.startState() : null;
120+
state.chunkStart = false;
121+
return 'keyword';
122+
}
123+
const { mode, modeState } = state;
124+
return mode.token(stream, modeState);
125+
},
126+
indent(state, textAfter, line) {
127+
const { mode, modeState } = state;
128+
if (mode.indent) return mode.indent(modeState, textAfter, line);
129+
},
130+
innerMode(state) {
131+
const { mode, modeState } = state;
132+
return { mode, state: modeState };
133+
},
134+
};
135+
});
136+
137+
CodeMirror.defineMIME('text/x-notebook', 'notebook');

wasm/notebook/src/sample.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ print("Javascript called. It wants its DOM elements back.")
66

77
## RustPython Notebook with Markdown and Math
88

9-
Look! I can write markdown in the notebook. It messes up code highligting, but at least it works.
9+
Look! I can write markdown in the notebook. It's even syntax-highlighted correctly!
1010

1111
1. i can make a list
1212
1. i can haz many lines and numbers

wasm/notebook/webpack.config.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,8 @@ module.exports = (env = {}) => {
7171
if (!env.noWasmPack) {
7272
config.plugins.push(
7373
new WasmPackPlugin({
74-
crateDirectory: path.join(__dirname, '../lib')
74+
crateDirectory: path.join(__dirname, '../lib'),
75+
forceMode: 'release'
7576
})
7677
);
7778
}

0 commit comments

Comments
 (0)