bugfix: work on tag regex and docs

This commit is contained in:
Al0cam 2025-11-10 07:59:19 +01:00
parent bdfe7d4549
commit fa556ce751
6 changed files with 73 additions and 7 deletions

View file

@ -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

View file

@ -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) {

View file

@ -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

28
docs/tag-rules.md Normal file
View file

@ -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.

View file

@ -69,8 +69,15 @@ Each project rule can be collapsed individually to hide its sub-rules, and the e
<img width="1531" height="611" alt="image" src="https://github.com/user-attachments/assets/c74d13de-6a2f-48f7-bc48-3e25f0f607e9" />
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

25
main.ts
View file

@ -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 !== "")
);
}