Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding ability to embed PDFs in Notion using Zotero Web API #107

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

advaitathreya
Copy link

@advaitathreya advaitathreya commented Aug 15, 2022

I've added the ability to embed PDFs using the Zotero Web API. This is not ready to actually be released, there are a lot of issues that need to be fixed that I was unable to figure out:

  • the preference window doesn't seem to register the entered User ID and API Key. It gives me an error that the preference value is null. I tested it using the defaults and then removed the values before committing
  • there are no options to not include the embed- that would need to be added to allow people to decide whether or not to have it

I also don't think I followed appropriate conventions, since I have not used TypeScript before. I hope this isn't too much additional work for you to clean up, my motivation in doing this was mostly just to see if it would be possible. Look forward to seeing a cleaned up version of this in some future release if you think it is worth it!

References #4

@advaitathreya advaitathreya marked this pull request as ready for review August 15, 2022 21:18
Copy link
Owner

@dvanoni dvanoni left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@advaitathreya, thanks for sharing this! I hadn't dug into the Zotero API yet, so I appreciate that you found the correct URL for retrieving the PDF files.

I agree this would need a way for users to opt in to the embedding functionality, and it should definitely be possible to add that into the preferences.

My biggest concern with this approach is that it requires embedding a user's Zotero API key into Notion pages (in the embed URL), and I see that as having potential to open the user up to security vulnerabilities. I imagine this is one of the reasons why the Zotero API docs recommend against authenticating with the API in this way. I suppose we could leave this decision up to the user, but I'd want to put a clear disclaimer about the risk implied by using this functionality.

I can think of a couple other things we'd need to figure out before we could release this:

  • We need to somehow confirm that the user is signed in to a Zotero account and has syncing enabled since this would be a prerequisite for this feature. If they don't, it'd be nice to display a message describing what this feature could do if they enabled Zotero account syncing.
  • We need to find a way to prevent re-appending the embed block whenever the item is modified in Zotero so that we don't end up with multiple embeds of the file.

That said, I think this is a great start to a solution for this!

Comment on lines +141 to +147
const attachmentIDs = this.zoteroItem
.getAttachments(false)
.slice()
// Sort to get largest ID first
.sort((a, b) => b - a);

for (const id of attachmentIDs) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In your issue comment, you mentioned that we could use getBestAttachment() for this. Just curious if that ended up not working for you?

Comment on lines +150 to +151
const pdfItemID = Zotero.URI.getItemURI(attachment).split('/').pop();
return `https://api.zotero.org/users/${zoteroUserID}/items/${pdfItemID}/file/view?key=${zoteroAPIKey}`;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Zotero has a getItemPath() method that should make this easier. It also includes the user ID, so we don't need to have that as a separate pref. 🙂

Suggested change
const pdfItemID = Zotero.URI.getItemURI(attachment).split('/').pop();
return `https://api.zotero.org/users/${zoteroUserID}/items/${pdfItemID}/file/view?key=${zoteroAPIKey}`;
return `https://api.zotero.org/${Zotero.URI.getItemPath(attachment)}/file/view?key=${zoteroAPIKey}`;

@@ -287,6 +287,8 @@ class Notero {

if ('url' in response) {
await noteroItem.saveNotionLinkAttachment(response.url);
const pageID = Notion.getPageIDFromURL(Notion.convertWebURLToLocal(response.url));
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think you can actually get the page ID from response.id.

<label id="notero-zoteroAPIKey-label"
value="&notero.preferences.zoteroAPIKey;"
control="notero-zoteroAPIKey" />
<textbox id="notero-zoteroAPIKey" preference="pref-zoteroAPIKey" />
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see why the preferences window isn't registering these values. In addition to what you have here, you also need to add a corresponding <preference> element within the <preferences> element on line 14.

<preference id="pref-zoteroAPIKey"
            name="extensions.notero.zoteroAPIKey"
            type="string" />

@advaitathreya
Copy link
Author

Thanks for your comments! I will come back to this when I have time and work on trying to address them.

On a different note, I just realized that PDF annotations made within the Zotero app are not stored with the PDF file but separately, which sort of negates my initial motivation to embed the PDF in Notion, which was to have one central place where all the annotations and notes on a certain article are saved. There's definitely still a way to do this, since the annotations are also supposedly accessible through the API, but this is not gonna be a straightforward task.

@SvennoNito
Copy link

Are there any news on this functionality?

@advaitathreya
Copy link
Author

Are there any news on this functionality?

Just as an update from my end, I no longer use Notion (moved to using Obsidian to have the ability to save my notes locally), so I'm not working on this. Still think this is a wonderful plugin though, just not something I need anymore.

@jiwatode-mohit
Copy link

Is it possible to embed the pdf that is stored in the Zotero library in Notion now?
If so, how can I do it?

@dvanoni
Copy link
Owner

dvanoni commented Feb 19, 2024

There haven't been any changes to the Notion or Zotero APIs, so the approach discussed in this PR is still the only way I know for embedding a Zotero item's PDF into a Notion page. This means it's also still vulnerable to the security issue I described above.

@jiwatode-mohit, if you're interested in manually embedding a PDF into Notion and accept the security risk, I believe you can do so by embedding a URL with the following format:

https://api.zotero.org/users/<userID>/items/<attachmentKey>/file/view?key=<privateKey>

You'll need to fill in the values for those three pieces of the URL: <userID>, <attachmentKey>, <privateKey>

You can find each of them as follows after logging into your Zotero account (https://www.zotero.org/user/login).

userID

You can find your userID on this page: https://www.zotero.org/settings/keys

privateKey

You can create a new private key here: https://www.zotero.org/settings/keys/new

attachmentKey

  1. Find the item you want in your Zotero web library: https://www.zotero.org/mylibrary
  2. Open the PDF for the item and take note of the URL which should be in the following format:
    https://www.zotero.org/<username>/items/<itemKey>/attachment/<attachmentKey>/reader
  3. Copy the <attachmentKey> part of the URL

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants