Skip to content

Commit

Permalink
Add better URI handling; upgrade packages
Browse files Browse the repository at this point in the history
  • Loading branch information
mgmeyers committed Jul 3, 2023
1 parent 3282048 commit 8d641d1
Show file tree
Hide file tree
Showing 22 changed files with 2,903 additions and 2,761 deletions.
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"id": "obsidian-zotero-desktop-connector",
"name": "Zotero Integration",
"version": "2.3.11",
"version": "2.4.0",
"minAppVersion": "1.1.1",
"description": "Insert and import citations, bibliographies, notes, and PDF annotations from Zotero.",
"author": "mgmeyers",
Expand Down
80 changes: 35 additions & 45 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,62 +9,52 @@
"check-types": "tsc --noemit",
"lint": "eslint ./src",
"lint:fix": "eslint ./src/**/* --fix",
"sort-imports": "import-sort --write \"./src/**/*.{ts,tsx}\"",
"prettier": "prettier --write \"./src/**/*.{ts,tsx}\"",
"clean": "yarn sort-imports && yarn prettier && yarn lint:fix",
"clean": "yarn prettier && yarn lint:fix",
"test": "jest"
},
"keywords": [],
"author": "",
"license": "MIT",
"devDependencies": {
"@babel/core": "^7.17.9",
"@babel/preset-env": "^7.16.11",
"@babel/preset-typescript": "^7.16.7",
"@types/download": "^8.0.1",
"@types/jest": "27.4.1",
"@types/node": "16.11.6",
"@types/nunjucks": "^3.2.1",
"@typescript-eslint/eslint-plugin": "5.2.0",
"@typescript-eslint/parser": "5.2.0",
"babel-jest": "^27.5.1",
"builtin-modules": "3.2.0",
"@babel/core": "7.22.5",
"@babel/preset-env": "7.22.5",
"@babel/preset-typescript": "7.22.5",
"@trivago/prettier-plugin-sort-imports": "4.1.1",
"@types/download": "8.0.2",
"@types/jest": "29.5.2",
"@types/node": "20.3.3",
"@types/nunjucks": "3.2.3",
"@typescript-eslint/eslint-plugin": "5.61.0",
"@typescript-eslint/parser": "5.61.0",
"babel-jest": "29.5.0",
"builtin-modules": "3.3.0",
"cross-env": "7.0.3",
"esbuild": "0.13.12",
"eslint": "^8.12.0",
"eslint-plugin-react": "^7.29.4",
"import-sort-cli": "^6.0.0",
"import-sort-style-module-alias": "^1.1.0",
"jest": "27.5.1",
"moment": "^2.29.2",
"obsidian": "^1.2.8",
"obsidian-plugin-cli": "^0.9.0",
"prettier": "^2.6.1",
"tslib": "2.3.1",
"typescript": "4.4.4",
"xml-js": "^1.6.11"
"esbuild": "0.18.11",
"eslint": "8.44.0",
"eslint-plugin-react": "7.32.2",
"import-sort-cli": "6.0.0",
"import-sort-style-module-alias": "1.1.0",
"jest": "29.5.0",
"moment": "2.29.4",
"obsidian": "1.2.8",
"obsidian-plugin-cli": "0.9.0",
"prettier": "2.8.8",
"tslib": "2.6.0",
"typescript": "5.1.6",
"xml-js": "1.6.11"
},
"dependencies": {
"@types/react-dom": "17.0.13",
"download": "^8.0.0",
"escape-path-with-spaces": "1.0.0",
"execa": "^7.1.1",
"fuse.js": "^6.5.3",
"nunjucks": "3.2.3",
"preact": "10.6.6",
"@types/react-dom": "18.2.6",
"download": "8.0.0",
"escape-path-with-spaces": "1.0.2",
"execa": "7.1.1",
"fuse.js": "6.6.2",
"nunjucks": "3.2.4",
"preact": "10.15.1",
"react": "npm:@preact/compat",
"react-dom": "npm:@preact/compat",
"react-json-tree": "0.16.1",
"react-select": "5.2.2"
},
"importSort": {
".js, .ts, .tsx": {
"style": "module-alias",
"options": {
"alias": [
"src"
]
}
}
"react-json-tree": "0.18.0",
"react-select": "5.7.3"
}
}
4 changes: 4 additions & 0 deletions .prettierrc.js → prettier.config.cjs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/* eslint-disable no-undef */
module.exports = {
arrowParens: 'always',
bracketSpacing: true,
Expand All @@ -13,4 +14,7 @@ module.exports = {
tabWidth: 2,
trailingComma: 'es5',
useTabs: false,
importOrder: ['^[./]'],
importOrderSeparation: true,
importOrderSortSpecifiers: true,
};
2 changes: 1 addition & 1 deletion src/bbt/basicTemplates/applyBasicTemplates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ const annotationsTemplate = `
{% for annotation in annots -%}
{%- if annotation.annotatedText -%}
> “{{annotation.annotatedText}}”{% if annotation.color %} {{annotation.colorCategory}} {{annotation.type | capitalize}} {% else %} {{annotation.type | capitalize}} {% endif %}[Page {{annotation.page}}]({{annotation.attachment.uri.replace("http://zotero.org", "zotero://open-pdf")}}?page={{annotation.page}})
> “{{annotation.annotatedText}}”{% if annotation.color %} {{annotation.colorCategory}} {{annotation.type | capitalize}} {% else %} {{annotation.type | capitalize}} {% endif %}[Page {{annotation.page}}]({{annotation.attachment.desktopURI.replace("select", "open-pdf")}}?page={{annotation.pageLabel}}&annotation={{annotation.id}})
{%- endif %}
{%- if annotation.imageRelativePath -%}
> ![[{{annotation.imageRelativePath}}]]
Expand Down
2 changes: 1 addition & 1 deletion src/bbt/cayw.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import { Notice, request } from 'obsidian';

import { getCurrentWindow } from '../helpers';
import { CitationFormat, Database } from '../types';
import { LoadingModal } from './LoadingModal';
import { defaultHeaders, getPort } from './helpers';
import { getBibFromCiteKeys } from './jsonRPC';
import { LoadingModal } from './LoadingModal';

export function getCiteKeyFromAny(item: any): CiteKey | null {
if (!item.citekey && !item.citationKey) return null;
Expand Down
31 changes: 23 additions & 8 deletions src/bbt/export.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { copyFileSync, existsSync, mkdirSync } from 'fs';
import path from 'path';

import { Notice, TFile, htmlToMarkdown, moment, normalizePath } from 'obsidian';
import path from 'path';

import { doesEXEExist, getVaultRoot } from '../helpers';
import {
Expand All @@ -14,7 +13,12 @@ import { applyBasicTemplates } from './basicTemplates/applyBasicTemplates';
import { CiteKey, getCiteKeyFromAny, getCiteKeys } from './cayw';
import { processZoteroAnnotationNotes } from './exportNotes';
import { extractAnnotations } from './extractAnnotations';
import { getColorCategory, mkMDDir, sanitizeFilePath } from './helpers';
import {
getColorCategory,
getLocalURI,
mkMDDir,
sanitizeFilePath,
} from './helpers';
import {
getAttachmentsFromCiteKey,
getBibFromCiteKey,
Expand Down Expand Up @@ -59,10 +63,12 @@ function processAttachment(attachment: any) {

if (attachment.uri) {
attachment.itemKey = attachment.uri.split('/').pop();
attachment.desktopURI = attachment.uri.replace(
'http://zotero.org',
'zotero://select'
);
attachment.desktopURI =
attachment.select || getLocalURI('select', attachment.uri);

if (attachment.path?.endsWith('.pdf')) {
attachment.pdfURI = getLocalURI('open-pdf', attachment.uri);
}
}
}

Expand All @@ -82,6 +88,10 @@ function processAnnotation(
path.join(imageRelativePath, annotation.imageBaseName)
);
}

annotation.desktopURI = getLocalURI('open-pdf', attachment.uri, {
page: annotation.pageLabel,
});
}

function convertNativeAnnotation(
Expand All @@ -105,6 +115,10 @@ function convertNativeAnnotation(
y: rect[1],
color: annotation.annotationColor,
colorCategory: getColorCategory(annotation.annotationColor),
desktopURI: getLocalURI('open-pdf', attachment.uri, {
page: annotation.annotationPageLabel,
annotation: annotation.key,
}),
source: 'zotero',
};

Expand Down Expand Up @@ -218,7 +232,8 @@ async function processItem(
item.importDate = importDate;
// legacy
item.exportDate = importDate;
item.desktopURI = item.uri?.replace('http://zotero.org', 'zotero://select');
item.desktopURI =
item.select || getLocalURI('select', item.uri, item.itemKey);

if (item.accessDate) {
item.accessDate = moment(item.accessDate);
Expand Down
21 changes: 8 additions & 13 deletions src/bbt/exportNotes.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import { copyFileSync } from 'fs';
import path from 'path';

import {
Editor,
Notice,
Expand All @@ -10,12 +8,12 @@ import {
htmlToMarkdown,
normalizePath,
} from 'obsidian';

import path from 'path';
import { getVaultRoot } from 'src/helpers';

import { Database } from '../types';
import { getCiteKeys } from './cayw';
import { mkMDDir, sanitizeFilePath } from './helpers';
import { getLocalURI, mkMDDir, sanitizeFilePath } from './helpers';
import { getAttachmentsFromCiteKey, getNotesFromCiteKeys } from './jsonRPC';
import { removeStartingSlash } from './template.helpers';

Expand Down Expand Up @@ -64,10 +62,10 @@ export async function processZoteroAnnotationNotes(
isImage ? 'afterend' : 'beforebegin',
createEl('a', {
text: 'Go to annotation',
href: `${json.attachmentURI.replace(
'http://zotero.org',
'zotero://open-pdf'
)}?page=${json.pageLabel}&annotation=${json.annotationKey}`,
href: getLocalURI('open-pdf', json.attachmentURI, {
page: json.pageLabel,
annotation: json.annotationKey,
}),
})
);
if (isImage) {
Expand Down Expand Up @@ -101,10 +99,7 @@ export async function processZoteroAnnotationNotes(
citeSpan.empty();
citeSpan.createEl('a', {
text,
href: json.citationItems[0].uris[0].replace(
'http://zotero.org',
'zotero://select'
),
href: getLocalURI('select', json.citationItems[0].uris[0]),
});
} catch (e) {
console.error(e);
Expand Down Expand Up @@ -145,7 +140,7 @@ export async function noteExportPrompt(
const images: Record<string, string> = {};

attachment.forEach((a: any) => {
a.annotations.forEach((annot: any) => {
a.annotations?.forEach((annot: any) => {
if (annot.annotationType === 'image') {
images[annot.key] = annot.annotationImagePath;
}
Expand Down
4 changes: 1 addition & 3 deletions src/bbt/extractAnnotations.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import path from 'path';

import { execa } from 'execa';
import { Notice } from 'obsidian';

import path from 'path';
import { getExeName, getExeRoot } from 'src/helpers';

import { LoadingModal } from './LoadingModal';
Expand Down
1 change: 0 additions & 1 deletion src/bbt/getCiteKeyExport.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { request } from 'obsidian';

import { Database } from 'src/types';

import { defaultHeaders, getPort } from './helpers';
Expand Down
45 changes: 44 additions & 1 deletion src/bbt/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import path from 'path';

import { Database } from 'src/types';

export const defaultHeaders = {
Expand Down Expand Up @@ -123,3 +122,47 @@ export function getColorCategory(hex: string) {
}
return 'Red';
}

/**
* Open a PDF at a given page (or try to)
*
* zotero://open-pdf/library/items/[itemKey]?page=[page]
* zotero://open-pdf/groups/[groupID]/items/[itemKey]?page=[page]
*
* Also supports ZotFile format:
* zotero://open-pdf/[libraryID]_[key]/[page]
*/

/**
* Select an item
*
* zotero://select/library/items/[itemKey]
* zotero://select/groups/[groupID]/items/[itemKey]
*
* Deprecated:
*
* zotero://select/[type]/0_ABCD1234
* zotero://select/[type]/1234 (not consistent across synced machines)
*/
export function getLocalURI(
ext: 'select' | 'open-pdf',
uri: string,
params?: Record<string, string>
) {
const itemId = uri.split('/').pop();
const prefix = `zotero://${ext}`;
let url = '';

if (/group/.test(uri)) {
url = uri.replace('http://zotero.org', prefix);
} else {
url = `${prefix}/library/items/${itemId}`;
}

if (params) {
const p = new URLSearchParams(params);
url += `?${p}`;
}

return url;
}
2 changes: 1 addition & 1 deletion src/bbt/jsonRPC.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ import { Notice, htmlToMarkdown, moment, request } from 'obsidian';

