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
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions content/notero-item.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import Notion from './notion';
import { NoteroPref, getNoteroPref } from './notero-pref';

const APA_STYLE = 'bibliography=http://www.zotero.org/styles/apa';

Expand Down Expand Up @@ -134,6 +135,26 @@ export default class NoteroItem {
return Zotero.URI.getItemURI(this.zoteroItem);
}

public getPDFURL(): string {
const zoteroAPIKey = getNoteroPref(NoteroPref.zoteroAPIKey);
const zoteroUserID = getNoteroPref(NoteroPref.zoteroUserID);
const attachmentIDs = this.zoteroItem
.getAttachments(false)
.slice()
// Sort to get largest ID first
.sort((a, b) => b - a);

for (const id of attachmentIDs) {
Comment on lines +141 to +147
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?

const attachment = Zotero.Items.get(id);
if (attachment.attachmentContentType == 'application/pdf') {
const pdfItemID = Zotero.URI.getItemURI(attachment).split('/').pop();
return `https://api.zotero.org/users/${zoteroUserID}/items/${pdfItemID}/file/view?key=${zoteroAPIKey}`;
Comment on lines +150 to +151
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}`;

}
}
return 'https://zotero.org';
}


public getNotionLinkAttachments(): Zotero.Item[] {
const attachmentIDs = this.zoteroItem
.getAttachments(false)
Expand Down
6 changes: 6 additions & 0 deletions content/notero-pref.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,17 @@ export enum NoteroPref {
notionDatabaseID = 'notionDatabaseID',
notionToken = 'notionToken',
syncOnModifyItems = 'syncOnModifyItems',
zoteroUserID = 'zoteroUserID',
zoteroAPIKey = 'zoteroAPIKey',
}

type NoteroPrefValue = Partial<{
[NoteroPref.collectionName]: string;
[NoteroPref.collectionSyncConfigs]: string;
[NoteroPref.notionDatabaseID]: string;
[NoteroPref.notionToken]: string;
[NoteroPref.zoteroAPIKey]: string;
[NoteroPref.zoteroUserID]: string;
[NoteroPref.syncOnModifyItems]: boolean;
}>;

Expand All @@ -35,6 +39,8 @@ export function getNoteroPref<P extends NoteroPref>(
[NoteroPref.collectionSyncConfigs]: stringPref,
[NoteroPref.notionDatabaseID]: stringPref,
[NoteroPref.notionToken]: stringPref,
[NoteroPref.zoteroAPIKey]: stringPref,
[NoteroPref.zoteroUserID]: stringPref,
[NoteroPref.syncOnModifyItems]: booleanPref,
}[pref];
}
Expand Down
2 changes: 2 additions & 0 deletions content/notero.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.

const response_block = await notion.addEmbedToPage(noteroItem, pageID);
}
}
}
Expand Down
26 changes: 26 additions & 0 deletions content/notion.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ import {
CreatePageResponse,
GetDatabaseResponse,
UpdatePageResponse,
AppendBlockChildrenParameters,
AppendBlockChildrenResponse,
UpdateBlockResponse
} from '@notionhq/client/build/src/api-endpoints';
import 'core-js/stable/object/from-entries';
import NoteroItem from './notero-item';
Expand Down Expand Up @@ -238,6 +241,11 @@ export default class Notion {
type: 'url',
buildRequest: () => item.getZoteroURI(),
},
{
name: 'File URL',
type: 'url',
buildRequest: () => item.getPDFURL(),
},
];

const validPropertyDefinitions =
Expand All @@ -254,4 +262,22 @@ export default class Notion {

return itemProperties;
}

public async addEmbedToPage(
item: NoteroItem,
pageID: string
): Promise<AppendBlockChildrenResponse> {
const children = [
{
"object": "block",
"type": "embed",
"embed": {"url": item.getPDFURL()},
},
];

return await this.client.blocks.children.append({ block_id: pageID, children });


}

}
10 changes: 10 additions & 0 deletions content/preferences.xul
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,16 @@
value="&notero.preferences.notionDatabaseID;"
control="notero-notionDatabaseID" />
<textbox id="notero-notionDatabaseID" preference="pref-notionDatabaseID" />
<separator />
<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" />

<separator />
<label id="notero-zoteroUserID-label"
value="&notero.preferences.zoteroUserID;"
control="notero-zoteroUserID" />
<textbox id="notero-zoteroUserID" preference="pref-zoteroUserID" />
</groupbox>

<separator />
Expand Down
2 changes: 2 additions & 0 deletions defaults/preferences/notero.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,5 @@ pref('extensions.notero.collectionSyncConfigs', '');
pref('extensions.notero.notionToken', '');
pref('extensions.notero.notionDatabaseID', '');
pref('extensions.notero.syncOnModifyItems', true);
pref('extensions.notero.zoteroAPIKey', '');
pref('extensions.notero.zoteroUserID', '');
1 change: 1 addition & 0 deletions locale/en-US/notero.dtd
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
<!ENTITY notero.preferences.readme "README">
<!ENTITY notero.preferences.notionToken "Integration Token">
<!ENTITY notero.preferences.notionDatabaseID "Database ID">
<!ENTITY notero.preferences.zoteroAPIKey "Zotero API Key">

<!ENTITY notero.preferences.syncGroupboxCaption "Sync Preferences">
<!ENTITY notero.preferences.syncGroupboxDescription1 "Notero will monitor the collections enabled below.">
Expand Down
1 change: 1 addition & 0 deletions locale/en-US/notero.properties
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
notero.preferences.collectionName = Collection Name
notero.preferences.notionToken = Notion Integration Token
notero.preferences.notionDatabaseID = Notion Database ID
notero.preferences.zoteroAPIKey = Zotero API Key