Skip to content

Commit 1b98212

Browse files
committed
codeblock: make execute async, add canExecute, use hljs to determine language
1 parent 30ece9b commit 1b98212

File tree

1 file changed

+14
-2
lines changed

1 file changed

+14
-2
lines changed

lib/com/codeblock.tsx

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ import { Node } from "../model/mod.ts";
55
export interface CodeExecutor {
66
// executes the source and returns an output string.
77
// exceptions in execution should be caught and returned as a string.
8-
execute(source: string, options: ExecuteOptions): string;
8+
async execute(source: string, options: ExecuteOptions): string;
9+
10+
canExecute(options: ExecuteOptions): boolean;
911
}
1012

1113
export interface ExecuteOptions {
@@ -14,11 +16,18 @@ export interface ExecuteOptions {
1416

1517
// defaultExecutor can be replaced with an external service, etc
1618
export let defaultExecutor: CodeExecutor = {
17-
execute: (source: string, options: ExecuteOptions): string => {
19+
async execute(source: string, options: ExecuteOptions): Promise<string> {
1820
if (options.language !== "javascript") {
1921
return `Unsupported language: ${options.language}`;
2022
}
2123
return JSON.stringify(window.eval(source));
24+
},
25+
26+
canExecute(options: ExecuteOptions): boolean {
27+
if (options.language === "javascript") {
28+
return true;
29+
}
30+
return false;
2231
}
2332
}
2433

@@ -27,9 +36,11 @@ export let defaultExecutor: CodeExecutor = {
2736
@component
2837
export class CodeBlock {
2938
code: string;
39+
language: string;
3040

3141
constructor() {
3242
this.code = "";
43+
this.language = "";
3344
}
3445

3546
childrenView() {
@@ -75,6 +86,7 @@ const CodeEditor = {
7586
// let's do it by this hack.
7687
editor.textContent = editor.textContent;
7788
window.hljs.highlightBlock(editor);
89+
snippet.language = window.hljs.highlightAuto(editor.textContent).language || "";
7890
});
7991
dom.jarEditor.updateCode(snippet.code);
8092
dom.jarEditor.onUpdate(code => {

0 commit comments

Comments
 (0)