@@ -3,7 +3,6 @@ import { FieldMountable } from 'packages/core/src/fields/FieldMountable';
3
3
import type { IPlugin } from 'packages/core/src/IPlugin' ;
4
4
import { MDLinkParser } from 'packages/core/src/parsers/MarkdownLinkParser' ;
5
5
import { ErrorCollection } from 'packages/core/src/utils/errors/ErrorCollection' ;
6
- import { ErrorLevel , MetaBindEmbedError } from 'packages/core/src/utils/errors/MetaBindErrors' ;
7
6
import { showUnloadedMessage } from 'packages/core/src/utils/Utils' ;
8
7
9
8
export class EmbedMountable extends FieldMountable {
@@ -18,59 +17,57 @@ export class EmbedMountable extends FieldMountable {
18
17
this . content = content ;
19
18
}
20
19
21
- async parseContent ( ) : Promise < string > {
20
+ async parseContent ( ) : Promise < { content ?: string ; error ?: string } > {
22
21
const lines = this . content
23
22
. split ( '\n' )
24
23
. map ( line => line . trim ( ) )
25
24
. filter ( line => line . length > 0 ) ;
26
25
27
26
if ( lines . length === 0 ) {
28
- return '' ;
27
+ return { content : '' } ;
29
28
}
30
29
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' } ;
36
31
}
37
32
38
33
const firstLine = lines [ 0 ] ;
39
34
const link = MDLinkParser . parseLink ( firstLine ) ;
40
35
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` } ;
46
37
}
47
38
const filePath = this . plugin . internal . getFilePathByName ( link . target , this . getFilePath ( ) ) ;
48
39
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` } ;
54
41
}
55
- return await this . plugin . internal . readFilePath ( filePath ) ;
42
+ return { content : await this . plugin . internal . readFilePath ( filePath ) } ;
56
43
}
57
44
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' } ) ;
66
51
}
67
52
68
53
async renderContent ( target : HTMLElement ) : Promise < void > {
69
54
try {
70
- this . checkMaxDepth ( ) ;
55
+ if ( this . exceedsMaxDepth ( ) ) {
56
+ this . createEmbedMessage ( target , 'Max embed depth exceeded' ) ;
57
+ return ;
58
+ }
71
59
72
60
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 (
74
71
/ ( ` ` ` + | ~ ~ ~ + ) m e t a - b i n d - e m b e d .* / g,
75
72
`$1meta-bind-embed-internal-${ this . depth + 1 } ` ,
76
73
) ;
@@ -94,13 +91,17 @@ export class EmbedMountable extends FieldMountable {
94
91
MB_DEBUG && console . debug ( 'meta-bind | EmbedMountable >> mount' , this . content ) ;
95
92
super . onMount ( targetEl ) ;
96
93
94
+ targetEl . addClass ( 'mb-embed' ) ;
95
+
97
96
void this . renderContent ( targetEl ) ;
98
97
}
99
98
100
99
protected onUnmount ( targetEl : HTMLElement ) : void {
101
100
MB_DEBUG && console . debug ( 'meta-bind | EmbedMountable >> unmount' , this . content ) ;
102
101
super . onUnmount ( targetEl ) ;
103
102
103
+ targetEl . removeClass ( 'mb-embed' ) ;
104
+
104
105
this . markdownUnloadCallback ?.( ) ;
105
106
106
107
showUnloadedMessage ( targetEl , 'Embed' ) ;
0 commit comments