Skip to content

Commit 8f65fb2

Browse files
committed
meta bind embed
1 parent cc71537 commit 8f65fb2

File tree

8 files changed

+139
-4
lines changed

8 files changed

+139
-4
lines changed

exampleVault/Embed Example.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
---
2+
completed: false
3+
select: a
4+
---
5+
6+
Test Hello
7+
8+
```meta-bind-embed test
9+
[[Test Template]]
10+
```
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
This comes from [[Other Template]].
2+
3+
```meta-bind
4+
INPUT[select(option(a), option(b)):select]
5+
```
Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
1-
## Task
1+
This comes from [[Test Template]].
22

3-
Completed: `INPUT[toggle:completed]`
3+
Completed: `INPUT[toggle:completed]`
4+
5+
[[Test Template]] embeds [[Other Template]].
6+
7+
```meta-bind-embed
8+
[[Other Template]]
9+
```

src/main.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ import { DEFAULT_SETTINGS, type MetaBindPluginSettings } from './settings/Settin
1111
import { type IPlugin } from './IPlugin';
1212
import { ObsidianMetadataAdapter } from './metadata/ObsidianMetadataAdapter';
1313
import { FaqView, MB_FAQ_VIEW_TYPE } from './utils/faq/FaqView';
14+
import { EMBED_MAX_DEPTH, EmbedMDRC } from './renderChildren/EmbedMDRC';
15+
import { getUUID } from './utils/Utils';
1416

1517
export default class MetaBindPlugin extends Plugin implements IPlugin {
1618
// @ts-ignore defined in `onload`
@@ -106,6 +108,20 @@ export default class MetaBindPlugin extends Plugin implements IPlugin {
106108
this.api.createJsViewFieldFromString(source, RenderChildType.BLOCK, ctx.sourcePath, el, ctx);
107109
});
108110

111+
this.registerMarkdownCodeBlockProcessor('meta-bind-embed', (source, el, ctx) => {
112+
const embed = new EmbedMDRC(el, source, this, ctx.sourcePath, getUUID(), 0);
113+
ctx.addChild(embed);
114+
console.warn(ctx);
115+
});
116+
117+
for (let i = 1; i <= EMBED_MAX_DEPTH; i++) {
118+
this.registerMarkdownCodeBlockProcessor(`meta-bind-embed-internal-${i}`, (source, el, ctx) => {
119+
const embed = new EmbedMDRC(el, source, this, ctx.sourcePath, getUUID(), i);
120+
ctx.addChild(embed);
121+
console.warn(ctx);
122+
});
123+
}
124+
109125
// LP editor extension
110126
this.registerEditorExtension(createMarkdownRenderChildWidgetEditorPlugin(this));
111127

src/renderChildren/EmbedMDRC.ts

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
import { AbstractMDRC } from './AbstractMDRC';
2+
import type MetaBindPlugin from '../main';
3+
import { RenderChildType } from './InputFieldMDRC';
4+
import { MarkdownRenderer } from 'obsidian';
5+
import { parseMdLink } from '../parsers/MarkdownLinkParser';
6+
import { ErrorLevel, MetaBindEmbedError } from '../utils/errors/MetaBindErrors';
7+
8+
export const EMBED_MAX_DEPTH = 8;
9+
10+
export class EmbedMDRC extends AbstractMDRC {
11+
content: string;
12+
depth: number;
13+
14+
constructor(
15+
containerEl: HTMLElement,
16+
content: string,
17+
plugin: MetaBindPlugin,
18+
filePath: string,
19+
uuid: string,
20+
depth: number,
21+
) {
22+
super(containerEl, RenderChildType.BLOCK, plugin, filePath, uuid);
23+
24+
this.content = content;
25+
this.depth = depth;
26+
}
27+
28+
async parseContent(): Promise<string> {
29+
const lines = this.content
30+
.split('\n')
31+
.map(line => line.trim())
32+
.filter(line => line.length > 0);
33+
if (lines.length === 0) {
34+
return '';
35+
}
36+
if (lines.length > 1) {
37+
throw new MetaBindEmbedError({
38+
errorLevel: ErrorLevel.ERROR,
39+
effect: 'can not create embed',
40+
cause: 'embed may only contain one link',
41+
});
42+
}
43+
const firstLine = lines[0];
44+
const link = parseMdLink(firstLine);
45+
if (!link.internal) {
46+
throw new MetaBindEmbedError({
47+
errorLevel: ErrorLevel.ERROR,
48+
effect: 'can not create embed',
49+
cause: 'embed link is not an internal link',
50+
});
51+
}
52+
const file = this.plugin.app.metadataCache.getFirstLinkpathDest(link.target, this.filePath);
53+
if (file === null) {
54+
throw new MetaBindEmbedError({
55+
errorLevel: ErrorLevel.ERROR,
56+
effect: 'can not create embed',
57+
cause: 'link target not found',
58+
});
59+
}
60+
61+
const fileContent = await this.plugin.app.vault.cachedRead(file);
62+
const fileContentLines = fileContent.split('\n');
63+
for (let i = 0; i < fileContentLines.length; i++) {
64+
const line = fileContentLines[i];
65+
if (line.startsWith('```meta-bind-embed')) {
66+
fileContentLines[i] = `\`\`\`meta-bind-embed-internal-${this.depth + 1}`;
67+
}
68+
}
69+
return fileContentLines.join('\n');
70+
}
71+
72+
public async onload(): Promise<void> {
73+
if (this.depth >= EMBED_MAX_DEPTH) {
74+
this.containerEl.empty();
75+
this.containerEl.addClass('mb-error');
76+
this.containerEl.innerText = 'Max embed depth reached';
77+
} else {
78+
const fileContent = await this.parseContent();
79+
80+
await MarkdownRenderer.render(this.plugin.app, fileContent, this.containerEl, this.filePath, this);
81+
}
82+
}
83+
}

src/renderChildren/ExcludedMDRC.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ export class ExcludedMDRC extends AbstractMDRC {
77
this.containerEl.empty();
88

99
this.containerEl.createEl('span', {
10-
text: 'this folder has been excluded in the meta bind plugin settings',
11-
cls: 'meta-bind-plugin-error',
10+
text: '[META_BIND] This folder has been excluded in the settings',
11+
cls: 'mb-error',
1212
});
1313
}
1414
}

src/utils/errors/MetaBindErrors.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ export enum ErrorType {
1616
VALIDATION = 'MB_VALIDATION',
1717
PARSINOM = 'MB_PARSINOM',
1818
EXAMPLE = 'MB_EXAMPLE',
19+
EMBED = 'MB_EMBED',
1920

2021
OTHER = 'OTHER',
2122
}
@@ -127,3 +128,9 @@ export class MetaBindExampleError extends MetaBindError {
127128
return ErrorType.EXAMPLE;
128129
}
129130
}
131+
132+
export class MetaBindEmbedError extends MetaBindError {
133+
public getErrorType(): ErrorType {
134+
return ErrorType.EMBED;
135+
}
136+
}

styles.css

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,14 @@ body {
1717
white-space: normal;
1818
}
1919

20+
.mb-input-inline {
21+
display: inline !important;
22+
}
23+
24+
.mb-input-block {
25+
display: block !important;
26+
}
27+
2028
/* View Wrappers */
2129
/* For higher specificity */
2230
:is(code, span).mb-view:has(> div.mb-view-wrapper) {

0 commit comments

Comments
 (0)