@@ -3,7 +3,6 @@ import { FieldMountable } from 'packages/core/src/fields/FieldMountable';
33import type { IPlugin } from 'packages/core/src/IPlugin' ;
44import { MDLinkParser } from 'packages/core/src/parsers/MarkdownLinkParser' ;
55import { ErrorCollection } from 'packages/core/src/utils/errors/ErrorCollection' ;
6- import { ErrorLevel , MetaBindEmbedError } from 'packages/core/src/utils/errors/MetaBindErrors' ;
76import { showUnloadedMessage } from 'packages/core/src/utils/Utils' ;
87
98export class EmbedMountable extends FieldMountable {
@@ -18,59 +17,57 @@ export class EmbedMountable extends FieldMountable {
1817 this . content = content ;
1918 }
2019
21- async parseContent ( ) : Promise < string > {
20+ async parseContent ( ) : Promise < { content ?: string ; error ?: string } > {
2221 const lines = this . content
2322 . split ( '\n' )
2423 . map ( line => line . trim ( ) )
2524 . filter ( line => line . length > 0 ) ;
2625
2726 if ( lines . length === 0 ) {
28- return '' ;
27+ return { content : '' } ;
2928 }
3029 if ( lines . length > 1 ) {
31- throw new MetaBindEmbedError ( {
32- errorLevel : ErrorLevel . ERROR ,
33- effect : 'can not create embed' ,
34- cause : 'embed may only contain one link' ,
35- } ) ;
30+ return { error : 'Embed may only contain one link' } ;
3631 }
3732
3833 const firstLine = lines [ 0 ] ;
3934 const link = MDLinkParser . parseLink ( firstLine ) ;
4035 if ( ! link . internal ) {
41- throw new MetaBindEmbedError ( {
42- errorLevel : ErrorLevel . ERROR ,
43- effect : 'can not create embed' ,
44- cause : 'embed link is not an internal link' ,
45- } ) ;
36+ return { error : `${ firstLine } is not an internal link` } ;
4637 }
4738 const filePath = this . plugin . internal . getFilePathByName ( link . target , this . getFilePath ( ) ) ;
4839 if ( filePath === undefined ) {
49- throw new MetaBindEmbedError ( {
50- errorLevel : ErrorLevel . ERROR ,
51- effect : 'can not create embed' ,
52- cause : 'link target not found' ,
53- } ) ;
40+ return { error : `"${ link . target } " is not created yet` } ;
5441 }
55- return await this . plugin . internal . readFilePath ( filePath ) ;
42+ return { content : await this . plugin . internal . readFilePath ( filePath ) } ;
5643 }
5744
58- checkMaxDepth ( ) : void {
59- if ( this . depth > EMBED_MAX_DEPTH ) {
60- throw new MetaBindEmbedError ( {
61- errorLevel : ErrorLevel . ERROR ,
62- effect : 'can not create embed' ,
63- cause : 'embed depth exceeds maximum' ,
64- } ) ;
65- }
45+ exceedsMaxDepth ( ) : boolean {
46+ return this . depth > EMBED_MAX_DEPTH ;
47+ }
48+
49+ createEmbedMessage ( target : HTMLElement , message : string ) : void {
50+ target . createSpan ( { text : message , cls : 'mb-embed-message' } ) ;
6651 }
6752
6853 async renderContent ( target : HTMLElement ) : Promise < void > {
6954 try {
70- this . checkMaxDepth ( ) ;
55+ if ( this . exceedsMaxDepth ( ) ) {
56+ this . createEmbedMessage ( target , 'Max embed depth exceeded' ) ;
57+ return ;
58+ }
7159
7260 const content = await this . parseContent ( ) ;
73- const renderContent = content . replace (
61+ if ( content . error ) {
62+ this . createEmbedMessage ( target , content . error ) ;
63+ return ;
64+ }
65+ if ( content . content === undefined ) {
66+ this . createEmbedMessage ( target , 'Embed content not found' ) ;
67+ return ;
68+ }
69+
70+ const renderContent = content . content . replace (
7471 / ( ` ` ` + | ~ ~ ~ + ) m e t a - b i n d - e m b e d .* / g,
7572 `$1meta-bind-embed-internal-${ this . depth + 1 } ` ,
7673 ) ;
@@ -94,13 +91,17 @@ export class EmbedMountable extends FieldMountable {
9491 MB_DEBUG && console . debug ( 'meta-bind | EmbedMountable >> mount' , this . content ) ;
9592 super . onMount ( targetEl ) ;
9693
94+ targetEl . addClass ( 'mb-embed' ) ;
95+
9796 void this . renderContent ( targetEl ) ;
9897 }
9998
10099 protected onUnmount ( targetEl : HTMLElement ) : void {
101100 MB_DEBUG && console . debug ( 'meta-bind | EmbedMountable >> unmount' , this . content ) ;
102101 super . onUnmount ( targetEl ) ;
103102
103+ targetEl . removeClass ( 'mb-embed' ) ;
104+
104105 this . markdownUnloadCallback ?.( ) ;
105106
106107 showUnloadedMessage ( targetEl , 'Embed' ) ;
0 commit comments