paperlib icon indicating copy to clipboard operation
paperlib copied to clipboard

[Feature Request] Customizable Bibtex Key

Open thequilo opened this issue 1 year ago • 3 comments

Correct me if I'm wrong, but I think it is currently impossible to customize the bibtex key. To match my current zotero setup, I would like to customize the bibtex keys to match those generated by zotero's betterbibtex integration.

I would also be willing to contribute a PR if I'm shown the correct place to start. I'm not an experienced JS dev, though.

thequilo avatar May 13 '24 08:05 thequilo

Hi, could you please describe what kinds of format do you want?

GeoffreyChen777 avatar May 13 '24 08:05 GeoffreyChen777

In the betterbibtex zotero plugin I have this configuration: auth + year + "_" + shorttitle(3,3), which results in, for example, Levenshtein1965_BinaryCodesCapable.

To match zotero's keys exactly, the title would have to be shortened to three words with unimportant words ignored. For example:

  • "From WER and RIL to MER and WIL: Improved Evaluation Measures for Connected Speech Recognition" -> Morris2004_WERRILMER

thequilo avatar May 13 '24 08:05 thequilo

OK, I would suggest you implement this in a hook extension, it's unnecessary to PR the main app.

https://paperlib.app/en/extension-doc/ext-types/hook-ext.html

We have some hooks for reference exporting. I would suggest you investigate these hooks:

https://paperlib.app/en/extension-doc/process-hook.html#citeobjcreatedinexportbibitem https://paperlib.app/en/extension-doc/process-hook.html#citeobjcreatedinexportbibtexkey https://paperlib.app/en/extension-doc/process-hook.html#citeObjCreatedInExportBibTexBody

The reference exporting logics are at here: https://github.com/Future-Scholars/paperlib/blob/main/app/renderer/services/reference-service.ts

Basically, let's use citeobjcreatedinexportbibtexkey hook as an example, if you hook this point, your function in the extension will receive an instance of Cite, and an instance of PaperEntity. the paperEntity contains all info of this paper. You can create your preferred bibkey, and overwrite the citation-key of the cite instance. After that, return the new cite instance, and you will get what you want.


Some useful source codes: https://github.com/Future-Scholars/paperlib/blob/da0f48e65dd3e4144bb6aaa71c2f07726c3b96f0/app/renderer/services/reference-service.ts#L80 https://github.com/Future-Scholars/paperlib/blob/da0f48e65dd3e4144bb6aaa71c2f07726c3b96f0/app/renderer/services/reference-service.ts#L339-L346

GeoffreyChen777 avatar May 13 '24 08:05 GeoffreyChen777

As this can be achieved via extension, I will close this for now.

If you have any questions about the extension, feel free to tell me.

GeoffreyChen777 avatar May 17 '24 21:05 GeoffreyChen777

Hey, I tried multiple times to add a hook in an extension, but I always get errors. With my current version I get the message [HookService] Failed to recover class of object with undefined in the console when I export the bibitem. I used the plugin template and added

class PaperlibExtension extends PLExtension {
  // ...
  async initialize() {
    // ...
    this.disposeCallbacks.push(
      PLAPI.hookService.hookModify(
        "citeObjCreatedInExportBibItem",
        this.id, 
        "getCiteKey",
      )
    );

  }

  async getCiteKey(cite, paperEntities) {
    console.log("getCiteKey", cite, paperEntities);
  }
}

thequilo avatar Jun 10 '24 09:06 thequilo

I will help you today, but now I'm going to have a meeting with my supervisors. I will investigate this after the meeting.

GeoffreyChen777 avatar Jun 10 '24 09:06 GeoffreyChen777

Hi, I create a demo repo for you:

https://github.com/GeoffreyChen777/paperlib-extension-demo-for-thequilo/tree/main

Your error indicates that you return nothing from the hook function.

In your implementation, you just console.log, and return nothing. The modifyHook requires the function to return the same things as the function arguments. The reason is that the modifyHook aims to modify the properties of the input arguments:

https://paperlib.app/en/extension-doc/ext-types/hook-ext.html#modify-hook-points

If you return nothing, it means that you transform the input arguments into undefined. In this case, you need to use the transformHook:

https://paperlib.app/en/extension-doc/ext-types/hook-ext.html#transform-hook-points


In the demo repo, I implement a function to modify the citation key, you can start from that!

GeoffreyChen777 avatar Jun 10 '24 10:06 GeoffreyChen777