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

Display Pocket-Highlights #107

Draft
wants to merge 2 commits into
base: master
Choose a base branch
from
Draft
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
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ following variables are supported:
| `{{tags-no-hash}}` | The Pocket tags for the Pocket item |
| `{{tags}}` | The Pocket tags for the Pocket item, with "#" prepended |
| `{{image}}` | The main image for the Pocket item |
| `{{highlights}}` | The highlighted areas of your Pocket item as list |

This is the default template in obsidian-pocket. It will populate the [YAML
frontmatter](https://help.obsidian.md/Advanced+topics/YAML+front+matter) of the
Expand Down
11 changes: 11 additions & 0 deletions src/ItemNote.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
URLToPocketItemNoteIndex,
} from "./data/URLToPocketItemNoteIndex";
import {
Highlight,
PocketTags,
pocketTagsToPocketTagList,
SavedPocketItem,
Expand Down Expand Up @@ -205,6 +206,12 @@ const generateInitialItemNoteContents = (
tags
);

const normalizeHighlights = (highlights?: Highlight[]) => {
if (!highlights) return "No Highlights";

return `${highlights.map((h) => `- ${h.quote}`).join("\n")}`;
};

const substitutions: Map<string, SubstitutionFn> = new Map([
["title", (item) => normalizeTitle(item.resolved_title) ?? "Untitled"],
["url", (item) => item.resolved_url ?? "Missing URL"],
Expand All @@ -219,6 +226,7 @@ const generateInitialItemNoteContents = (
return image_src ? `![image](${image_src})` : "";
},
],
["highlights", (item) => normalizeHighlights(item.annotations)],
]);

return Array.from(substitutions.entries()).reduce((acc, currentValue) => {
Expand Down Expand Up @@ -275,6 +283,9 @@ Excerpt: >
---
{{tags}}
{{image}}

### Highlights
{{highlights}}
`;

const loadTemplateContents = async (
Expand Down
7 changes: 4 additions & 3 deletions src/pocket_api/PocketAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ export const getRequestToken: GetRequestToken = async (authRedirectURI) => {
const REQUEST_TOKEN_URL = "https://getpocket.com/v3/oauth/request";

const responseBody = await doRequest(REQUEST_TOKEN_URL, {
consumer_key: CONSUMER_KEY,
consumer_key: "78809-9423d8c743a58f62b23ee85c",
redirect_uri: authRedirectURI,
});

Expand All @@ -99,7 +99,7 @@ export const getAccessToken: GetAccessToken = async () => {
const ACCESS_TOKEN_URL = "https://getpocket.com/v3/oauth/authorize";

const responseBody = await doRequest(ACCESS_TOKEN_URL, {
consumer_key: CONSUMER_KEY,
consumer_key: "78809-9423d8c743a58f62b23ee85c",
code: storedRequestToken,
});

Expand Down Expand Up @@ -128,13 +128,14 @@ export const getPocketItems: GetPocketItems = async (
const nextTimestamp = Math.floor(Date.now() / 1000);

const requestOptions = {
consumer_key: CONSUMER_KEY,
consumer_key: "78809-9423d8c743a58f62b23ee85c",
access_token: accessToken,
since: !!lastUpdateTimestamp
? new Number(lastUpdateTimestamp).toString()
: null,
detailType: "complete",
tag: pocketSyncTag,
annotations: "1",
};

if (!!lastUpdateTimestamp) {
Expand Down
10 changes: 10 additions & 0 deletions src/pocket_api/PocketAPITypes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,23 @@ export interface Image {
src: string;
}

export interface Highlight {
annotation_id: string;
created_at: string;
item_id: string;
patch: string;
quote: string;
version: string;
}

export interface SavedPocketItem extends BasePocketItem {
status: PocketItemStatus.Unread | PocketItemStatus.Archived;
resolved_title: string;
resolved_url: string;
excerpt: string;
tags: PocketTags;
image?: Image;
annotations?: Highlight[];
}

export const pocketTagsToPocketTagList = (tags: PocketTags): PocketTag[] =>
Expand Down