diff --git a/main.ts b/main.ts index 9917e84..6394d3a 100644 --- a/main.ts +++ b/main.ts @@ -200,32 +200,87 @@ export default class SmartMemosPlugin extends Plugin { const text = editor.getRange({ line: 0, ch: 0 }, position); const regex = [/(?<=\[\[)(([^[\]])+)\.(mp3|mp4|mpeg|mpga|m4a|wav|webm)(?=]])/g, /(?<=\[(.*)]\()(([^[\]])+)\.(mp3|mp4|mpeg|mpga|m4a|wav|webm)(?=\))/g]; - this.findFilePath(text, regex).then((audioFile: TFile) => { - this.app.vault.readBinary(audioFile).then((audioBuffer) => { - if (this.writing) { - new Notice('Generator is already in progress.'); - return; - } - this.writing = true; - new Notice("Generating transcript..."); - const fileType = audioFile.extension; - this.generateTranscript(audioBuffer, fileType).then((result) => { - this.transcript = result; - const prompt = this.settings.prompt + result; - new Notice('Transcript generated...'); + this.findFilePath(text, regex).then((path) => { + const fileType = path.split('.').pop(); + if (fileType == undefined || fileType == null || fileType == '') { + new Notice('No audio file found'); + } else { + this.app.vault.adapter.exists(path).then((exists) => { + if (!exists) throw new Error(path + ' does not exist'); + this.app.vault.adapter.readBinary(path).then((audioBuffer) => { + if (this.writing) { + new Notice('Generator is already in progress.'); + return; + } + this.writing = true; + new Notice("Generating transcript..."); + this.generateTranscript(audioBuffer, fileType).then((result) => { + this.transcript = result; + const prompt = this.settings.prompt + result; + new Notice('Transcript generated...'); this.generateText(prompt, editor , editor.getCursor('to').line); - }).catch(error => { - console.warn(error.message); - new Notice(error.message); - this.writing = false; + }).catch(error => { + console.warn(error.message); + new Notice(error.message); + this.writing = false; + }); + }); }); - }); + } }).catch(error => { console.warn(error.message); new Notice(error.message); }); } + + async findFilePath(text: string, regex: RegExp[]) { + const fullPath = await this.getAttachmentDir().then((attachmentPath) => { + let filename = ''; + let result: RegExpExecArray | null; + for (const reg of regex) { + while ((result = reg.exec(text)) !== null) { + filename = normalizePath(decodeURI(result[0])).trim(); + } + } + + if (filename == '') throw new Error('No file found in the text.'); + + const fileInSpecificFolder = filename.includes('/'); + const AttInRootFolder = attachmentPath === '' || attachmentPath === '/'; + const AttInCurrentFolder = attachmentPath.startsWith('./'); + const AttInSpecificFolder = !AttInRootFolder && !AttInCurrentFolder; + + let fullPath = ''; + + if (AttInRootFolder || fileInSpecificFolder) fullPath = filename; + else { + if (AttInSpecificFolder) fullPath = attachmentPath + '/' + filename; + if (AttInCurrentFolder) { + const attFolder = attachmentPath.substring(2); + if (attFolder.length == 0) fullPath = this.getCurrentPath() + '/' + filename; + else fullPath = this.getCurrentPath() + '/' + attFolder + '/' + filename; + } + } + + const exists = this.app.vault.getAbstractFileByPath(fullPath) instanceof TAbstractFile; + if (exists) return fullPath; + else { + let path = ''; + let found = false; + this.app.vault.getFiles().forEach((file) => { + if (file.name === filename) { + path = file.path; + found = true; + } + }); + if (found) return path; + else throw new Error('File not found'); + } + }); + return fullPath as string; + } + async generateTranscript(audioBuffer: ArrayBuffer, filetype: string) { if (this.settings.apiKey.length <= 1) throw new Error('OpenAI API Key is not provided.'); @@ -296,46 +351,21 @@ export default class SmartMemosPlugin extends Plugin { return results.join(' '); } - async findFilePath(text: string, regex: RegExp[]) { - let filename = ''; - let result: RegExpExecArray | null; + async getAttachmentDir() { + const activeFile = this.app.workspace.getActiveFile(); + if (!activeFile) throw new Error('No active file'); + const dir = this.app.vault.getAvailablePathForAttachments(activeFile.basename, activeFile?.extension, activeFile); // getAvailablePathForAttachments is undocumented + console.log('dir: ', dir); + return dir; + } - // Find the filename using the provided regex patterns - for (const reg of regex) { - while ((result = reg.exec(text)) !== null) { - filename = normalizePath(decodeURI(result[0])).trim(); - } - } - - if (filename === '') { - throw new Error('No file found in the text.'); - } - - let fullPath; - if (filename.includes(this.appJsonObj.attachmentFolderPath)) { - fullPath = filename; - console.log('full path 1: ', fullPath); - } else { - - // Ensure no leading or trailing slashes in the attachment folder path and filename - const folderPath = this.appJsonObj.attachmentFolderPath.replace(/\/$/, ''); // Remove trailing slash if any - const filePath = filename.replace(/^\//, ''); // Remove leading slash if any - - // Construct the full path - fullPath = `${folderPath}/${filePath}`; - fullPath = normalizePath(fullPath); // Normalize the full path - - console.log('full path 2: ', fullPath); - - } - - // Attempt to find the file in the vault - const file = this.app.vault.getAbstractFileByPath(fullPath); - if (file instanceof TFile) { - return file; - } else { - throw new Error('File not found at ' + fullPath); - } + getCurrentPath() { + const activeFile = this.app.workspace.getActiveFile(); + if (!activeFile) throw new Error('No active file'); + const currentPath = activeFile.path.split('/'); + currentPath.pop(); + const currentPathString = currentPath.join('/'); + return currentPathString; } diff --git a/manifest.json b/manifest.json index 7a89076..8494df1 100644 --- a/manifest.json +++ b/manifest.json @@ -1,7 +1,7 @@ { "id": "smart-memos", "name": "Smart Memos", - "version": "1.1.1", + "version": "1.1.2", "minAppVersion": "0.15.0", "description": "Create personalized and intelligent analysis, summaries, and more for audio recordings that can be imported or spoken directly into a note", "author": "Evan Moscoso", diff --git a/package-lock.json b/package-lock.json index 1279599..26e374f 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,15 +1,16 @@ { "name": "smart-memos", - "version": "1.0.10", + "version": "1.1.1", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "smart-memos", - "version": "1.0.10", + "version": "1.1.1", "license": "MIT", "dependencies": { "bootstrap": "^5.3.3", + "obsidian": "latest", "smart-chat-model": "^1.0.5", "wavesurfer.js": "^7.8.0" }, @@ -21,7 +22,6 @@ "builtin-modules": "3.3.0", "dotenv": "^16.4.5", "esbuild": "0.17.3", - "obsidian": "^1.5.7-1", "tslib": "2.4.0", "typescript": "4.7.4" } @@ -30,14 +30,12 @@ "version": "6.4.1", "resolved": "https://registry.npmjs.org/@codemirror/state/-/state-6.4.1.tgz", "integrity": "sha512-QkEyUiLhsJoZkbumGZlswmAhA7CBU02Wrz7zvH4SrcifbsqwlXShVXg65f3v/ts57W3dqyamEriMhij1Z3Zz4A==", - "dev": true, "peer": true }, "node_modules/@codemirror/view": { - "version": "6.28.2", - "resolved": "https://registry.npmjs.org/@codemirror/view/-/view-6.28.2.tgz", - "integrity": "sha512-A3DmyVfjgPsGIjiJqM/zvODUAPQdQl3ci0ghehYNnbt5x+o76xq+dL5+mMBuysDXnI3kapgOkoeJ0sbtL/3qPw==", - "dev": true, + "version": "6.28.4", + "resolved": "https://registry.npmjs.org/@codemirror/view/-/view-6.28.4.tgz", + "integrity": "sha512-QScv95fiviSQ/CaVGflxAvvvDy/9wi0RFyDl4LkHHWiMr/UPebyuTspmYSeN5Nx6eujcPYwsQzA6ZIZucKZVHQ==", "peer": true, "dependencies": { "@codemirror/state": "^6.4.0", @@ -544,7 +542,6 @@ "version": "5.60.8", "resolved": "https://registry.npmjs.org/@types/codemirror/-/codemirror-5.60.8.tgz", "integrity": "sha512-VjFgDF/eB+Aklcy15TtOTLQeMjTo07k7KAjql8OK5Dirr7a6sJY4T1uVBDuTVG9VEmn1uUsohOpYnVfgC6/jyw==", - "dev": true, "dependencies": { "@types/tern": "*" } @@ -805,8 +802,7 @@ "node_modules/@types/estree": { "version": "1.0.5", "resolved": "https://registry.npmjs.org/@types/estree/-/estree-1.0.5.tgz", - "integrity": "sha512-/kYRxGDLWzHOB7q+wtSUQlFrtcdUccpfy+X+9iMBpHK8QLLhx2wIPYuS5DYtR9Wa/YlZAbIovy7qVdB1Aq6Lyw==", - "dev": true + "integrity": "sha512-/kYRxGDLWzHOB7q+wtSUQlFrtcdUccpfy+X+9iMBpHK8QLLhx2wIPYuS5DYtR9Wa/YlZAbIovy7qVdB1Aq6Lyw==" }, "node_modules/@types/geojson": { "version": "7946.0.14", @@ -830,7 +826,6 @@ "version": "0.23.9", "resolved": "https://registry.npmjs.org/@types/tern/-/tern-0.23.9.tgz", "integrity": "sha512-ypzHFE/wBzh+BlH6rrBgS5I/Z7RD21pGhZ2rltb/+ZrVM1awdZwjx7hE5XfuYgHWk9uvV5HLZN3SloevCAp3Bw==", - "dev": true, "dependencies": { "@types/estree": "*" } @@ -2009,7 +2004,6 @@ "version": "2.29.4", "resolved": "https://registry.npmjs.org/moment/-/moment-2.29.4.tgz", "integrity": "sha512-5LC9SOxjSc2HF6vO2CyuTDNivEdoz2IvyJJGj6X8DJ0eFyfszE0QiEd+iXmBvUP3WHxSjFH/vIsA0EN00cgr8w==", - "dev": true, "engines": { "node": "*" } @@ -2031,7 +2025,6 @@ "version": "1.5.7-1", "resolved": "https://registry.npmjs.org/obsidian/-/obsidian-1.5.7-1.tgz", "integrity": "sha512-T5ZRuQ1FnfXqEoakTTHVDYvzUEEoT8zSPnQCW31PVgYwG4D4tZCQfKHN2hTz1ifnCe8upvwa6mBTAP2WUA5Vng==", - "dev": true, "dependencies": { "@types/codemirror": "5.60.8", "moment": "2.29.4" @@ -2356,7 +2349,6 @@ "version": "4.1.2", "resolved": "https://registry.npmjs.org/style-mod/-/style-mod-4.1.2.tgz", "integrity": "sha512-wnD1HyVqpJUI2+eKZ+eo1UwghftP6yuFheBqqe+bWCotBjC2K1YnteJILRMs3SM4V/0dLEW1SC27MWP5y+mwmw==", - "dev": true, "peer": true }, "node_modules/supports-color": { @@ -2471,7 +2463,6 @@ "version": "2.2.8", "resolved": "https://registry.npmjs.org/w3c-keyname/-/w3c-keyname-2.2.8.tgz", "integrity": "sha512-dpojBhNsCNN7T82Tm7k26A6G9ML3NkhDsnw9n/eoxSRlVBB4CEtIQ/KTCLI2Fwf3ataSXRhYFkQi3SlnFwPvPQ==", - "dev": true, "peer": true }, "node_modules/wavesurfer.js": { diff --git a/package.json b/package.json index a370a2b..3175cbc 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "smart-memos", - "version": "1.1.1", + "version": "1.1.2", "description": "Create personalized and intelligent analysis, summaries, and more for audio recordings that can be imported or spoken directly into a note", "main": "main.js", "scripts": { @@ -30,12 +30,12 @@ "builtin-modules": "3.3.0", "dotenv": "^16.4.5", "esbuild": "0.17.3", - "obsidian": "^1.5.7-1", "tslib": "2.4.0", "typescript": "4.7.4" }, "dependencies": { "bootstrap": "^5.3.3", + "obsidian" : "latest", "smart-chat-model": "^1.0.5", "wavesurfer.js": "^7.8.0" }