|
| 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 | +} |
0 commit comments