Skip to content

Commit 7f5a3b4

Browse files
committed
use eval, fix highlighting error
1 parent 8b5c243 commit 7f5a3b4

File tree

5 files changed

+24
-7
lines changed

5 files changed

+24
-7
lines changed

.github/FUNDING.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
github: ["mProjectsCode"]
1+
github: ['mProjectsCode']

jsEngine/engine/ExecutionStatsComponent.svelte

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,16 @@
44
55
export let execution: JsExecution;
66
let messages = execution.getMessages();
7-
let prism = window.Prism;
8-
$: highlightCode = prism.highlight(execution.code, prism.languages.javascript, 'javascript');
7+
8+
function highlightCode(code: string): string {
9+
const prism = window.Prism;
10+
try {
11+
return prism.highlight(code, prism.languages.javascript, 'javascript');
12+
} catch (e) {
13+
console.warn('js-engine | failed to highlight code', e);
14+
return code;
15+
}
16+
}
917
</script>
1018

1119
<div>
@@ -14,7 +22,7 @@
1422
<p>Execution ID: {execution.uuid}</p>
1523

1624
<h3>Code</h3>
17-
<pre class="language-js"><code class="language-js">{@html highlightCode}</code></pre>
25+
<pre class="language-js"><code class="language-js">{@html highlightCode(execution.code)}</code></pre>
1826

1927
<h3>Time</h3>
2028
<p>Build Time: {Math.round(execution.functionBuildTime ?? 0)} ms</p>

jsEngine/engine/ExecutionStatsModal.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ export class ExecutionStatsModal extends Modal {
3030
this.contentEl.addClass('js-engine-execution-stats-modal');
3131
}
3232

33+
if (this.execution === undefined) {
34+
this.contentEl.createEl('p', { cls: 'mod-warning', text: 'No execution data available.' });
35+
return;
36+
}
37+
3338
this.component = new ExecutionStatsComponent({
3439
target: this.contentEl,
3540
props: {

jsEngine/engine/JsExecution.ts

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ import { InstanceId, InstanceType } from '../api/InstanceId';
66
import * as Obsidian from 'obsidian';
77
import { type EngineExecutionParams } from './Engine';
88

9-
const AsyncFunction = async function (): Promise<void> {}.constructor;
10-
119
/**
1210
* An async JavaScript function.
1311
*/
@@ -128,7 +126,11 @@ export class JsExecution {
128126

129127
try {
130128
// this.func = AsyncFunction(...Object.keys(this.globals), this.code) as JsFunc;
131-
this.func = window.eval(`(async function anonymous(${Object.keys(this.globals).join(', ')}) {\n\n${this.code}\n\n})\n //# sourceURL=${encodeURIComponent(this.apiInstance.instanceId.toString())}`) as JsFunc;
129+
this.func = window.eval(
130+
`(async function anonymous(${Object.keys(this.globals).join(', ')}) {\n\n${this.code}\n\n})\n //# sourceURL=${encodeURIComponent(
131+
this.apiInstance.instanceId.toString(),
132+
)}`,
133+
) as JsFunc;
132134
} catch (e) {
133135
if (e instanceof Error) {
134136
this.functionBuildError = e;

styles.css

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,4 +113,6 @@ If your plugin does not need CSS, delete this file.
113113

114114
.modal:has(> .js-engine-execution-stats-modal) {
115115
width: unset;
116+
min-width: 80%;
117+
max-width: 100%;
116118
}

0 commit comments

Comments
 (0)