diff --git a/README.md b/README.md index ead22cb..8467855 100644 --- a/README.md +++ b/README.md @@ -21,6 +21,7 @@ Therefore, this plugin supports regex and regex groups to create the destination - [UI Guide](docs/ui-guide.md) - Complete overview of the plugin interface and settings - [Moving Rules](docs/moving-rules.md) - Filename-based rules with regex examples +- [Tag Rules](docs/tag-rules.md) - Filename-based rules with regex examples - [Project Rules](docs/project-rules.md) - Organize files by project using frontmatter - [Exclusion Rules](docs/exclusion-rules.md) - Protect specific files and folders diff --git a/Utils/RuleMatcherUtil.ts b/Utils/RuleMatcherUtil.ts index e313a0c..8afe3ec 100644 --- a/Utils/RuleMatcherUtil.ts +++ b/Utils/RuleMatcherUtil.ts @@ -99,10 +99,21 @@ class RuleMatcherUtil { */ public getGroupMatches(file: TFile, rule: MovingRule): RegExpMatchArray | null { const regex = this.getCompiledRegex(rule.regex); + console.log("Compiled regex: ", regex); const matches = file.name.match(regex); return matches; } + public getGroupMatchesForTags(tags: TagCache[], rule: MovingRule): RegExpMatchArray | null { + const regex = this.getCompiledRegex(rule.regex); + console.log("Compiled regex: ", regex); + for (const tag of tags) { + const matches = tag.tag.match(regex); + } + + return matches; + } + /** * Checks if the regex pattern is valid * Meaning, syntax errors are caught, but not logical errors @@ -112,6 +123,7 @@ class RuleMatcherUtil { */ private isValidRegex(pattern: string): boolean { try { + console.log("Validating regex pattern: ", pattern); new RegExp(pattern); return true; } catch (e) { diff --git a/docs/project-rules.md b/docs/project-rules.md index 9994312..3a0d565 100644 --- a/docs/project-rules.md +++ b/docs/project-rules.md @@ -1,6 +1,9 @@ # Project Rules Project rules allow you to organize files by project using frontmatter metadata. Files with a `Project` or `project` field in their frontmatter will be matched against project rules first, before checking regular moving rules or tag rules. +For reference, frontmatter refers to the obsidian file properties defined at the top of a note, enclosed within triple dashes (`---`). + +Reference to file properties: https://help.obsidian.md/properties ## How Project Rules Work diff --git a/docs/tag-rules.md b/docs/tag-rules.md new file mode 100644 index 0000000..e0d9476 --- /dev/null +++ b/docs/tag-rules.md @@ -0,0 +1,28 @@ +# Tag Rules + +The tag rules apply only to the one tag, tag arrays and sequences are not supported. +And if you define multiple tag rules, only the first matching one will be applied. + +## Important Notes + +#### Important note 1: The text is case sensitive. +#### Important note 2: Use the "/" character to separate folders. +#### Writing Regex: The best tool for writing regex is Regex101 (https://regex101.com/) + +## Examples without Regex + +Lets imagine you have some files tagged with different tags such as #work, #personal and #urgent. + +And tag rules defined such as these: +Regex: work → Folder: Work +Regex: personal → Folder: Personal +Regex: #urgent → Folder: Urgent +(As you can see, if you use # before the tag in the rulename, it will work as well.) + +After applying the tag rules, the files will be moved to their respective folders based on their tags. + +## Example with Regex + +Imagine you have files tagged with tags like #project-alpha, #project-beta, and #project-gamma. + + diff --git a/docs/ui-guide.md b/docs/ui-guide.md index c7f638d..b9487a9 100644 --- a/docs/ui-guide.md +++ b/docs/ui-guide.md @@ -69,8 +69,15 @@ Each project rule can be collapsed individually to hide its sub-rules, and the e image 1. **Add Project rule button**: This button will add a project rule to the list of project rules. -2. **Delete rule button**: This button will delete the selected rule from the list of rules. -3. **Duplicate rule button**: This button will duplicate the selected rule from the list of rules. +2. **Delete rule button**: This button will delete the selected project rule from the list of rules. +3. **Duplicate rule button**: This button will duplicate the selected project rule from the list of rules. +4. **Collapse/Expand arrow**: This arrow will collapse or expand the project rule to hide or show its sub-rules. +5. **Add moving rule button**: This button will add a moving rule to the selected project rule. +6. **Delete moving rule button**: This button will delete the selected moving rule from the project rule. +7. **Duplicate moving rule button**: This button will duplicate the selected moving rule + +On the picture you can see also an expanded project rule with two moving rules inside it. +And below it a collapsed project rule. ## Tag Rules UI diff --git a/main.ts b/main.ts index 21bb23a..f6f6bec 100644 --- a/main.ts +++ b/main.ts @@ -146,6 +146,9 @@ export default class AutoMoverPlugin extends obsidian.Plugin { if (ruleMatcherUtil.isRegexGrouped(tagRule)) { const matches = ruleMatcherUtil.getGroupMatches(file, tagRule); + console.log("File: ", file.path); + console.log("Tag rule: ", tagRule); + console.log("Tag matches: ", matches); const finalDestinationPath = ruleMatcherUtil.constructFinalDesinationPath(tagRule, matches!); movingUtil.moveFile(file, finalDestinationPath); } else { @@ -176,9 +179,9 @@ export default class AutoMoverPlugin extends obsidian.Plugin { // If no rule matches or folder is "./", move to project root if (rule == null || rule.folder === "./") { - console.log("No matching rule or './' destination, moving to project root"); - movingUtil.moveFile(file, projectRule.folder); - return true; + console.log("No matching rule or './' destination, moving to project root"); + movingUtil.moveFile(file, projectRule.folder); + return true; } // console.log("Project rule's moving rule found: ", rule); @@ -221,9 +224,21 @@ export default class AutoMoverPlugin extends obsidian.Plugin { areThereRulesToApply(): boolean { return ( (this.settings.movingRules.length > 0 && - this.settings.movingRules.every((rule) => rule.regex !== "" && rule.folder !== "")) || + this.settings.movingRules.some((rule) => rule.regex !== "" && rule.folder !== "")) || (this.settings.tagRules.length > 0 && - this.settings.tagRules.every((rule) => rule.regex !== "" && rule.folder !== "")) + this.settings.tagRules.some((rule) => rule.regex !== "" && rule.folder !== "")) || + this.areThereProjectRulesToApply() + ); + } + + /** + * If there are no project rules to apply, then there is no point in checking for them + * @returns boolean + */ + areThereProjectRulesToApply(): boolean { + return ( + this.settings.projectRules.length > 0 && + this.settings.projectRules.some((projectRule) => projectRule.projectName !== "" && projectRule.folder !== "") ); }