Repository: platers/obsidian-linter (ts)

Issue:
Bug: YAML Key Sort Removes Multiline Dictionaries
## Describe the Bug

It looks like, when YAML Key Sort is on and set to sort keys alphabetically, any multiline dictionaries in the metadata will have all their keys deleted. This issue does not occur when key sorting is off, or when the "YAML Sort Order for Other Keys" option is set to "None".

## How to Reproduce

Steps to reproduce the behavior:

1. Make sure "YAML Key Sort" is on in Linter settings, with "YAML Sort Order for Other Keys" set to either Ascending Alphabetical or Descending Alphabetical.
2. Add a multiline dictionary to the YAML front matter (see below for example).
3. Lint file.

Or

Example to reproduce issue with

``` markdown
---
test_key: "Hello"
dictionary:
  abc: "abc"
  def: "def"
tags: 
  - Test
---
```

Upon linting, the front matter turns into this:

```markdown
---
dictionary: 
tags:
  - Test
test_key: "Hello"
---
```

## Expected Behavior

I expected the keys to be sorted in alphabetical order, without removing the dictionary's keys.

Expected output if applicable:

``` markdown
---
dictionary: 
  abc: "abc"
  def: "def"
tags:
  - Test
test_key: "Hello"
---
```

## Device

- [x] Desktop
- [ ] Mobile

## Additional Context

I tried uninstalling and reinstalling the plugin to see if that might fix things, but it didn't seem to have an effect.

It also looks like linting the file while the keys are already in alphabetical order will still delete any dictionary keys. Single-line dictionaries don't seem to be affected.


Pull request description:
Allow for Dictionary Value Retrieval for Yaml Key Sort
Fixes #415 

Changes Made:
- Updated the regex for getting yaml to allow for getting dictionary values back (hopefully other nesting levels as well)
- Added a UT for the reported scenario

Patch:
diff --git a/src/utils/yaml.ts b/src/utils/yaml.ts
index b7c4405..5e97d64 100644
--- a/src/utils/yaml.ts
+++ b/src/utils/yaml.ts
@@ -38,7 +38,7 @@ export function toSingleLineArrayYamlString<T>(arr: T[]): string {
 }
 
 function getYamlSectionRegExp(rawKey: string): RegExp {
-  return new RegExp(`${rawKey}:[ \\t]*(\\S.*|(?:(?:\\n *- \\S.*)|((?:\\n *- *))*)*)\\n`);
+  return new RegExp(`${rawKey}:[ \\t]*(\\S.*|(?:(?:\\n *- \\S.*)|((?:\\n *- *))*|(\\n([ \\t]+[^\\n]*))*)*)\\n`);
 }
 
 export function setYamlSection(yaml: string, rawKey: string, rawValue: string): string {


Test patch:
diff --git a/__tests__/yaml-key-sort.test.ts b/__tests__/yaml-key-sort.test.ts
index 6d941f8..dfea56a 100644
--- a/__tests__/yaml-key-sort.test.ts
+++ b/__tests__/yaml-key-sort.test.ts
@@ -40,5 +40,31 @@ ruleTest({
         yamlTimestampDateModifiedEnabled: true,
       },
     },
+    { // accounts for https://github.com/platers/obsidian-linter/issues/415
+      testName: 'Sort works with dictionary Yaml value',
+      before: dedent`
+        ---
+        test_key: "Hello"
+        dictionary:
+          abc: "abc"
+          def: "def"
+        tags: 
+          - Test
+        ---
+      `,
+      after: dedent`
+        ---
+        dictionary:
+          abc: "abc"
+          def: "def"
+        tags:
+          - Test
+        test_key: "Hello"
+        ---
+      `,
+      options: {
+        yamlSortOrderForOtherKeys: 'Ascending Alphabetical',
+      },
+    },
   ],
 });