Skip to content

Commit d6bcbe6

Browse files
committed
more api work
1 parent bc0639b commit d6bcbe6

15 files changed

+169
-42
lines changed

exampleVault/Dataview.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
2+
```dataviewjs
3+
console.log(test);
4+
asdasd asdasd
5+
```

exampleVault/Test.md

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
1-
asd
1+
---
2+
text: taasdasd
3+
number: 12234234
4+
---
5+
6+
Some text
27
```js-engine
38
let a = "test";
49
return a;
510
```
6-
asdasd
7-
asdasd
8-
asdasd
11+
Some more text
912

1013
```js-engine
11-
let markdownBuilder = engine.createMarkdownBuilder()
14+
let markdownBuilder = engine.markdown.createBuilder()
1215
1316
markdownBuilder.createHeading(2, "Test Heading")
1417
markdownBuilder.createParagraph("This is a test paragraph.")
@@ -44,8 +47,23 @@ let lib = await engine.importJs("lib.js");
4447
return lib.getGreeting();
4548
```
4649

47-
# Async Tewst
50+
# Async Test
4851

4952
```js-engine
5053
return await engine.test()
54+
```
55+
56+
# Meta Bind Test
57+
58+
```js-engine
59+
const meta_bind_api = engine.getPlugin("obsidian-meta-bind-plugin").api
60+
61+
const div1 = container.createDiv()
62+
const div2 = container.createDiv()
63+
64+
const inputField = meta_bind_api.createInputFieldFromString("INPUT[text:text]", "INLINE", context.file.path, div1);
65+
const inputField2 = meta_bind_api.createInputFieldFromString("INPUT[number:number]", "INLINE", context.file.path, div2);
66+
67+
component.addChild(inputField)
68+
component.addChild(inputField2)
5169
```

src/ArgumentManager.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { App, TFile } from 'obsidian';
1+
import {App, Component, TFile} from 'obsidian';
22
import JsEnginePlugin from './main';
33

44
export interface ExecutionContext {
@@ -31,13 +31,21 @@ export class ArgumentManager {
3131
});
3232
}
3333

34-
constructArgs(context: ExecutionContext): ExecutionArgument[] {
34+
constructArgs(context: ExecutionContext, component: Component, container: HTMLElement): ExecutionArgument[] {
3535
return [
3636
...this.defaultArgs,
3737
{
3838
key: 'context',
3939
value: context,
4040
},
41+
{
42+
key: 'component',
43+
value: component,
44+
},
45+
{
46+
key: 'container',
47+
value: container,
48+
},
4149
];
4250
}
4351
}

src/JsEngine.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,12 @@ export class JsEngine {
1414
this.argsManager = new ArgumentManager(app, plugin);
1515
}
1616

