Skip to content

Commit c4a90d5

Browse files
committed
add execute file function
1 parent b432ea1 commit c4a90d5

File tree

3 files changed

+36
-2
lines changed

3 files changed

+36
-2
lines changed

exampleVault/Test.md

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,28 @@ let lib = await self.require.import("lib.js");
6565
return lib.getGreeting();
6666
```
6767

68+
Run File:
69+
70+
```js-engine
71+
const execution = await engine.internal.executeFile('testFile.js', {
72+
container: container,
73+
component: component,
74+
});
75+
const renderer = engine.internal.createRenderer(container, context.file.path, component);
76+
renderer.render(execution.result);
77+
container.createEl('p', {text: 'hello, this is rendered from this file'});
78+
```
79+
80+
Without creating a renderer:
81+
82+
```js-engine
83+
const execution = await engine.internal.executeFile('testFile.js', {
84+
container: container,
85+
component: component,
86+
});
87+
return execution.result
88+
```
89+
6890
# Lib Test
6991

7092
Importing packaged libraries works. In this example [parsiNOM](https://github.com/mProjectsCode/parsiNOM) is used.

exampleVault/testFile.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
console.log('This is a test file');
2+
return engine.markdown.create('## This is Markdown Rendered from the Test File');

jsEngine/api/Internal.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { type API } from './API';
22
import { type EngineExecutionParams } from '../engine/Engine';
33
import { type JsExecution } from '../engine/JsExecution';
4-
import { type Component } from 'obsidian';
4+
import { type Component, TFile } from 'obsidian';
55
import { ResultRenderer } from '../ResultRenderer';
66

77
/**
@@ -14,11 +14,21 @@ export class InternalAPI {
1414
this.apiInstance = apiInstance;
1515
}
1616

17-
public async excute(params: EngineExecutionParams): Promise<JsExecution> {
17+
public async execute(params: EngineExecutionParams): Promise<JsExecution> {
1818
return await this.apiInstance.plugin.jsEngine.execute(params);
1919
}
2020

2121
public createRenderer(container: HTMLElement, sourcePath: string, component: Component): ResultRenderer {
2222
return new ResultRenderer(this.apiInstance.plugin, container, sourcePath, component);
2323
}
24+
25+
public async executeFile(path: string, params: Omit<EngineExecutionParams, 'code'>): Promise<JsExecution> {
26+
const file = this.apiInstance.app.vault.getAbstractFileByPath(path);
27+
if (!file || !(file instanceof TFile)) {
28+
throw new Error(`File ${path} not found.`);
29+
}
30+
const fullParams = params as EngineExecutionParams;
31+
fullParams.code = await this.apiInstance.app.vault.read(file);
32+
return await this.execute(fullParams);
33+
}
2434
}

0 commit comments

Comments
 (0)