Skip to content

Commit 6f54606

Browse files
author
Michael Lively
authored
Merge pull request microsoft#156619 from microsoft/mlively/ipynbRender
more checking for undefined objects in attachmentRenderer, fixed bug causing renderer crash
2 parents 0e2e0e2 + 6fbee10 commit 6f54606

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

extensions/ipynb/src/cellAttachmentRenderer.ts

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,11 +22,19 @@ export async function activate(ctx: RendererContext<void>) {
2222
md.renderer.rules.image = (tokens: MarkdownItToken[], idx: number, options, env, self) => {
2323
const token = tokens[idx];
2424
const src = token.attrGet('src');
25-
const attachments: Record<string, Record<string, string>> = env.outputItem.metadata?.custom?.attachments;
25+
const attachments: Record<string, Record<string, string>> = env.outputItem.metadata?.custom?.attachments; // this stores attachment entries for every image in the cell
2626
if (attachments && src) {
27-
const [attachmentKey, attachmentVal] = Object.entries(attachments[src.replace('attachment:', '')])[0];
28-
const b64Markdown = 'data:' + attachmentKey + ';base64,' + attachmentVal;
29-
token.attrSet('src', b64Markdown);
27+
const imageAttachment = attachments[src.replace('attachment:', '')];
28+
if (imageAttachment) {
29+
// objEntries will always be length 1, with objEntries[0] holding [0]=mime,[1]=b64
30+
// if length = 0, something is wrong with the attachment, mime/b64 weren't copied over
31+
const objEntries = Object.entries(imageAttachment);
32+
if (objEntries.length) {
33+
const [attachmentKey, attachmentVal] = objEntries[0];
34+
const b64Markdown = 'data:' + attachmentKey + ';base64,' + attachmentVal;
35+
token.attrSet('src', b64Markdown);
36+
}
37+
}
3038
}
3139

3240
if (original) {

0 commit comments

Comments
 (0)