Skip to content

Commit

Permalink
Merge pull request #40 from munach/35-notes-export-path
Browse files Browse the repository at this point in the history
35 notes export path
  • Loading branch information
floxdeveloper authored Feb 11, 2025
2 parents 16a9eeb + 472cc90 commit 903d825
Show file tree
Hide file tree
Showing 7 changed files with 306 additions and 241 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ As such, you can relate comments for your topics (here 'Hello World') from sever
- {{body}}: 'Body of annotation'

## Versions
1.7.0 add settings for dynamic export path (next to PDF) and export name

1.6.0 fix bug after pdfjs api change

1.5.0 add setting for export path
Expand Down
2 changes: 1 addition & 1 deletion manifest.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"id": "obsidian-extract-pdf-annotations",
"name": "Extract PDF Annotations",
"version": "1.6.0",
"version": "1.7.0",
"minAppVersion": "1.8.0",
"description": "Extract PDF Annotations (text, highlight, underline, squiggle, free text, etc.) from files inside and outside the vault and sort them by topics",
"author": "Franz Achermann",
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "obsidian-extract-pdf-annotations",
"version": "1.6.0",
"version": "1.7.0",
"description": "Extract notes and highlights from PDF Files in Obsidian (https://obsidian.md)",
"main": "main.js",
"scripts": {
Expand Down
44 changes: 36 additions & 8 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,21 +107,27 @@ export default class PDFAnnotationPlugin extends Plugin {
else return text
}

async loadSinglePDFFile(file: TFile) {
async loadSinglePDFFile(pdfFile: TFile) {
const pdfjsLib = await loadPdfJs()
const containingFolder = file.parent.name;
const containingFolder = pdfFile.parent.name;
const grandtotal = [] // array that will contain all fetched Annotations
const desiredAnnotations = this.settings.parsedSettings.desiredAnnotations;
const exportPath = this.settings.exportPath;
console.log('loading from file ', file)
const content = await this.app.vault.readBinary(file)
await loadPDFFile(PDFFile.convertTFileToPDFFile(file, content), pdfjsLib, containingFolder, grandtotal, desiredAnnotations)
console.log('loading from file ', pdfFile)
const content = await this.app.vault.readBinary(pdfFile)
await loadPDFFile(PDFFile.convertTFileToPDFFile(pdfFile, content), pdfjsLib, containingFolder, grandtotal, desiredAnnotations)
this.sort(grandtotal)
const finalMarkdown = this.format(grandtotal, false)

let filePath = file.name.replace(".pdf", ".md");
filePath = "Annotations for " + filePath;
await this.saveHighlightsToFileAndOpenIt(exportPath + filePath, finalMarkdown);
let filePathOfExportNote = "";
const fileNameOfExportNote = this.getResolvedExportName(pdfFile) + '.md';
// Check if export path should be dynamic=next to PDF (./) or static=from settings (path/)
if (exportPath === './') {
filePathOfExportNote = pdfFile.path.replace(pdfFile.name, fileNameOfExportNote);
} else {
filePathOfExportNote = exportPath + fileNameOfExportNote;
}
await this.saveHighlightsToFileAndOpenIt(filePathOfExportNote, finalMarkdown);
}

async loadAnnotationsFromSinglePDFFileFromClipboardPath(filePathFromClipboard: string) {
Expand Down Expand Up @@ -229,6 +235,7 @@ export default class PDFAnnotationPlugin extends Plugin {
'useFolderNames',
'sortByTopic',
'exportPath',
'exportName',
'desiredAnnotations',
'noteTemplateExternalPDFs',
'noteTemplateInternalPDFs',
Expand Down Expand Up @@ -281,6 +288,13 @@ export default class PDFAnnotationPlugin extends Plugin {
);
}

get exportNameTemplate(): Template {
return compileTemplate(
this.settings.exportName,
this.templateSettings,
);
}

getTemplateVariablesForAnnotation(annotation: any): Record<string, any> {
const shortcuts = {
highlightedText: annotation.highlightedText,
Expand All @@ -295,6 +309,14 @@ export default class PDFAnnotationPlugin extends Plugin {
return { annotation: annotation, ...shortcuts };
}

getTemplateVariablesForExportName(file: TFile): Record<string, any> {
const shortcuts = {
filename: file.basename,
};

return { file: file, ...shortcuts };
}


getContentForNoteFromExternalPDF(annotation: any): string {
return this.noteFromExternalPDFsTemplate(
Expand All @@ -320,6 +342,12 @@ export default class PDFAnnotationPlugin extends Plugin {
);
}

getResolvedExportName(file: TFile): string {
return this.exportNameTemplate(
this.getTemplateVariablesForExportName(file),
);
}

async saveHighlightsToFileAndOpenIt(filePath: string, mdString: string) {
const fileExists = await this.app.vault.adapter.exists(filePath);
if (fileExists) {
Expand Down
Loading

0 comments on commit 903d825

Please sign in to comment.