Skip to content

Instantly share code, notes, and snippets.

@Maarrk
Last active March 9, 2023 19:13
Show Gist options
  • Star 6 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Maarrk/01172c1689897979b944c05f8ca948b3 to your computer and use it in GitHub Desktop.
Save Maarrk/01172c1689897979b944c05f8ca948b3 to your computer and use it in GitHub Desktop.
Zotero items into dendron notes

Zotero items into dendron notes

See demo on imgur

Prerequisites

Ensure you have these:

  • Visual Studio Code
    • Dendron
    • Citation Picker for Zotero
  • Zotero
    • Better BibTeX

Setup

  • Change Citation Picker "Port" setting:
"zotero-citation-picker.port": "http://127.0.0.1:23119/better-bibtex/cayw?format=playground&keyprefix=[[bib.&keypostfix=]]"
    hooks:
        onCreate:
            -
                id: newbib
                pattern: bib.*
                type: js
  • Replace the newbib.js hook with the file below

Usage

  • Add a citation with Zotero Citation Picker
  • Once you get the [[bib.smithExamplePaper2022]] links, visit them to create filled notes
/**
@params wsRoot: string, root of your current workspace
@params note: Object with following properties https://github.com/dendronhq/dendron/blob/master/packages/common-all/src/types/foundation.ts#L66:L66
@params NoteUtils: utilities for working with notes. [code](https://github.com/dendronhq/dendron/blob/master/packages/common-all/src/dnode.ts#L323:L323)
@params execa: instance of [execa](https://github.com/sindresorhus/execa#execacommandcommand-options)
@params axios: instance of [axios](https://axios-http.com/docs/example)
@params _: instance of [lodash](https://lodash.com/docs)
*/
module.exports = async function ({ wsRoot, note, NoteUtils, execa, axios, _ }) {
// Prevent making any changes if the note already exists
if (note.body !== "") {
return { note };
}
let csl = {};
try {
const response = await axios({
url: 'http://localhost:23119/better-bibtex/json-rpc',
method: 'post',
headers: {
'Content-Type': 'application/json',
'Accept': 'application/json'
},
data: {
"jsonrpc": "2.0",
"method": "item.export",
"params": [
[note.title],
'f4b52ab0-f878-4556-85a0-c7aeedd09dfc' // GUID of translator 'Better CSL JSON'
]
}
})
if ("error" in response.data) {
note.body = `Zotero responded with error code \`${response.data.error.code}\`, message: \`${response.data.error.message}\`\n`;
return { note };
}
csl = JSON.parse(response.data.result[2])[0]; // Skip HTTP response code and content type, get first paper
} catch (error) {
note.body = `Importing data from BibTeX failed with error:\n\n\`\`\`\n${error}\n\`\`\`\n`;
return { note };
}
note.title = csl.title;
var shortAuthor = "";
if (csl.author.length === 1) {
shortAuthor = csl.author[0].family;
} else if (csl.author.length === 2) {
shortAuthor = csl.author[0].family + " and " + csl.author[1].family;
} else {
shortAuthor = csl.author[0].family + " et al.";
}
note.desc = shortAuthor + ", " + csl.issued["date-parts"][0][0].toString();
note.body =
note.body + `\n## Abstract\n\n${csl.abstract}\n\n## Authors\n\n`;
for (author of csl.author) {
note.body = note.body + `- ${author.family}, ${author.given}\n`;
}
return { note };
};
@Sylvan54
Copy link

Sylvan54 commented Nov 8, 2022

Hi @Maarrk I followed the steps and got this in the note created, any thoughts? Thanks in advance.

---
id: 44k3mtwwd3yoy9b9l2e4qp5
title: Beaty2001
desc: ''
updated: 1667940497030
created: 1667940497030
---
Importing data from Zotero failed with error:

```
TypeError: Cannot read properties of undefined (reading '2')
```

@Maarrk
Copy link
Author

Maarrk commented Nov 8, 2022

@Sylvan54 this happens when Zotero didn't find a paper with key Beaty2001. But it doesn't make sense, if you created the link with citationpicker...

I might rework it to fail in a nicer way at some point. But you're not the first person to report this issue, I need to see if I missed a step in the instructions

@alatuszam
Copy link

Thank you for this @Maarrk . If I may, I would like to add a bug report.

Refactoring or renaming notes that are referenced by the zotero-generated bib.* note, causes the content of the note to be deleted and to throw:

Importing data from Zotero failed with error:

TypeError: Cannot read properties of undefined (reading '2')

To reproduce:

  • Generate bib.* note following the steps outlined above.
  • Add a reference to the note by creating a new note, such as [[test.zotero]]
  • use ctrl + enter to navigate to [[test.zotero]]
  • rename note using dendron: rename note
  • revisit the bib.* note

Thanks again for this use of hooks!

@Maarrk
Copy link
Author

Maarrk commented Nov 16, 2022

Thank you all for feedback, I improved the hook to solve the issues.

@Sylvan54 now the note will tell what error Zotero gave you

@alatuszam thanks for finding that, I could reproduce the issue you described so I raised #3785 upstream. The new version has this workaround at the start that exits early if the note already has something

@alatuszam
Copy link

Awesome thanks @Maarrk

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment