Skip to content

Commit

Permalink
Disables plugin temporarily and shows a modal instead
Browse files Browse the repository at this point in the history
  • Loading branch information
akaalias committed Jan 13, 2021
1 parent 3791300 commit b5c4e3d
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 29 deletions.
4 changes: 2 additions & 2 deletions manifest.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
{
"id": "obsidian-extract-pdf-highlights",
"name": "PDF Highlights",
"version": "0.0.2",
"version": "0.0.3",
"minAppVersion": "0.9.12",
"description": "Extract highlights, underlines and annotations from your PDFs into Obsidian",
"description": "[Currently disabled] Extract highlights, underlines and annotations from your PDFs into Obsidian",
"author": "Alexis Rondeau",
"authorUrl": "https://publish.obsidian.md/alexisrondeau",
"isDesktopOnly": false
Expand Down
8 changes: 4 additions & 4 deletions src/PDFAnnotationsManager.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import pdfjsCustom from "node_modules/pdfjs-dist/build/pdf";
import worker from "node_modules/pdfjs-dist/build/pdf.worker.entry";
import * as customPDFJS from "node_modules/pdfjs-dist/build/pdf";
import * as customPDFJSWorker from "node_modules/pdfjs-dist/build/pdf.worker.entry";

var finalHighlightsAnnotations = new Array();

export default class PDFAnnotationsManager {

async fetchRawAnnotationsFromPDF(arrayBuffer) {

pdfjsCustom.GlobalWorkerOptions.workerSrc = worker;
customPDFJS.GlobalWorkerOptions.workerSrc = customPDFJSWorker;

finalHighlightsAnnotations = new Array();

var SUPPORTED_ANNOTS = ['Text', 'Highlight', 'Underline'];

var loadingTask = pdfjsCustom.getDocument(arrayBuffer);
var loadingTask = customPDFJS.getDocument(arrayBuffer);

return await loadingTask.promise
.then(function (doc) {
Expand Down
69 changes: 46 additions & 23 deletions src/main.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import { Plugin } from 'obsidian';
import {App, Modal, Plugin} from 'obsidian';

import ExtractPDFHighlightsPluginSettings from "./ExtractPDFHighlightsPluginSettings";
import ExtractPDFHighlightsPluginSettingsTab from "./ExtractPDFHighlightsPluginSettingsTab";
import PDFAnnotationsManager from "./PDFAnnotationsManager";
// import PDFAnnotationsManager from "./PDFAnnotationsManager";

export default class ExtractPDFHighlightsPlugin extends Plugin {

public settings: ExtractPDFHighlightsPluginSettings;
private modal: ProgressModal;

async onload() {
this.loadSettings();
Expand All @@ -19,27 +20,30 @@ export default class ExtractPDFHighlightsPlugin extends Plugin {
onunload() {}

async processPDFHighlights() {
let file = this.app.workspace.getActiveFile();

if (file === null) return;
if (file.extension !== 'pdf') return;

let arrayBuffer = await this.app.vault.readBinary(file);
let pdfAnnotationsManager = new PDFAnnotationsManager();

let rawAnnotationsFromPDF = await pdfAnnotationsManager.fetchRawAnnotationsFromPDF(arrayBuffer);
let filteredAnnotations = pdfAnnotationsManager.filterRawAnnotations(rawAnnotationsFromPDF);
let groupedAnnotationsByPageMap = pdfAnnotationsManager.groupAnnotationsByPage(filteredAnnotations);
let sortedAnnotationsByPositionGroupedByPage = pdfAnnotationsManager.sortAnnotationsByPosition(groupedAnnotationsByPageMap);
let flattenedAnnotationsByPosition = pdfAnnotationsManager.flattenAnnotationsByPosition(sortedAnnotationsByPositionGroupedByPage);

const finalMarkdown = this.generateFinalMarkdown(flattenedAnnotationsByPosition, file.name);

let filePath = file.name.replace(".pdf", ".md");
filePath = "Highlights for " + filePath;

await this.saveHighlightsToFile(filePath, finalMarkdown);
await this.app.workspace.openLinkText(filePath, '', true);
this.modal = new ProgressModal(this.app);
this.modal.open();

// let file = this.app.workspace.getActiveFile();
//
// if (file === null) return;
// if (file.extension !== 'pdf') return;
//
// let arrayBuffer = await this.app.vault.readBinary(file);
// let pdfAnnotationsManager = new PDFAnnotationsManager();
//
// let rawAnnotationsFromPDF = await pdfAnnotationsManager.fetchRawAnnotationsFromPDF(arrayBuffer);
// let filteredAnnotations = pdfAnnotationsManager.filterRawAnnotations(rawAnnotationsFromPDF);
// let groupedAnnotationsByPageMap = pdfAnnotationsManager.groupAnnotationsByPage(filteredAnnotations);
// let sortedAnnotationsByPositionGroupedByPage = pdfAnnotationsManager.sortAnnotationsByPosition(groupedAnnotationsByPageMap);
// let flattenedAnnotationsByPosition = pdfAnnotationsManager.flattenAnnotationsByPosition(sortedAnnotationsByPositionGroupedByPage);
//
// const finalMarkdown = this.generateFinalMarkdown(flattenedAnnotationsByPosition, file.name);
//
// let filePath = file.name.replace(".pdf", ".md");
// filePath = "Highlights for " + filePath;
//
// await this.saveHighlightsToFile(filePath, finalMarkdown);
// await this.app.workspace.openLinkText(filePath, '', true);
}

private generateFinalMarkdown(annotations, fileName) {
Expand Down Expand Up @@ -135,3 +139,22 @@ export default class ExtractPDFHighlightsPlugin extends Plugin {
return "";
}
}

class ProgressModal extends Modal {
public fileName: string;

constructor(app: App) {
super(app);
}

onOpen() {
let {contentEl} = this;
contentEl.createEl("h2", {text: "Extract PDF Highlights"});
contentEl.createEl("p", {text: "I'm sorry but due to an unexpected incompatibility with Obsidian Core PDF handling as of v0.10.8 this plugin is currently disabled. In the meantime, you can use Zotero + Zotfile to extract PDF highlights and annotations. I'm sorry for the inconvenience and working on fixing this issue. If you have any questions, please email me at [email protected]! Thank you for your patience, Alexis :)"});
}

onClose() {
let {contentEl} = this;
contentEl.empty();
}
}

0 comments on commit b5c4e3d

Please sign in to comment.