Skip to content

Commit 0278a4c

Browse files
committed
minor refactor; add more comments
1 parent 6478290 commit 0278a4c

25 files changed

+1112
-832
lines changed

JsEngine.d.ts

Lines changed: 747 additions & 573 deletions
Large diffs are not rendered by default.

bun.lockb

3.05 KB
Binary file not shown.

jsEngine/ArgumentManager.ts

Lines changed: 0 additions & 33 deletions
This file was deleted.

jsEngine/JsMDRC.ts

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
import { type MarkdownPostProcessorContext, MarkdownRenderChild, Menu, setIcon, type TAbstractFile, TFile } from 'obsidian';
22
import type JsEnginePlugin from './main';
3-
import { type ExecutionContext } from './ArgumentManager';
43

5-
import { type JsExecution } from './engine/JsExecution';
6-
import { ResultRenderer } from './ResultRenderer';
4+
import { type JsExecutionContext, type JsExecution } from './engine/JsExecution';
5+
import { ResultRenderer } from './engine/ResultRenderer';
76

87
export class JsMDRC extends MarkdownRenderChild {
98
plugin: JsEnginePlugin;
@@ -35,7 +34,7 @@ export class JsMDRC extends MarkdownRenderChild {
3534
}
3635
}
3736

38-
buildExecutionContext(): ExecutionContext {
37+
buildExecutionContext(): JsExecutionContext {
3938
console.log(this.ctx);
4039
const file = this.getExecutionFile()!;
4140
return {
@@ -45,7 +44,7 @@ export class JsMDRC extends MarkdownRenderChild {
4544
};
4645
}
4746

48-
async tryRun(context: ExecutionContext): Promise<JsExecution> {
47+
async tryRun(context: JsExecutionContext): Promise<JsExecution> {
4948
return this.plugin.jsEngine.execute({
5049
code: this.content,
5150
context: context,

jsEngine/ResultRenderer.ts

Lines changed: 0 additions & 103 deletions
This file was deleted.

jsEngine/api/API.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,7 @@ export class API {
5151
*
5252
* @param path the vault relative path of the file to import
5353
*/
54-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
55-
public async importJs(path: string): Promise<any> {
54+
public async importJs(path: string): Promise<unknown> {
5655
let fullPath = this.app.vault.adapter.getResourcePath(path);
5756
if (!fullPath.includes('?')) {
5857
const scriptFile = this.app.metadataCache.getFirstLinkpathDest(path, '');
@@ -72,6 +71,13 @@ export class API {
7271
return this.app.plugins.getPlugin(pluginId);
7372
}
7473

74+
/**
75+
* Creates a reactive component.
76+
* Reactive components are useful for creating dynamic content.
77+
*
78+
* @param fn the function to rerun. It's return value will be rendered.
79+
* @param initialArgs the initial arguments (for the first render) to pass to the function.
80+
*/
7581
public reactive(fn: JsFunc, ...initialArgs: unknown[]): ReactiveComponent {
7682
return new ReactiveComponent(fn, initialArgs);
7783
}

jsEngine/api/Internal.ts

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

77
/**
88
* The internal API provides access to some of js engines internals.
@@ -14,14 +14,32 @@ export class InternalAPI {
1414
this.apiInstance = apiInstance;
1515
}
1616

17+
/**
18+
* Executes the given code.
19+
*
20+
* @param params
21+
*/
1722
public async execute(params: EngineExecutionParams): Promise<JsExecution> {
1823
return await this.apiInstance.plugin.jsEngine.execute(params);
1924
}
2025

26+
/**
27+
* Creates a result renderer.
28+
*
29+
* @param container
30+
* @param sourcePath
31+
* @param component
32+
*/
2133
public createRenderer(container: HTMLElement, sourcePath: string, component: Component): ResultRenderer {
2234
return new ResultRenderer(this.apiInstance.plugin, container, sourcePath, component);
2335
}
2436

37+
/**
38+
* Load and execute the given file.
39+
*
40+
* @param path
41+
* @param params
42+
*/
2543
public async executeFile(path: string, params: Omit<EngineExecutionParams, 'code'>): Promise<JsExecution> {
2644
const file = this.apiInstance.app.vault.getAbstractFileByPath(path);
2745
if (!file || !(file instanceof TFile)) {

jsEngine/api/LibAPI.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,9 @@ export class LibAPI {
2525
this.apiInstance = apiInstance;
2626
}
2727

28+
/**
29+
* Get the [ParsiNOM](https://github.com/mProjectsCode/parsiNOM) library.
30+
*/
2831
public parsinom(): LibParsiNOM {
2932
return {
3033
P: P,
@@ -36,8 +39,10 @@ export class LibAPI {
3639
};
3740
}
3841

39-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
40-
public itertools(): any {
42+
/**
43+
* Get the [itertools-ts](https://github.com/Smoren/itertools-ts) library.
44+
*/
45+
public itertools(): typeof IterTools {
4146
return IterTools;
4247
}
4348
}

jsEngine/api/markdown/AbstractMarkdownElement.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,23 @@
11
import { type MarkdownElementType } from './MarkdownElementType';
22
import { MarkdownString } from './MarkdownString';
33

4+
/**
5+
* @internal
6+
*/
47
export abstract class AbstractMarkdownElement {
8+
/**
9+
* Converts the element to a string.
10+
*/
511
abstract toString(): string;
612

13+
/**
14+
* @internal
15+
*/
716
abstract getType(): MarkdownElementType;
817

18+
/**
19+
* Converts the element to a {@link MarkdownString}.
20+
*/
921
toMarkdown(): MarkdownString {
1022
return new MarkdownString(this.toString());
1123
}

jsEngine/api/markdown/AbstractMarkdownElementContainer.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ import { AbstractMarkdownElement } from './AbstractMarkdownElement';
22
import { MarkdownElementType } from './MarkdownElementType';
33
import { AbstractMarkdownLiteral } from './AbstractMarkdownLiteral';
44

5+
/**
6+
* @internal
7+
*/
58
export abstract class AbstractMarkdownElementContainer extends AbstractMarkdownElement {
69
markdownElements: AbstractMarkdownElement[];
710

@@ -11,12 +14,21 @@ export abstract class AbstractMarkdownElementContainer extends AbstractMarkdownE
1114
this.markdownElements = [];
1215
}
1316

17+
/**
18+
* @internal
19+
*/
1420
abstract allowElement(element: AbstractMarkdownElement): boolean;
1521

1622
public getType(): MarkdownElementType {
1723
return MarkdownElementType.NON_LITERAL;
1824
}
1925

26+
/**
27+
* Adds a child element to the container.
28+
*
29+
* @param element
30+
* @throws Error if the element is not allowed in the container.
31+
*/
2032
addElement(element: AbstractMarkdownElement): void {
2133
if (this.allowElement(element)) {
2234
this.markdownElements.push(element);
@@ -96,6 +108,9 @@ export abstract class AbstractMarkdownElementContainer extends AbstractMarkdownE
96108
// LITERALS
97109
// --------------------------------------------
98110

111+
/**
112+
* Represents a piece of pure markdown text.
113+
*/
99114
export class TextElement extends AbstractMarkdownLiteral {
100115
content: string;
101116
bold: boolean;
@@ -134,6 +149,9 @@ export class TextElement extends AbstractMarkdownLiteral {
134149
}
135150
}
136151

152+
/**
153+
* Represents an inline markdown code block.
154+
*/
137155
export class CodeElement extends AbstractMarkdownLiteral {
138156
content: string;
139157

@@ -152,6 +170,9 @@ export class CodeElement extends AbstractMarkdownLiteral {
152170
// NON LITERALS
153171
// --------------------------------------------
154172

173+
/**
174+
* Represents a markdown heading.
175+
*/
155176
export class HeadingElement extends AbstractMarkdownElementContainer {
156177
level: number;
157178

@@ -171,6 +192,9 @@ export class HeadingElement extends AbstractMarkdownElementContainer {
171192
}
172193
}
173194

195+
/**
196+
* Represents a markdown paragraph.
197+
*/
174198
export class ParagraphElement extends AbstractMarkdownElementContainer {
175199
constructor(content: string) {
176200
super();
@@ -187,6 +211,9 @@ export class ParagraphElement extends AbstractMarkdownElementContainer {
187211
}
188212
}
189213

214+
/**
215+
* Represents a markdown code block.
216+
*/
190217
export class CodeBlockElement extends AbstractMarkdownElementContainer {
191218
language: string;
192219

@@ -206,6 +233,9 @@ export class CodeBlockElement extends AbstractMarkdownElementContainer {
206233
}
207234
}
208235

236+
/**
237+
* Represents a markdown block quote.
238+
*/
209239
export class BlockQuoteElement extends AbstractMarkdownElementContainer {
210240
public allowElement(_: AbstractMarkdownElement): boolean {
211241
return true;
@@ -216,6 +246,9 @@ export class BlockQuoteElement extends AbstractMarkdownElementContainer {
216246
}
217247
}
218248

249+
/**
250+
* Represents a markdown callout.
251+
*/
219252
export class CalloutElement extends AbstractMarkdownElementContainer {
220253
title: string;
221254
type: string;
@@ -238,6 +271,9 @@ export class CalloutElement extends AbstractMarkdownElementContainer {
238271
}
239272
}
240273

274+
/**
275+
* Represents a markdown table.
276+
*/
241277
export class TableElement extends AbstractMarkdownElementContainer {
242278
header: string[];
243279
body: string[][];

0 commit comments

Comments
 (0)