Format Zotero annotations

Hi!

I was super frustrated with copy/pasting annotations, so I made this little snippet to format a draft copied from Zotero.

// Grab current draft content.
let text = draft.content;

// Pattern matching the Zotero citation structure. Only the last capture group (note) is optional.
const pattern = /“(?<quote>.*?)” \(\[(?<citation>.*?)\]\(zotero:\/\/(?:select|open)\/library\/items\/(?<record_id>.*?)\)\) \(\[pdf\]\(zotero:\/\/open-pdf\/library\/items\/(?<file_id>.*?)\?page=(?<page_num>.*?)&annotation=(?<annotation_id>.*?)\)\)(?<note>.*)/g;

// Capture the groups.
let captured = pattern.exec(text);

let quote = captured.groups.quote;
let citation = captured.groups.citation;

let record_id = captured.groups.record_id;
let file_id = captured.groups.file_id;
let page_num = captured.groups.page_num;
let annotation_id = captured.groups.annotation_id;

let note = captured.groups.note;

// Define a title.
let title = "# Quote from " + citation;

// Reformat the metadata for URLs.
let record = `[${citation}](zotero://select/library/items/${record_id})`;
let file = `[pdf](zotero://open-pdf/library/items/${file_id}?page=1&annotation=${annotation_id})`;

// Join the parts of the head.
let links = [record, file].join('\n');
let header = [title, links].join('\n\n');

// Reformat the quote.
quote = `> ${quote}`

// Append the note.
let body = [quote, note].join('\n\n');

// Attach the head to the body.
let content = [header, body].join('\n\n');

// Dispatch the payload.
draft.content = content;
draft.update();

I hope it helps someone, somewhere.

Install URL.