frthjf / obsidian-zotero-sync-client

A Zotero Sync client for Obsidian
GNU Affero General Public License v3.0
24 stars 3 forks source link

Sync across local Zotero port to expose Better BibTex citation keys #9

Open jamespcollins opened 10 months ago

jamespcollins commented 10 months ago

I have a feature request that would hopefully enable some interoperability between this plugin and the Obsidian Pandoc plugin. Ideally, I'd like to have Better BibTex citation keys exposed in the JSON data synced through Zotero Sync Client so I can use Better BibTex citation keys as synced note names. This would allow formatted in-text citation links generated through the Obsidian Pandoc UI to link to synced Zotero notes.

I'm not very familiar with the various Zotero APIs. Obsidian Pandoc uses a local port to grab Better BibTex keys and other item data. Perhaps this local sync method could be added in addition to the Zotero web API?

Thanks for considering. As I have time, I may try to dig into this and add this functionality myself.

frthjf commented 10 months ago

I guess the best way to achieve this would be if the BBT keys were accessible via the Zotero API; it might be best to open an issue upstream for that.

If you have BBT and Zotero running locally, you could probably use the RPC API to get the data during template generation. Something like this:

fetch('http://127.0.0.1:23119/better-bibtex/json-rpc', {
  method: 'POST',
  headers: {
    'Content-Type': 'application/json',
    'Accept': 'application/json'
  },
  body: JSON.stringify({
    "jsonrpc": "2.0", 
    "method": "item.citationkey", 
    "params": [data.key]
  })
})
.then(response => response.json())
.then(data => console.log(data))
.catch((error) => console.error('Error:', error));

It seems like there is a CORS issue though, which you could probably workaround with cors-anywhere or again filing an upstream issue. Let me know if you find a workable solution.

mattie152 commented 6 months ago

Not sure if a solution has been found for this yet, but I had the same issue and realized there is actually a pretty simple workaround! When you change a BetterBibTeX key, it adds it to the "extra" field, and otherwise it is already using your own template, which you can also give to the Zotery Sync template.

In my case the Citation key formula in Better BibTeX is auth + year. My Zotero Sync template has this:

function getCiteKey() {
    if (data.extra && data.extra.contains('Citation Key:')) {
      let extra = data.extra.split('\n');
      let key = extra.filter(e => e.startsWith('Citation Key:'))[0].replace('Citation Key: ', ''));
      return key;
    } else if (data.creators[0]?.lastName && data.date) { 
      let citeKey = '';
      citeKey += data.creators[0]?.lastName;
      let year = new Date(data.date).getFullYear();
      citeKey += year;
      return citeKey;
    } else {
      return '';
    }
  };

let citekey = getCiteKey();

And then the variable 'citekey' can be used in the template as required. This should work as long as you either have a 'lastname' field for at least one creator and a date, or manually set a key within Zotero.

(And any information in the "extra" field should be in format {key}: {value}\n{key2}: {value2} and so on. As far as I know that's already the standard format.)

As for interoperability, I have this in the frontmatter creation part of the template:

  n += 'annotation: "[[Library/Annotations/' + citekey + ']]"\n'

(Replace "Library/Annotations/" with folder of preference.)

Then if you want to create a literature note linked to the Zotero reference you just click this link in the note created by Zotero Sync. The new note can be edited within Obsidian without being changed whenever Zotero Sync syncs, and it will stay linked to the note created with Zotero Sync. Additionally, if you use the Pandoc Reference List plugin the hover references and reference list in your sidebar will automatically link the annotation notes to citations.

If you don't need to make annotations in Obsidian, You could also simply set the citekey as the title for Zotero Sync notes.