17-
async execute(code: string, context: ExecutionContext): Promise<unknown> {
17+
async execute(code: string, args: ExecutionArgument[]): Promise<unknown> {
1818
if (!code) {
1919
return;
2020
}
2121

2222
const AsyncFunction = async function (): Promise<void> {}.constructor;
23-
const args: ExecutionArgument[] = this.argsManager.constructArgs(context);
2423

2524
// @ts-ignore
2625
const func: any = new AsyncFunction(...args.map(x => x.key), code);

src/JsMDRC.ts

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
import { MarkdownRenderChild, MarkdownRenderer, TAbstractFile, TFile } from 'obsidian';
1+
import { MarkdownRenderChild, TAbstractFile, TFile } from 'obsidian';
22
import JsEnginePlugin from './main';
33
import { MarkdownPostProcessorContext } from 'obsidian/publish';
4-
import { ExecutionContext } from './ArgumentManager';
5-
import { MarkdownBuilder } from './markdownBuilder/MarkdownBuilder';
6-
import { MarkdownString } from './api/MarkdownAPI';
4+
import {ExecutionArgument, ExecutionContext} from './ArgumentManager';
5+
import { MarkdownBuilder } from './api/markdown/MarkdownBuilder';
6+
import {MarkdownString} from './api/markdown/MarkdownString';
77

88
export class JsMDRC extends MarkdownRenderChild {
99
plugin: JsEnginePlugin;
@@ -40,16 +40,24 @@ export class JsMDRC extends MarkdownRenderChild {
4040
};
4141
}
4242

43-
async tryRun(): Promise<unknown> {
43+
buildExecutionArgs(container: HTMLElement): ExecutionArgument[] {
4444
if (!this.plugin.jsEngine) {
45-
return;
45+
throw new Error("jsEngine is undefined");
46+
}
47+
return this.plugin.jsEngine?.argsManager.constructArgs(this.buildExecutionContext(), this, container);
48+
}
49+
50+
async tryRun(args: ExecutionArgument[]): Promise<unknown> {
51+
if (!this.plugin.jsEngine) {
52+
throw new Error("jsEngine is undefined");
4653
}
4754

48-
return this.plugin.jsEngine.execute(this.content, this.buildExecutionContext());
55+
return this.plugin.jsEngine.execute(this.content, args);
4956
}
5057

5158
async tryRender(container: HTMLElement): Promise<void> {
52-
let res = await this.tryRun();
59+
const args = this.buildExecutionArgs(container);
60+
let res = await this.tryRun(args);
5361

5462
if (!res) {
5563
return;
@@ -83,7 +91,6 @@ export class JsMDRC extends MarkdownRenderChild {
8391
await this.tryRender(this.containerEl);
8492
} catch (e) {
8593
this.containerEl.innerText = e instanceof Error ? e.stack?.toString() ?? '' : (e as string);
86-
console.log(e);
8794
}
8895
}
8996

src/api/API.ts

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,23 +1,18 @@
1-
import { MarkdownBuilder } from '../markdownBuilder/MarkdownBuilder';
2-
import { MarkdownString } from './MarkdownAPI';
3-
import { App } from 'obsidian';
1+
import {MarkdownAPI} from './MarkdownAPI';
2+
import {App, Plugin} from 'obsidian';
43
import JsEnginePlugin from '../main';
54

65
export class API {
7-
app: App;
8-
plugin: JsEnginePlugin;
6+
readonly app: App;
7+
readonly plugin: JsEnginePlugin;
8+
readonly markdown: MarkdownAPI;
9+
910

1011
constructor(app: App, plugin: JsEnginePlugin) {
1112
this.app = app;
1213
this.plugin = plugin;
13-
}
14-
15-
createMarkdownBuilder(): MarkdownBuilder {
16-
return new MarkdownBuilder();
17-
}
1814

19-
createMarkdown(markdown: string): MarkdownString {
20-
return new MarkdownString(markdown);
15+
this.markdown = new MarkdownAPI(app, plugin);
2116
}
2217

2318
/**
@@ -29,4 +24,8 @@ export class API {
2924
const fullPath = this.app.vault.adapter.getResourcePath(path);
3025
return import(fullPath);
3126
}
27+
28+
getPlugin(pluginId: string): Plugin {
29+
return this.app.plugins.getPlugin(pluginId);
30+
}
3231
}

src/api/MarkdownAPI.ts

Lines changed: 73 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,79 @@
1-
import { Component, MarkdownRenderer } from 'obsidian';
1+
import {App} from 'obsidian';
2+
import {MarkdownBuilder} from './markdown/MarkdownBuilder';
3+
import JsEnginePlugin from '../main';
4+
import {MarkdownString} from './markdown/MarkdownString';
5+
import {
6+
BlockQuoteElement,
7+
CalloutElement,
8+
CodeBlockElement,
9+
CodeElement,
10+
HeadingElement,
11+
ParagraphElement,
12+
TableElement,
13+
TextElement,
14+
} from './markdown/AbstractMarkdownElementContainer';
215

3-
export class MarkdownString {
4-
content: string;
516

6-
constructor(content: string) {
7-
this.content = content;
17+
18+
export class MarkdownAPI {
19+
readonly app: App;
20+
readonly plugin: JsEnginePlugin;
21+
22+
23+
constructor(app: App, plugin: JsEnginePlugin) {
24+
this.app = app;
25+
this.plugin = plugin;
826
}
927

10-
async render(element: HTMLElement, sourcePath: string, component: Component): Promise<void> {
11-
await MarkdownRenderer.renderMarkdown(this.content, element, sourcePath, component);
28+
createBuilder(): MarkdownBuilder {
29+
return new MarkdownBuilder();
30+
}
31+
32+
create(markdown: string): MarkdownString {
33+
return new MarkdownString(markdown);
34+
}
35+
36+
createText(text: string): TextElement {
37+
return new TextElement(text, false, false, false);
38+
}
39+
40+
createBoldText(text: string): TextElement {
41+
return new TextElement(text, true, false, false);
42+
}
43+
44+
createCursiveText(text: string): TextElement {
45+
return new TextElement(text, false, true, false);
46+
}
47+
48+
createUnderlinedText(text: string): TextElement {
49+
return new TextElement(text, false, false, true);
1250
}
13-
}
1451

15-
export class MarkdownAPI {}
52+
createCode(text: string): CodeElement {
53+
return new CodeElement(text);
54+
}
55+
56+
createParagraph(content: string): ParagraphElement {
57+
return new ParagraphElement(content);
58+
}
59+
60+
createHeading(level: number, content: string): HeadingElement {
61+
return new HeadingElement(level, content);
62+
}
63+
64+
createBlockQuote(): BlockQuoteElement {
65+
return new BlockQuoteElement();
66+
}
67+
68+
createCallout(title: string, type: string, args: string = ''): CalloutElement {
69+
return new CalloutElement(title, type, args);
70+
}
71+
72+
createCodeBlock(language: string, content: string): CodeBlockElement {
73+
return new CodeBlockElement(language, content);
74+
}
75+
76+
createTable(header: string[], body: string[][]): TableElement {
77+
return new TableElement(header, body);
78+
}
79+
}

src/markdownBuilder/AbstractMarkdownElement.ts renamed to src/api/markdown/AbstractMarkdownElement.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { MarkdownElementType } from './MarkdownElementType';
2-
import { MarkdownString } from '../api/MarkdownAPI';
2+
import { MarkdownString } from '../MarkdownAPI';
33

44
export abstract class AbstractMarkdownElement {
55
abstract toString(): string;

0 commit comments

Comments
 (0)