Skip to content

Commit 1fd1e65

Browse files
committed
add lib api
1 parent d5c4b88 commit 1fd1e65

19 files changed

+240
-262
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ data.json
2222
.DS_Store
2323

2424
obsidian.css
25+
index.d.ts
2526

2627
!exampleVault/.obsidian
2728

.prettierignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@ exampleVault
55
main.js
66
Publish.js
77
/docs
8+
index.d.ts

exampleVault/Dataview.md

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

exampleVault/Test.md

Lines changed: 43 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
text: asdakdbg
2+
text: this is some text
33
number: 12234234
44
---
55

@@ -49,77 +49,91 @@ return markdownBuilder
4949

5050
# Import Test
5151

52+
You can import JS files that lie inside of your vault.
53+
54+
JS Engine version:
55+
5256
```js-engine
5357
let lib = await engine.importJs("lib.js");
5458
return lib.getGreeting();
5559
```
5660

61+
Modules Plugin version:
62+
5763
```js-engine
5864
let lib = await self.require.import("lib.js");
5965
return lib.getGreeting();
6066
```
6167

68+
# Lib Test
69+
70+
Importing packaged libraries works. In this example [parsiNOM](https://github.com/mProjectsCode/parsiNOM) is used.
71+
6272
```js-engine
63-
let lib = await engine.importJs("svelte_project/dist/assets/index-9a060cab.js");
64-
console.log(lib);
65-
console.log(lib.App);
73+
let { P, P_UTILS } = engine.lib.parsinom();
74+
75+
const parser = P.sequence(
76+
P_UTILS.letters(),
77+
P_UTILS.digits()
78+
).map(
79+
([a, b]) => b + a
80+
);
81+
82+
return parser.parse("test123");
6683
```
6784

68-
# Async Test
85+
This example uses [IterTools](https://github.com/Smoren/itertools-ts).
6986

7087
```js-engine
71-
return await engine.test()
88+
let { Stream } = engine.lib.itertools();
89+
90+
const result = Stream.of([1, 1, 2, 2, 3, 4, 5])
91+
.distinct() // [1, 2, 3, 4, 5]
92+
.map((x) => x ** 2) // [1, 4, 9, 16, 25]
93+
.filter((x) => x < 10) // [1, 4, 9]
94+
.toSum(); // 14
95+
96+
return result;
7297
```
7398

7499
# Meta Bind Test
75100

101+
You can interact with other plugins APIs, in this example [Meta Bind](https://github.com/mProjectsCode/obsidian-meta-bind-plugin) is used.
102+
76103
```js-engine
77-
const meta_bind_api = engine.getPlugin("obsidian-meta-bind-plugin").api
104+
const mb = engine.getPlugin("obsidian-meta-bind-plugin").api
78105
79106
const div1 = container.createDiv()
80107
const div2 = container.createDiv()
81108
82-
const inputField = meta_bind_api.createInputFieldFromString("INPUT[text:text]", "INLINE", context.file.path, div1);
83-
const inputField2 = meta_bind_api.createInputFieldFromString("INPUT[number:number]", "INLINE", context.file.path, div2);
84-
85-
component.addChild(inputField)
86-
component.addChild(inputField2)
109+
const inputField = mb.createInputFieldFromString("INPUT[text:text]", "INLINE", context.file.path, div1, component, undefined);
110+
const inputField2 = mb.createInputFieldFromString("INPUT[number:number]", "INLINE", context.file.path, div2, component, undefined);
87111
```
88112

89-
# Reactive Idea (TODO)
90-
91-
```js
92-
// define a function that takes in some args and returns what should be rendered
93-
function render(args) {
94-
return 'Something';
95-
}
96-
97-
// create a reactive component, passing in the function and arguments for the initial render
98-
const reactive = engine.reactive(render, initialArgs);
113+
# Reactive Components
99114

100-
// subscribe to events and call refresh with new arguments, which causes the render function to be rerun with these arguments, replacing the existing content
101-
event.on('...', (args) => reactive.refresh(args));
102-
103-
// return the reactive component
104-
return reactive;
105-
```
115+
You can also create reactive components that re-render based on a specific event. In this case we subscribe to obsidians metadata cache.
106116

107117
```js-engine
108118
119+
// define a function that takes in some args and returns what should be rendered
109120
function render(args) {
110-
console.log(args)
111121
return args?.text ?? "undefined";
112122
}
113123
124+
// create a reactive component, passing in the function and arguments for the initial render
114125
const reactive = engine.reactive(render, context.metadata.frontmatter);
115126
127+
// subscribe to events and call refresh with new arguments, which causes the render function to be rerun with these arguments, replacing the existing content
116128
const unloadCb = engine.app.metadataCache.on('changed', async (file, data, cache) => {
117129
if (file.path === context.file.path) {
118130
reactive.refresh(cache.frontmatter);
119131
}
120132
});
121133
134+
// register the subscription to be unloaded when the code block is unloaded
122135
component.registerEvent(unloadCb);
123136
137+
// return the reactive component
124138
return reactive;
125139
```

exampleVault/dv.js

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

exampleVault/test.js

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

package-lock.json

Lines changed: 14 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
"test": "jest",
1111
"format": "prettier --write .",
1212
"lint": "eslint --max-warnings=0 src/**",
13-
"types": "tsc",
13+
"types": "tsc --declaration --emitDeclarationOnly --outfile index.d.ts",
1414
"docs": "node docs.js"
1515
},
1616
"keywords": [],
@@ -41,9 +41,11 @@
4141
"typescript": "^5.1.6"
4242
},
4343
"dependencies": {
44+
"@lemons_dev/parsinom": "^0.0.11",
4445
"@popperjs/core": "^2.11.8",
46+
"itertools-ts": "^1.27.0",
4547
"obsidian-svelte": "^0.1.9",
46-
"svelte-portal": "^2.2.0",
47-
"svelte": "^4.0.1"
48+
"svelte": "^4.0.1",
49+
"svelte-portal": "^2.2.0"
4850
}
4951
}

src/JsMDRC.ts

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
11
import { MarkdownPostProcessorContext, MarkdownRenderChild, Menu, setIcon, TAbstractFile, TFile } from 'obsidian';
22
import JsEnginePlugin from './main';
3-
import { ExecutionArgument, ExecutionContext } from './ArgumentManager';
4-
import { MarkdownBuilder } from './api/markdown/MarkdownBuilder';
5-
import { MarkdownString } from './api/markdown/MarkdownString';
6-
import { MessageWrapper } from './messages/MessageManager';
7-
import MessageComponent from './messages/MessageComponent.svelte';
3+
import { ExecutionContext } from './ArgumentManager';
84

95
import { JsExecution } from './jsEngine/JsExecution';
106
import { ResultRenderer } from './ResultRenderer';

src/ResultRenderer.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ export class ResultRenderer {
4343

4444
if (content instanceof HTMLElement) {
4545
this.container.append(content);
46+
return;
4647
}
4748

4849
if (content instanceof MessageWrapper) {
@@ -55,11 +56,15 @@ export class ResultRenderer {
5556
showMessageSource: false,
5657
},
5758
});
59+
return;
5860
}
5961

6062
if (content instanceof ReactiveComponent) {
6163
content.setRenderer(this);
6264
content.initialRender();
65+
return;
6366
}
67+
68+
this.container.innerText = JSON.stringify(content);
6469
}
6570
}

0 commit comments

Comments
 (0)