Repository: platers/obsidian-linter (ts)

Issue:
Bug: Move Tags to Yaml not respecting the "Tags to ignore" list
## Describe the Bug
The Move Tags to Yaml option does not respect the "Tags to ignore" list.

## How to Reproduce

### Settings
Turn on Move all tags to Yaml front matter of the document. 

Turn "Body tag operation" to "Remove whole tag" option

### Testing
Open markdown file and test:

``` markdown

#tag1
#tag2
#ignored-tag test data
#ignored-tag/nested-tag more tested data

```

Result:
``` markdown
---
tags: tag1 tag2
---

test data
more tested data
```

## Expected Behavior
Expected output:

``` markdown
---
tags: tag1 tag2
---

#ignored-tag test data
#ignored-tag/nested-tag more tested data
```
## Device

- [x] Desktop - macOS Ventura 13.0.1
- [x] Mobile - iOS 16.1.1

Running Linter 1.8.0

## Additional Context

If the "Remove hashtag" option is used instead, it performs as expected. The ignored tags remain hashtagged, and the expected tags that should have the hashtag removed are so.


If you need any more information, please let me know! 

Thanks!


Pull request description:
Fix Issue with Removing Tags In Move Tags to YAML Where Ignored Tags Were Removed
Fixes #521 

Changes Made:
- Updated logic to always use the custom regex replace method and to just return an empty string when not removing the hashtag when an actual action is suggested.
- Added a UT to make sure we don't hit this problem in the future

Patch:
diff --git a/src/rules/move-tags-to-yaml.ts b/src/rules/move-tags-to-yaml.ts
index c99e12d..b909600 100644
--- a/src/rules/move-tags-to-yaml.ts
+++ b/src/rules/move-tags-to-yaml.ts
@@ -90,7 +90,7 @@ export default class MoveTagsToYaml extends RuleBuilder<MoveTagsToYamlOptions> {
         return `---\n${newYaml}---`;
       });
 
-      if (options.howToHandleExistingTags === 'Remove hashtag') {
+      if (options.howToHandleExistingTags !== 'Nothing') {
         text = text.replace(tagRegex, (tag: string) => {
           const hashtagIndex = tag.indexOf('#');
 
@@ -99,10 +99,12 @@ export default class MoveTagsToYaml extends RuleBuilder<MoveTagsToYamlOptions> {
             return tag;
           }
 
-          return tag.substring(0, hashtagIndex) + tagContents;
+          if (options.howToHandleExistingTags === 'Remove hashtag') {
+            return tag.substring(0, hashtagIndex) + tagContents;
+          }
+
+          return '';
         });
-      } else if (options.howToHandleExistingTags === 'Remove whole tag') {
-        text = text.replace(tagRegex, '');
       }
 
       return text;


Test patch:
diff --git a/__tests__/move-tags-to-yaml.test.ts b/__tests__/move-tags-to-yaml.test.ts
index 44ea611..6461b24 100644
--- a/__tests__/move-tags-to-yaml.test.ts
+++ b/__tests__/move-tags-to-yaml.test.ts
@@ -1,5 +1,5 @@
 import MoveTagsToYaml from '../src/rules/move-tags-to-yaml';
-import {NormalArrayFormats, SpecialArrayFormats} from '../src/utils/yaml';
+import {NormalArrayFormats, SpecialArrayFormats, TagSpecificArrayFormats} from '../src/utils/yaml';
 import dedent from 'ts-dedent';
 import {ruleTest} from './common';
 
@@ -162,7 +162,26 @@ ruleTest({
         <mark style="background: #FFB8EBA6;">some text</mark>
       `,
     },
-
-
+    { // accounts for https://github.com/platers/obsidian-linter/issues/521
+      testName: 'Make sure that removing tags respects ignore list',
+      before: dedent`
+        #tag1
+        #tag2
+        #ignored-tag test data
+        #ignored-tag/nested-tag more tested data
+      `,
+      after: dedent`
+        ---
+        tags: tag1 tag2
+        ---
+        #ignored-tag test data
+        #ignored-tag/nested-tag more tested data
+      `,
+      options: {
+        tagArrayStyle: TagSpecificArrayFormats.SingleStringSpaceDelimited,
+        howToHandleExistingTags: 'Remove whole tag',
+        tagsToIgnore: ['ignored-tag', 'ignored-tag/nested-tag'],
+      },
+    },
   ],
 });