@@ -14,7 +14,7 @@ export class D2Processor {
14
14
source : string ,
15
15
el : HTMLElement ,
16
16
ctx : MarkdownPostProcessorContext ,
17
- signal : AbortSignal
17
+ signal ? : AbortSignal
18
18
) => Promise < void >
19
19
> ;
20
20
abortControllerMap : Map < string , AbortController > ;
@@ -36,17 +36,31 @@ export class D2Processor {
36
36
text : "Generating D2 diagram..." ,
37
37
cls : "D2__Loading" ,
38
38
} ) ;
39
- let debouncedFunc = this . debouncedMap . get ( ctx . docId ) ;
39
+
40
+ // we need to generate a debounce per split page, and ctx.containerEl is the only element we have access to that's page specific
41
+ // however, it is not publically available in MarkdownPostProcessorContext, so we hack its access by casting it to an 'any' type
42
+ const pageContainer = ( ctx as any ) . containerEl ;
43
+ let pageID = pageContainer . dataset . pageID ;
44
+ if ( ! pageID ) {
45
+ pageID = Math . floor ( Math . random ( ) * Date . now ( ) ) . toString ( ) ;
46
+ pageContainer . dataset . pageID = pageID ;
47
+ }
48
+
49
+ let debouncedFunc = this . debouncedMap . get ( pageID ) ;
40
50
if ( ! debouncedFunc ) {
51
+ // No need to debounce initial render
52
+ await this . export ( source , el , ctx ) ;
53
+
41
54
debouncedFunc = debounce ( this . export , this . plugin . settings . debounce , {
42
55
leading : true ,
43
56
} ) ;
44
- this . debouncedMap . set ( ctx . docId , debouncedFunc ) ;
57
+ this . debouncedMap . set ( pageID , debouncedFunc ) ;
58
+ return ;
45
59
}
46
60
47
- this . abortControllerMap . get ( ctx . docId ) ?. abort ( ) ;
61
+ this . abortControllerMap . get ( pageID ) ?. abort ( ) ;
48
62
const newAbortController = new AbortController ( ) ;
49
- this . abortControllerMap . set ( ctx . docId , newAbortController ) ;
63
+ this . abortControllerMap . set ( pageID , newAbortController ) ;
50
64
51
65
await debouncedFunc ( source , el , ctx , newAbortController . signal ) ;
52
66
} ;
@@ -88,7 +102,7 @@ export class D2Processor {
88
102
source : string ,
89
103
el : HTMLElement ,
90
104
ctx : MarkdownPostProcessorContext ,
91
- signal : AbortSignal
105
+ signal ? : AbortSignal
92
106
) => {
93
107
try {
94
108
const image = await this . generatePreview ( source , signal ) ;
@@ -126,10 +140,13 @@ export class D2Processor {
126
140
if ( this . prevImage ) {
127
141
this . insertImage ( this . prevImage , el , ctx ) ;
128
142
}
143
+ } finally {
144
+ const pageContainer = ( ctx as any ) . containerEl ;
145
+ this . abortControllerMap . delete ( pageContainer . dataset . id ) ;
129
146
}
130
147
} ;
131
148
132
- async generatePreview ( source : string , signal : AbortSignal ) : Promise < string > {
149
+ async generatePreview ( source : string , signal ? : AbortSignal ) : Promise < string > {
133
150
const pathArray = [ process . env . PATH , "/opt/homebrew/bin" , "/usr/local/bin" ] ;
134
151
135
152
// platform will be win32 even on 64 bit windows
0 commit comments