Future-Scholars / paperlib

An open-source academic paper management tool.
https://paperlib.app
GNU General Public License v3.0
1.58k stars 68 forks source link

[Feature Request] Customizable Bibtex Key #526

Closed thequilo closed 6 months ago

thequilo commented 6 months ago

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.

GeoffreyChen777 commented 6 months ago

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

thequilo commented 6 months ago

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:

GeoffreyChen777 commented 6 months ago

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 commented 6 months ago

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.

thequilo commented 5 months ago

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);
  }
}
GeoffreyChen777 commented 5 months ago

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 commented 5 months ago

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!