Skip to content

Commit

Permalink
fix: keep extra in journal item & remove arXiv key
Browse files Browse the repository at this point in the history
Thanks @falbarelli from #24
  • Loading branch information
AllanChain committed Nov 26, 2024
1 parent 4e9e917 commit 5775ae2
Showing 1 changed file with 22 additions and 2 deletions.
24 changes: 22 additions & 2 deletions src/modules/arxiv-merge.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { catchError } from "./error";
import { getPref } from "../utils/prefs";

export class arXivMerge {
static reservedKeys = ["collections", "dateAdded", "dateModified", "extra"];
static reservedKeys = ["collections", "dateAdded", "dateModified"];

@catchError
static registerRightClickMenuItem() {
Expand Down Expand Up @@ -82,6 +82,21 @@ export class arXivMerge {
// @ts-ignore some fields are not listed in zotero-type
journalJSON[field] = preprintJSON[field];
});
// `extra` field need more care
const preprintExtra = Zotero.Utilities.Internal.extractExtraFields(
preprintJSON.extra as string,
);
journalJSON.extra = Zotero.Utilities.Internal.combineExtraFields(
journalJSON.extra as string,
preprintExtra.fields,
);
// We generally want to keep extra information, but remove arXiv info
const notes = preprintExtra.extra
.split("\n")
.filter((line) => !line.startsWith("arXiv:"))
.join("\n");
if (notes) journalJSON.extra += "\n" + notes;

/* Avoid citation key collision after preprint item updates (say year)
* For example, no collision:
* - Published item: li_wang_2024-1
Expand All @@ -96,7 +111,12 @@ export class arXivMerge {
* The workaround here is to set a random citation key for the published item.
* TODO: What if the user wants to keep the citation key (not fixed in BBT)?
*/
publishedItem.setField("extra", `Citation Key: ${crypto.randomUUID()}`);
const tempExtra = Zotero.Utilities.Internal.combineExtraFields(
journalJSON.extra as string,
// @ts-expect-error key not listed in type
new Map([["Citation Key", crypto.randomUUID()]]),
);
publishedItem.setField("extra", tempExtra);
publishedItem.saveTx();
preprintItem.fromJSON(journalJSON);
preprintItem.saveTx();
Expand Down

0 comments on commit 5775ae2

Please sign in to comment.