Repository: platers/obsidian-linter (ts)

Issue:
Bug: `YAML Title Alias` does not escape commas
## Describe the Bug

If an h1 contains commas, the generated aliases array entry is not correctly escaped. 
Obsidian expects an array for the aliases field; thus, any entry without quotes that contains commas is interpreted as multiple entries. Unfortunately, headers with commas are not enquoted, resulting in multiple aliases instead of one.

## How to Reproduce

The following example shows the problem:

``` markdown
---
aliases: [Consistent, Durable, and Safe Memory Management for Byte-addressable Non Volatile Main Memory]
---

# Consistent, Durable, and Safe Memory Management for Byte-addressable Non Volatile Main Memory

Lorem Ipsum...
```

Obsidian parses this as multiple aliases:
- Consistent 
- Durable
- and Safe Memory Management for Byte-addressable Non Volatile Main Memory

This can be seen for example in the open document dialog.

<img width="732" alt="file-open-dialog" src="https://user-images.githubusercontent.com/20145354/218313805-a8154d7f-d602-4708-bcf9-348f36b24236.png">

## Expected Behavior

If a title contains commas, it should be enquoted, exactly like if it contains `:` and their like.

``` markdown
---
aliases: ["Consistent, Durable, and Safe Memory Management for Byte-addressable Non Volatile Main Memory"]
---

# Consistent, Durable, and Safe Memory Management for Byte-addressable Non Volatile Main Memory

Lorem Ipsum...
```

The alias is now correct:

<img width="724" alt="file-open-dialog-exp" src="https://user-images.githubusercontent.com/20145354/218314014-f99bfb21-33cd-45f8-8db8-8070fca5fafb.png">

## Device

- [x] Desktop
- [x] Mobile


Pull request description:
Fix `YAML Title Alias` Not Escaping Commas
Fixes #630 

Changes Made:
- Added tests for the scenarios where headers with a comma should be escaped due to comma delimited aliases being used
- Added a test to make sure multi-line arrays were not getting escaped by the new logic as well
- Updated the logic to force the escaping of a value if the title has a comma and the alias style uses commas to delimit the alias list

Patch:
diff --git a/src/rules/yaml-title-alias.ts b/src/rules/yaml-title-alias.ts
index 138c330..caf2d39 100644
--- a/src/rules/yaml-title-alias.ts
+++ b/src/rules/yaml-title-alias.ts
@@ -53,7 +53,8 @@ export default class YamlTitleAlias extends RuleBuilder<YamlTitleAliasOptions> {
 
     previousTitle = loadYAML(getYamlSectionValue(yaml, LINTER_ALIASES_HELPER_KEY));
 
-    title = escapeStringIfNecessaryAndPossible(title, options.defaultEscapeCharacter);
+    const forceEscape = title.includes(',') && (options.aliasArrayStyle === NormalArrayFormats.SingleLine || options.aliasArrayStyle === SpecialArrayFormats.SingleStringToSingleLine || options.aliasArrayStyle === SpecialArrayFormats.SingleStringCommaDelimited);
+    title = escapeStringIfNecessaryAndPossible(title, options.defaultEscapeCharacter, forceEscape);
     const getNewAliasValue = function(originalValue: string |string[], shouldRemoveTitle: boolean): string |string[] {
       if (originalValue == null) {
         return shouldRemoveTitle ? '' : title;


Test patch:
diff --git a/__tests__/yaml-title-alias.test.ts b/__tests__/yaml-title-alias.test.ts
index 21e7029..0999bc8 100644
--- a/__tests__/yaml-title-alias.test.ts
+++ b/__tests__/yaml-title-alias.test.ts
@@ -1156,5 +1156,85 @@ ruleTest({
         useYamlKeyToKeepTrackOfOldFilenameOrHeading: false,
       },
     },
+    { // accounts for https://github.com/platers/obsidian-linter/issues/630
+      testName: 'Make sure alias is escaped when a comma is present and the array style is single-line',
+      before: dedent`
+        ---
+        aliases: [alias1]
+        ---
+        # Header, with comma
+      `,
+      after: dedent`
+        ---
+        aliases: ["Header, with comma", alias1]
+        ---
+        # Header, with comma
+      `,
+      options: {
+        aliasArrayStyle: NormalArrayFormats.SingleLine,
+        useYamlKeyToKeepTrackOfOldFilenameOrHeading: false,
+      },
+    },
+    { // accounts for https://github.com/platers/obsidian-linter/issues/630
+      testName: 'Make sure alias is escaped when a comma is present and the array style is single string to single-line',
+      before: dedent`
+        ---
+        aliases: alias1
+        ---
+        # Header, with comma
+      `,
+      after: dedent`
+        ---
+        aliases: ["Header, with comma", alias1]
+        ---
+        # Header, with comma
+      `,
+      options: {
+        aliasArrayStyle: SpecialArrayFormats.SingleStringToSingleLine,
+        useYamlKeyToKeepTrackOfOldFilenameOrHeading: false,
+      },
+    },
+    { // accounts for https://github.com/platers/obsidian-linter/issues/630
+      testName: 'Make sure alias is escaped when a comma is present and the array style is a comma delimited string',
+      before: dedent`
+        ---
+        aliases: alias1
+        ---
+        # Header, with comma
+      `,
+      after: dedent`
+        ---
+        aliases: "Header, with comma", alias1
+        ---
+        # Header, with comma
+      `,
+      options: {
+        aliasArrayStyle: SpecialArrayFormats.SingleStringCommaDelimited,
+        useYamlKeyToKeepTrackOfOldFilenameOrHeading: false,
+      },
+    },
+    { // relates to https://github.com/platers/obsidian-linter/issues/630
+      testName: 'Make sure alias is not escaped when a comma is present and the array style is multi-line',
+      before: dedent`
+        ---
+        aliases:
+          - alias1
+        ---
+        # Header, with comma
+      `,
+      after: dedent`
+        ---
+        aliases:
+          - Header, with comma
+          - alias1
+        ---
+        # Header, with comma
+      `,
+      options: {
+        aliasArrayStyle: NormalArrayFormats.MultiLine,
+        useYamlKeyToKeepTrackOfOldFilenameOrHeading: false,
+      },
+    },
+
   ],
 });