urschrei / pyzotero

Pyzotero: a Python client for the Zotero API
https://pyzotero.readthedocs.org
Other
908 stars 99 forks source link

Markdown formatting and line breaks not recognized when creating notes #168

Open 9j7axvsLuF opened 1 year ago

9j7axvsLuF commented 1 year ago

When creating notes using pyzotero, the markdown formatting isn't being recognized. I've followed the documentation closely, but the created notes show markdown characters (such as #) instead of rendering them as intended, and collapse line breaks (\n) into whitespaces.

Here's a simplified Python code snippet to demonstrate the issue:

from pyzotero import zotero

# Initialize Zotero API
library_id = "your_library_id"
api_key = "your_api_key"
zot = zotero.Zotero(library_id, "user", api_key)

# Retrieve a sample item
sample_item = zot.top()[0]

# Create a note with markdown formatting
note_template = zot.item_template('note')
note_template['note'] = "# Note Title\n\nThis is a note."
zot.create_items([note_template], sample_item['key'])

The created note should render the markdown formatting, displaying as follows in Zotero:

Note Title

This is a note.

Instead, this is what I see:

# Note Title This is a note.

When I export the resulting note in markdown, I get this with the hash symbol escaped:

\# Note Title This is a note.

At the very least I would like my created notes to respect line breaks in the content, otherwise the notes are unreadable in Zotero.

Additional Context:

9j7axvsLuF commented 1 year ago

I found a (perhaps hacky) solution: converting the content of the note from markdown to html using the markdown package before passing it to Zotero, like so:

note_content = markdown.markdown(note_content)

There might be a better way to do this though!

urschrei commented 1 year ago

It looks like the escaping is being inserted on the server side. Here's the JSON Pyzotero sends in its POST request:

{"itemType": "note", "note": "# Note Title\n\nThis is a note.", "tags": [], "collections": [], "relations": {}}

I'll see whether someone on the Zotero side knows what's going on.

9j7axvsLuF commented 1 year ago

Thanks for the quick response! In any case the html trick shared above works great. Perhaps there could be args in a future version of pyzotero to automatically handle the markdown-to-html conversion for markdown content, or at least something in the docs to explain how to preserve formatting (unless, of course, Zotero decides to change their API).