diff --git a/README.md b/README.md index 18b04483..dcf3f4ee 100644 --- a/README.md +++ b/README.md @@ -60,9 +60,9 @@ The script needs to be able to: Current features (check out the wiki for more details): * **Custom note types** - You're not limited to the 6 built-in note types of Anki. -* **Custom scan directory** +* **Custom scan directories** * The plugin will scan the entire vault by default - * You can also set which directory (includes all sub-directories as well) to scan via plugin settings + * You can also set which directories (includes all sub-directories as well) to scan via plugin settings * **Ignore Folders and Files** * You can specify which files and folders to ignore * This can be done in the settings of this plugin with [Glob syntax](https://en.wikipedia.org/wiki/Glob_(programming)#Syntax). diff --git a/main.ts b/main.ts index 4b4c1d30..1f09d0a3 100644 --- a/main.ts +++ b/main.ts @@ -32,7 +32,7 @@ export default class MyPlugin extends Plugin { "Frozen Fields Line": "FROZEN" }, Defaults: { - "Scan Directory": "", + "Scan Directories": [], "Tag": "Obsidian_to_Anki", "Deck": "Default", "Scheduling Interval": 0, @@ -195,16 +195,18 @@ export default class MyPlugin extends Plugin { } new Notice("Successfully connected to Anki! This could take a few minutes - please don't close Anki until the plugin is finished") const data: ParsedSettings = await settingToData(this.app, this.settings, this.fields_dict) - const scanDir = this.app.vault.getAbstractFileByPath(this.settings.Defaults["Scan Directory"]) + const scanDirs = this.settings.Defaults["Scan Directories"]; let manager = null; - if (scanDir !== null) { + if (scanDirs && scanDirs.length > 0) { let markdownFiles = []; - if (scanDir instanceof TFolder) { - console.info("Using custom scan directory: " + scanDir.path) - markdownFiles = this.getAllTFilesInFolder(scanDir); - } else { - new Notice("Error: incorrect path for scan directory " + this.settings.Defaults["Scan Directory"]) - return + for (const dirPath of scanDirs) { + const scanDir = this.app.vault.getAbstractFileByPath(dirPath); + if (scanDir instanceof TFolder) { + console.info("Using custom scan directory: " + scanDir.path) + markdownFiles.push(...this.getAllTFilesInFolder(scanDir)); + } else { + new Notice("Error: incorrect path for scan directory " + dirPath) + } } manager = new FileManager(this.app, data, markdownFiles, this.file_hashes, this.added_media) } else { diff --git a/src/interfaces/settings-interface.ts b/src/interfaces/settings-interface.ts index dd022b0f..b43d8a55 100644 --- a/src/interfaces/settings-interface.ts +++ b/src/interfaces/settings-interface.ts @@ -18,7 +18,7 @@ export interface PluginSettings { "Frozen Fields Line": string }, Defaults: { - "Scan Directory": string, + "Scan Directories": string[], "Tag": string, "Deck": string, "Scheduling Interval": number diff --git a/src/settings.ts b/src/settings.ts index 08f42fe2..8827d8df 100644 --- a/src/settings.ts +++ b/src/settings.ts @@ -2,7 +2,7 @@ import { PluginSettingTab, Setting, Notice, TFolder } from 'obsidian' import * as AnkiConnect from './anki' const defaultDescs = { - "Scan Directory": "The directory to scan. Leave empty to scan the entire vault", + "Scan Directories": "The directories to scan. Leave empty to scan the entire vault. One path per line.", "Tag": "The tag that the plugin automatically adds to any generated cards.", "Deck": "The deck the plugin adds cards to if TARGET DECK is not specified in the file.", "Scheduling Interval": "The time, in minutes, between automatic scans of the vault. Set this to 0 to disable automatic scanning.", @@ -175,10 +175,22 @@ export class SettingsTab extends PluginSettingTab { const plugin = (this as any).plugin let defaults_settings = containerEl.createEl('h3', {text: 'Defaults'}) - // To account for new scan directory - if (!(plugin.settings["Defaults"].hasOwnProperty("Scan Directory"))) { - plugin.settings["Defaults"]["Scan Directory"] = "" + // Migration from old setting + if (plugin.settings["Defaults"].hasOwnProperty("Scan Directory")) { + const oldValue = plugin.settings["Defaults"]["Scan Directory"]; + if (typeof oldValue === 'string' && oldValue.trim() !== '') { + plugin.settings["Defaults"]["Scan Directories"] = [oldValue]; + } else { + plugin.settings["Defaults"]["Scan Directories"] = []; + } + delete plugin.settings["Defaults"]["Scan Directory"]; + plugin.saveAllData(); + } + + if (!(plugin.settings["Defaults"].hasOwnProperty("Scan Directories"))) { + plugin.settings["Defaults"]["Scan Directories"] = [] } + // To account for new add context if (!(plugin.settings["Defaults"].hasOwnProperty("Add Context"))) { plugin.settings["Defaults"]["Add Context"] = false @@ -195,9 +207,25 @@ export class SettingsTab extends PluginSettingTab { if (!(plugin.settings["Defaults"].hasOwnProperty("Add Obsidian Tags"))) { plugin.settings["Defaults"]["Add Obsidian Tags"] = false } + + new Setting(defaults_settings) + .setName("Scan Directories") + .setDesc(defaultDescs["Scan Directories"]) + .addTextArea(text => { + text.setValue(plugin.settings.Defaults["Scan Directories"].join("\n")) + .setPlaceholder("path/to/folder1\npath/to/folder2") + .onChange((value) => { + let scanDirs = value.split("\n").map(dir => dir.trim()).filter(dir => dir !== ""); + plugin.settings.Defaults["Scan Directories"] = scanDirs; + plugin.saveAllData(); + }) + text.inputEl.rows = 5; + text.inputEl.cols = 30; + }); + for (let key of Object.keys(plugin.settings["Defaults"])) { // To account for removal of regex setting - if (key === "Regex") { + if (key === "Regex" || key === "Scan Directories") { continue } if (typeof plugin.settings["Defaults"][key] === "string") {