Repository: platers/obsidian-linter (ts)

Issue:
Bug: `Move Tags to YAML` Can Leave Whitespace on the end of the YAML Frontmatter Leadint to YAML Duplication for Running of the Next YAML Rule
Hi,

## Describe the Bug

The "remove to tag" feature from the [Move Tags to YAML](https://github.com/platers/obsidian-linter/blob/master/docs/rules.md#move-tags-to-yaml) section destroy the YAML format when :
- there is a #tag in the first line, first letter of the md document AND there is a space after this tag.
- there is a #tag just after the YAML section, first line and first letter too AND there is a space after this tag.

I have several options in the YAML section enabled, see joined config :
[configObsidianLinter.txt](https://github.com/platers/obsidian-linter/files/10327740/configObsidianLinter.txt)

## How to Reproduce

Before lint :

```
---
some YAML
---
#tag-error 

```

After lint :

```
---
title: test
date: 2022-12-31
edit: 2022-12-31
---
---
title:
aliases:
notetype:
date:
edit:
tags: tag-error
--- 

```

When I disable the "remove to tag" feature, the bug disappear, but it may be a conflict with other parameters.

## Expected Behavior

The YAML should not be duplicated.

## Screenshots

After the lint on Obsidian :
![after-linter](https://user-images.githubusercontent.com/88883564/210151440-ad890d8a-1c72-4b0c-b6bd-9d54dccde3b1.JPG)

## Device

- [x] Desktop (Windows)
- [ ] Mobile (dunno)


Thanks in advance, and thanks for this beautiful plugin !

(hope this issue wasn’t already reported, didn’t see anything with the "tag" search. 🤞)

Pull request:
Remove Whitespace after YAML Frontmatter after Moving Tags to YAML
Fixes #573 

Changes Made:
- Added a UT for the scenario in question
- Removed whitespace from the ending indicator for YAML frontmatter

Patch:
diff --git a/src/rules/move-tags-to-yaml.ts b/src/rules/move-tags-to-yaml.ts
index 4572537..b4eea4a 100644
--- a/src/rules/move-tags-to-yaml.ts
+++ b/src/rules/move-tags-to-yaml.ts
@@ -107,6 +107,10 @@ export default class MoveTagsToYaml extends RuleBuilder<MoveTagsToYamlOptions> {
         });
       }
 
+      // Make sure that the yaml frontmatter does not have whitespace added after the end of the yaml frontmatter.
+      // This accounts for https://github.com/platers/obsidian-linter/issues/573
+      text = text.replace(/(\n---)( |\t)+/, '$1');
+
       return text;
     });
   }


Test patch:
diff --git a/__tests__/move-tags-to-yaml.test.ts b/__tests__/move-tags-to-yaml.test.ts
index 49728fe..76e40a8 100644
--- a/__tests__/move-tags-to-yaml.test.ts
+++ b/__tests__/move-tags-to-yaml.test.ts
@@ -197,5 +197,28 @@ ruleTest({
         tagArrayStyle: TagSpecificArrayFormats.SingleStringSpaceDelimited,
       },
     },
+    { // accounts for https://github.com/platers/obsidian-linter/issues/573
+      testName: 'Make sure that removing a tag after the yaml frontmatter does not leave whitespace on the same line as the frontmatter',
+      before: dedent`
+        ---
+        title: Move Tags to YAML Duplicates YAML
+        date: 2023-01-13
+        edit: 2023-01-231
+        ---
+        #tag-error${' '}
+      `,
+      after: dedent`
+        ---
+        title: Move Tags to YAML Duplicates YAML
+        date: 2023-01-13
+        edit: 2023-01-231
+        tags: tag-error
+        ---
+      `,
+      options: {
+        tagArrayStyle: TagSpecificArrayFormats.SingleStringSpaceDelimited,
+        howToHandleExistingTags: 'Remove whole tag',
+      },
+    },
   ],
 });