Skip to content

Commit a0762a7

Browse files
committed
fix #499
1 parent 8a8cd46 commit a0762a7

File tree

5 files changed

+49
-9
lines changed

5 files changed

+49
-9
lines changed

exampleVault/note1.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
foo: bar
3+
---
4+
`INPUT[slider]`
5+
6+
`BUTTON[foo]`

exampleVault/note2.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
bar: foo
3+
---
4+
5+
6+
```meta-bind-embed
7+
8+
![[note1]]
9+
```

exampleVault/templates/Test Template.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
---
2+
foo: barssadad
3+
---
4+
15
This comes from [[Test Template]].
26

37
Completed: `INPUT[toggle:completed]`

packages/core/src/api/InternalAPI.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ import type { ContextMenuItemDefinition, IContextMenu } from 'packages/core/src/
2828
import type { IFuzzySearch } from 'packages/core/src/utils/IFuzzySearch';
2929
import type { IJsRenderer } from 'packages/core/src/utils/IJsRenderer';
3030
import type { MBLiteral } from 'packages/core/src/utils/Literal';
31-
import { mount } from 'svelte';
31+
import { mount, unmount } from 'svelte';
3232
import type { z } from 'zod';
3333

3434
export interface ErrorIndicatorProps {
@@ -352,17 +352,22 @@ export abstract class InternalAPI<Plugin extends IPlugin> {
352352

353353
/**
354354
* Create an error indicator in the given element.
355+
* Returns a cleanup function.
355356
*
356357
* @param element
357358
* @param settings
358359
*/
359-
createErrorIndicator(element: HTMLElement, settings: ErrorIndicatorProps): void {
360-
mount(ErrorIndicatorComponent, {
360+
createErrorIndicator(element: HTMLElement, settings: ErrorIndicatorProps): () => void {
361+
const component = mount(ErrorIndicatorComponent, {
361362
target: element,
362363
props: {
363364
plugin: this.plugin,
364365
settings: settings,
365366
},
366367
});
368+
369+
return () => {
370+
void unmount(component);
371+
};
367372
}
368373
}

packages/obsidian/src/main.ts

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import type { MarkdownPostProcessorContext, WorkspaceLeaf } from 'obsidian';
2-
import { Plugin, stringifyYaml } from 'obsidian';
2+
import { loadPrism, Plugin, stringifyYaml } from 'obsidian';
33
import { RenderChildType } from 'packages/core/src/config/APIConfigs';
44
import { EMBED_MAX_DEPTH } from 'packages/core/src/config/FieldConfigs';
55
import type { IPlugin } from 'packages/core/src/IPlugin';
@@ -115,6 +115,7 @@ export default class MetaBindPlugin extends Plugin implements IPlugin {
115115
}
116116

117117
console.timeEnd('meta-bind | Main >> load-time');
118+
118119
// TODO: not sure if this is still needed, but it adds 100+ms to the load time, so I disabled it for now
119120
// we need to wait for prism to load first, otherwise prism will cause problems by highlighting things that it shouldn't
120121
// await loadPrism();
@@ -168,6 +169,11 @@ export default class MetaBindPlugin extends Plugin implements IPlugin {
168169
}
169170

170171
addPostProcessors(): void {
172+
// In every processor we await prism to load, otherwise prism may break our rendering.
173+
// Luckily `await loadPrism()` is a no-op if prism is already loaded.
174+
// We could also load prism once on startup, but that would add 100+ms to the reported plugin load time.
175+
// Prism is always loaded, so the total startup time would be the same, but the higher reported time may scare users.
176+
171177
// inline code blocks
172178
this.registerMarkdownPostProcessor((el: HTMLElement, ctx: MarkdownPostProcessorContext) => {
173179
const codeBlocks = el.querySelectorAll('code');
@@ -192,7 +198,9 @@ export default class MetaBindPlugin extends Plugin implements IPlugin {
192198
}, 1);
193199

194200
// "meta-bind" code blocks
195-
this.registerMarkdownCodeBlockProcessor('meta-bind', (source, el, ctx) => {
201+
this.registerMarkdownCodeBlockProcessor('meta-bind', async (source, el, ctx) => {
202+
await loadPrism();
203+
196204
const codeBlock = el;
197205
const content = source.trim();
198206
const filePath = ctx.sourcePath;
@@ -214,7 +222,9 @@ export default class MetaBindPlugin extends Plugin implements IPlugin {
214222
});
215223

216224
// "meta-bind-js-view" code blocks
217-
this.registerMarkdownCodeBlockProcessor('meta-bind-js-view', (source, el, ctx) => {
225+
this.registerMarkdownCodeBlockProcessor('meta-bind-js-view', async (source, el, ctx) => {
226+
await loadPrism();
227+
218228
const mountable = this.api.createJsViewFieldMountable(ctx.sourcePath, {
219229
declaration: source,
220230
});
@@ -223,7 +233,9 @@ export default class MetaBindPlugin extends Plugin implements IPlugin {
223233
});
224234

225235
// "meta-bind-embed" code blocks
226-
this.registerMarkdownCodeBlockProcessor('meta-bind-embed', (source, el, ctx) => {
236+
this.registerMarkdownCodeBlockProcessor('meta-bind-embed', async (source, el, ctx) => {
237+
await loadPrism();
238+
227239
const mountable = this.api.createEmbedMountable(ctx.sourcePath, {
228240
content: source,
229241
depth: 0,
@@ -233,7 +245,9 @@ export default class MetaBindPlugin extends Plugin implements IPlugin {
233245
});
234246

235247
for (let i = 1; i <= EMBED_MAX_DEPTH; i++) {
236-
this.registerMarkdownCodeBlockProcessor(`meta-bind-embed-internal-${i}`, (source, el, ctx) => {
248+
this.registerMarkdownCodeBlockProcessor(`meta-bind-embed-internal-${i}`, async (source, el, ctx) => {
249+
await loadPrism();
250+
237251
const mountable = this.api.createEmbedMountable(ctx.sourcePath, {
238252
content: source,
239253
depth: i,
@@ -244,7 +258,9 @@ export default class MetaBindPlugin extends Plugin implements IPlugin {
244258
}
245259

246260
// "meta-bind-button" code blocks
247-
this.registerMarkdownCodeBlockProcessor('meta-bind-button', (source, el, ctx) => {
261+
this.registerMarkdownCodeBlockProcessor('meta-bind-button', async (source, el, ctx) => {
262+
await loadPrism();
263+
248264
const mountable = this.api.createButtonMountable(ctx.sourcePath, {
249265
declaration: source,
250266
isPreview: false,

0 commit comments

Comments
 (0)