retorquere / zotero-better-bibtex

Make Zotero effective for us LaTeX holdouts
https://retorque.re/zotero-better-bibtex/
MIT License
5.2k stars 285 forks source link

API documentation for available Zotero item fields ? #2059

Closed Elaws closed 2 years ago

Elaws commented 2 years ago

Hello,

Scripting section of Better BibTeX documentation mentions the following :

item is the Zotero item that’s the source of the reference.

However, I can't seem to find anywhere a complete description of the available fields. For exemple, a message in Zotero forum shows the following example :

if (Translator.BetterTeX) reference.add({ name: 'key', value: item.itemKey })

But item.itemKey does not seem mentionned anywhere else (I've also looked in extra fields section of Better BibTeX documentation and Zotero Item Types and Fields of Zotero official documentation).

Hence the following questions :

  1. Are the Zotero's item fields available in Better BibTeX API described somewhere ?
  2. For each attachment of a Zotero item, I would like to get its Zotero ID or only the storage/ZoteroID/documentName.pdf part of its path : are there item fields that would help for that or do I need to filter the full path using regex to get these values ?

Thanks for taking time to read this, and I'm sorry if this has been answered before, I really tried to search before asking but may have missed it !

retorquere commented 2 years ago

I don't think Zotero has documented this anywhere, but if you export an item to BetterBibTeX JSON you can see exactly what is available to the postscript.

retorquere commented 2 years ago

It is always OK to ask if anything is unclear.

Elaws commented 2 years ago

Thank you for your reply ! This answers my question.

Zotero IDs of attachments are not exported to the BetterBibTex JSON, so I will just try to extract it from the attachment path.

Have a great day ;)

retorquere commented 2 years ago

It'd be easy to add the itemkey.

Elaws commented 2 years ago

To add the Zotero IDs of attachments you meant ?

The use case would be for each attachment to get the "attachmentKey/attachmentName" path so that we have a cross-platform link, in case where you are syncing Zotero's storage folder on multiple platforms.

I can't think of an application of "attachmentKey" alone, but since "attachmentName" is available in the BetterBibTex JSON (title), "attachmentKey/attachmentName" would then obviously be achievable.

retorquere commented 2 years ago

attachments and items are both mostly just "items" to Zotero, they both have an itemKey

Elaws commented 2 years ago

Well, I had no luck so far, there's something I must not understand.

So in order to keep only storage\attachmentKey\documentName I added the following postscript :

if (Translator.BetterBibLaTeX && item.attachments) {
  reference.add({ name: 'fileRelative', value: item.attachments.filter(a => a.defaultPath).replace(/.+?(?=Zotero\\)/, ''), enc: 'attachments' })
}

But fileRelative is unfortunately not added to BibLaTeX export. I'm not against a little help at this point !

Also, attachments in my BetterBibTeX JSON are as follow :

"attachments": [
        {
          "dateAdded": "2022-01-06T18:22:33Z",
          "dateModified": "2022-01-06T18:22:33Z",
          "itemType": "attachment",
          "path": "D:\\Software\\Zotero\\storage\\K24ANA76\\document.pdf",
          "relations": [],
          "tags": [],
          "title": "document.pdf",
          "uri": "http://zotero.org/users/8362141/items/K24ANA76"
        }

For now :

Thanks a lot !

retorquere commented 2 years ago
if (Translator.BetterBibLaTeX && item.attachments) {
  reference.add({ name: 'fileRelative', value: item.attachments.filter(a => a.localPath).map(a => ({ ...a, localPath: a.localPath.replace(/.+?(?=Zotero\\)/i, '') })), enc: 'attachments' })
}

For now :

* `relativePath` is added only if the `value` in my postscript is `item.attachments.filter(a => a.defaultPath)` : as soon as I add the regex, it disappears.

That's because the .filter leaves an array of attachment objects, not the path.

* if I use `a.path` instead of `a.defaultPath`, nothing appears, which is odd because the `Better BibTeX JSON` has a `path` field in `attachments`, not `defaultPath` field.

Yeah, my bad. The BBT JSON format is mostly what the postscript gets. That's mostly but not entirely true for attachments -- the postscripts have mostly been used for single-value fields, and tags. defaultPath is available if you are exporting the attachments. localPath is always present for file attachments and it the actual path into the storage directory. The .filter is still necessary because non-file attachments won't have localPath.

* `enc:` does not seem documented anywhere ?

Same reason: the postscripts have been mostly used for single-value fields, and then you don't really need the enc parameter. It defaults to latex, and BBT knows which fields are the other types and selects the appropriate one automatically, so in most cases, you don't need enc. The valid values are:

Elaws commented 2 years ago

Thanks a lot for the added details and your help for the script : it's perfect :)

retorquere commented 2 years ago

Comments on a closed issue reopen the issue ;) You're welcome.