@@ -153,16 +153,16 @@ export class AttachmentCleaner implements vscode.CodeActionProvider {
153
153
this . saveAllAttachmentsToCache ( cell . metadata , notebookUri , cellFragment ) ;
154
154
}
155
155
156
- if ( this . checkMetadataAttachmentsExistence ( cell . metadata ) ) {
156
+ if ( this . checkMetadataHasAttachmentsField ( cell . metadata ) ) {
157
157
// the cell metadata contains attachments, check if any are used in the markdown source
158
158
159
- for ( const currFilename of Object . keys ( cell . metadata . attachments ) ) {
159
+ for ( const [ currFilename , attachment ] of Object . entries ( cell . metadata . attachments ) ) {
160
160
// means markdown reference is present in the metadata, rendering will work properly
161
161
// therefore, we don't need to check it in the next loop either
162
162
if ( markdownAttachmentsRefedInCell . has ( currFilename ) ) {
163
163
// attachment reference is present in the markdown source, no need to cache it
164
164
markdownAttachmentsRefedInCell . get ( currFilename ) ! . valid = true ;
165
- markdownAttachmentsInUse [ currFilename ] = cell . metadata . attachments [ currFilename ] ;
165
+ markdownAttachmentsInUse [ currFilename ] = attachment as IAttachmentData ;
166
166
} else {
167
167
// attachment reference is not present in the markdown source, cache it
168
168
this . saveAttachmentToCache ( notebookUri , cellFragment , currFilename , cell . metadata ) ;
@@ -227,7 +227,7 @@ export class AttachmentCleaner implements vscode.CodeActionProvider {
227
227
228
228
const diagnostics : IAttachmentDiagnostic [ ] = [ ] ;
229
229
const markdownAttachments = this . getAttachmentNames ( document ) ;
230
- if ( this . checkMetadataAttachmentsExistence ( activeCell . metadata ) ) {
230
+ if ( this . checkMetadataHasAttachmentsField ( activeCell . metadata ) ) {
231
231
for ( const [ currFilename , attachment ] of markdownAttachments ) {
232
232
if ( ! activeCell . metadata . attachments [ currFilename ] ) {
233
233
// no attachment reference in the metadata
@@ -295,8 +295,8 @@ export class AttachmentCleaner implements vscode.CodeActionProvider {
295
295
* @param metadata metadata of cell
296
296
* @returns boolean representing the presence of any attachments
297
297
*/
298
- private checkMetadataAttachmentsExistence ( metadata : { [ key : string ] : any } ) : boolean {
299
- return ! ! ( metadata . attachments ) ;
298
+ private checkMetadataHasAttachmentsField ( metadata : { [ key : string ] : unknown } ) : metadata is { readonly attachments : Record < string , unknown > } {
299
+ return ! ! metadata . attachments && typeof metadata . attachments === 'object' ;
300
300
}
301
301
302
302
/**
@@ -305,14 +305,16 @@ export class AttachmentCleaner implements vscode.CodeActionProvider {
305
305
* @param notebookUri uri for the notebook being edited
306
306
* @param cellFragment fragment of cell being edited
307
307
*/
308
- private saveAllAttachmentsToCache ( metadata : { [ key : string ] : any } , notebookUri : string , cellFragment : string ) : void {
308
+ private saveAllAttachmentsToCache ( metadata : { [ key : string ] : unknown } , notebookUri : string , cellFragment : string ) : void {
309
309
const documentCache = this . _attachmentCache . get ( notebookUri ) ?? new Map ( ) ;
310
310
this . _attachmentCache . set ( notebookUri , documentCache ) ;
311
311
const cellCache = documentCache . get ( cellFragment ) ?? new Map < string , IAttachmentData > ( ) ;
312
312
documentCache . set ( cellFragment , cellCache ) ;
313
313
314
- for ( const currFilename of Object . keys ( metadata . attachments ) ) {
315
- cellCache . set ( currFilename , metadata . attachments [ currFilename ] ) ;
314
+ if ( metadata . attachments && typeof metadata . attachments === 'object' ) {
315
+ for ( const [ currFilename , attachment ] of Object . entries ( metadata . attachments ) ) {
316
+ cellCache . set ( currFilename , attachment ) ;
317
+ }
316
318
}
317
319
}
318
320
0 commit comments