fix for attachment naming within notebook metadata

This commit is contained in:
Michael Lively 2022-08-25 15:04:56 -07:00
parent 74acf9c038
commit 16f842ea76

View file

@ -31,13 +31,13 @@ class CopyPasteEditProvider implements vscode.DocumentPasteEditProvider {
} }
// get filename data from paste // get filename data from paste
const pasteFilename = dataItem.asFile()?.name; const clipboardFilename = dataItem.asFile()?.name;
if (!pasteFilename) { if (!clipboardFilename) {
return undefined; return undefined;
} }
const separatorIndex = pasteFilename?.lastIndexOf('.'); const separatorIndex = clipboardFilename?.lastIndexOf('.');
const filename = pasteFilename?.slice(0, separatorIndex); const filename = clipboardFilename?.slice(0, separatorIndex);
const filetype = pasteFilename?.slice(separatorIndex); const filetype = clipboardFilename?.slice(separatorIndex);
if (!filename || !filetype) { if (!filename || !filetype) {
return undefined; return undefined;
} }
@ -46,13 +46,12 @@ class CopyPasteEditProvider implements vscode.DocumentPasteEditProvider {
if (!currentCell) { if (!currentCell) {
return undefined; return undefined;
} }
const notebookUri = currentCell.notebook.uri; const notebookUri = currentCell.notebook.uri;
// create updated metadata for cell (prep for WorkspaceEdit) // create updated metadata for cell (prep for WorkspaceEdit)
const b64string = encodeBase64(fileDataAsUint8); const b64string = encodeBase64(fileDataAsUint8);
const startingAttachments = currentCell.metadata.custom?.attachments; const startingAttachments = currentCell.metadata.custom?.attachments;
const newMetadata = buildMetadata(b64string, currentCell, pasteFilename, filetype, startingAttachments); const [newMetadata, revisedFilename] = buildMetadata(b64string, currentCell, filename, filetype, startingAttachments);
// build edits // build edits
const nbEdit = vscode.NotebookEdit.updateCellMetadata(currentCell.index, newMetadata); const nbEdit = vscode.NotebookEdit.updateCellMetadata(currentCell.index, newMetadata);
@ -62,8 +61,8 @@ class CopyPasteEditProvider implements vscode.DocumentPasteEditProvider {
// create a snippet for paste // create a snippet for paste
const pasteSnippet = new vscode.SnippetString(); const pasteSnippet = new vscode.SnippetString();
pasteSnippet.appendText('!['); pasteSnippet.appendText('![');
pasteSnippet.appendPlaceholder(`${pasteFilename}`); pasteSnippet.appendPlaceholder(`${revisedFilename}`);
pasteSnippet.appendText(`](attachment:${pasteFilename})`); pasteSnippet.appendText(`](attachment:${revisedFilename})`);
return { insertText: pasteSnippet, additionalEdit: workspaceEdit }; return { insertText: pasteSnippet, additionalEdit: workspaceEdit };
} }
@ -123,27 +122,27 @@ function encodeBase64(buffer: Uint8Array, padded = true, urlSafe = false) {
return output; return output;
} }
function buildMetadata(b64: string, cell: vscode.NotebookCell, filename: string, filetype: string, startingAttachments: any): { [key: string]: any } { function buildMetadata(b64: string, cell: vscode.NotebookCell, filename: string, filetype: string, startingAttachments: any): [{ [key: string]: any }, string] {
const outputMetadata = { ...cell.metadata }; const outputMetadata = { ...cell.metadata };
let tempFilename = filename + filetype;
if (!outputMetadata.custom) { if (!outputMetadata.custom) {
outputMetadata['custom'] = { 'attachments': { [filename]: { 'image/png': b64 } } }; outputMetadata['custom'] = { 'attachments': { [tempFilename]: { 'image/png': b64 } } };
} else if (!outputMetadata.custom.attachments) { } else if (!outputMetadata.custom.attachments) {
outputMetadata.custom['attachments'] = { [filename]: { 'image/png': b64 } }; outputMetadata.custom['attachments'] = { [tempFilename]: { 'image/png': b64 } };
} else { } else {
for (let appendValue = 2; filename in startingAttachments; appendValue++) { for (let appendValue = 2; tempFilename in startingAttachments; appendValue++) {
const objEntries = Object.entries(startingAttachments[filename]); const objEntries = Object.entries(startingAttachments[tempFilename]);
if (objEntries.length) { // check that mime:b64 are present if (objEntries.length) { // check that mime:b64 are present
const [, attachmentb64] = objEntries[0]; const [, attachmentb64] = objEntries[0];
if (attachmentb64 !== b64) { // append a "-#" here. same name, diff data. this matches jupyter behavior if (attachmentb64 !== b64) { // append a "-#" here. same name, diff data. this matches jupyter behavior
filename = filename.concat(`-${appendValue}`) + filetype; tempFilename = filename.concat(`-${appendValue}`) + filetype;
} }
} }
} }
outputMetadata.custom.attachments[filename] = { 'image/png': b64 }; outputMetadata.custom.attachments[tempFilename] = { 'image/png': b64 };
} }
return [outputMetadata, tempFilename];
return outputMetadata;
} }
export function imagePasteSetup() { export function imagePasteSetup() {