import { padNumber } from '../helpers';
import { Database } from '../types';
import { LoadingModal } from './LoadingModal';
import { CiteKey, getCiteKeyFromAny } from './cayw';
import { defaultHeaders, getPort } from './helpers';
import { LoadingModal } from './LoadingModal';

export async function getNotesFromCiteKeys(
citeKeys: CiteKey[],
Expand Down
1 change: 0 additions & 1 deletion src/bbt/template.helpers.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Notice, TFile, moment } from 'obsidian';

import { ExportToMarkdownParams } from 'src/types';

export function loadTemplate(
Expand Down
1 change: 0 additions & 1 deletion src/citeSuggest/citeSuggest.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import {
MarkdownView,
Platform,
} from 'obsidian';

import { isZoteroRunning } from 'src/bbt/cayw';
import { getAllCiteKeys } from 'src/bbt/getCiteKeyExport';
import ZoteroConnector from 'src/main';
Expand Down
5 changes: 2 additions & 3 deletions src/helpers.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { execa } from 'execa';
import fs from 'fs';
import { FileSystemAdapter, Notice } from 'obsidian';
import os from 'os';
import path from 'path';

import { execa } from 'execa';
import { FileSystemAdapter, Notice } from 'obsidian';

export function getCurrentWindow() {
return require('electron').remote.BrowserWindow.getFocusedWindow();
}
Expand Down
Loading

0 comments on commit 8d641d1

Please sign in to comment.