Repository: platers/obsidian-linter (ts)

Issue:
Bug: YAML Key Sort Does Not Work when There is Trailing Whitespace after a Colon in Config Values
- [x] I have verified that I am on the latest version of the Linter

## Describe the Bug

I put in the keys, i run the lint, orders do change, but not as they should

## How to Reproduce

Steps to reproduce the behavior:

1.  Have existing frontmatter of
---
pr-type: 
pr-urgency: 
pr-okr: 
pr-completed-date: 
related-companies: 
pr-priority: 
modified: 2024-10-21 09:58
stakeholders: 
pr-pipeline-stage: 
pr-size: 
template: "[[Pro New Project Outcome Template]]"
pr-due-date: 
created-date: '[[<% tp.file.creation_date("YYYY-MM-DD") %>]]'
---


2. run lint on current file with settings

related-companies: 
stakeholders: 
pr-priority:
pr-pipeline-stage:
pr-size:
pr-type: 
pr-okr: 
pr-due-date:
pr-urgency:
pr-completed-date: 
template: 
created-date:
modified:


3. give this result

---
pr-type: 
pr-okr: 
pr-completed-date: 
related-companies: 
stakeholders: 
template: "[[Pro New Project Outcome Template]]"
pr-priority: 
pr-pipeline-stage: 
pr-size: 
pr-due-date: 
pr-urgency: 
created-date: '[[<% tp.file.creation_date("YYYY-MM-DD") %>]]'
modified: 2024-10-21 09:58
---



Running linter
rules before regular rules: 0.09999990463256836 ms
Running Custom Regex
custom regex rules: 0.10000002384185791 ms
Running YAML Key Sort
yaml-key-sort: 1.100000023841858 ms
---
pr-type: 
pr-okr: 
pr-completed-date: 
related-companies: 
stakeholders: 
template: "[[Pro New Project Outcome Template]]"
pr-priority: 
pr-pipeline-stage: 
pr-size: 
pr-due-date: 
pr-urgency: 
created-date: '[[<% tp.file.creation_date("YYYY-MM-DD") %>]]'
modified: 2024-10-21 09:58
---
<% await tp.file.move("/CRM/Projects/" + tp.file.title) %>

## Navigation
![[🪧 Navigation Template]]

## Related discussions
```dataview
TABLE WITHOUT ID
    file.link AS "Meeting",
    interaction-date AS "Interaction Date",
    companies AS "Companies",
    attendees AS "Attendees"
FROM "CRM/Meetings"
WHERE contains(related-projects, this.file.link)
SORT file.link DESC
```

[[Project Prioritisation Matrix]]

|Project Name|Urgency (U)|Importance (I)|Size (S)|Priority Score|
|---|---|---|---|---|
|Project A|||||
|Project B|||||
|Project C|||||


## Notes


## Reference Links
- [[Kanban - My Projects]]
- 





rules after regular rules: 2 ms
rules running: 2.399999976158142 ms




## Device

- [ ] Desktop
- [ ] Mobile




Pull request:
Fix `YAML Key Sort` Not Accounting for Trailing Whitespace on Keys
Fixes #1202 

There was an issue where Priority Sort would not work if there was a space after the colon assuming there was a colon. So the values are now getting trimmed.

Changes Made:
- Added a test for the scenario provided
- Removed trailing whitespace on key names

Patch:
diff --git a/src/rules/yaml-key-sort.ts b/src/rules/yaml-key-sort.ts
index 7c7a9c4..345e0bf 100644
--- a/src/rules/yaml-key-sort.ts
+++ b/src/rules/yaml-key-sort.ts
@@ -45,9 +45,12 @@ export default class YamlKeySort extends RuleBuilder<YamlKeySortOptions> {
 
     const yamlKeys: string[] = options.yamlKeyPrioritySortOrder;
     let index = 0;
-    for (const key of yamlKeys) {
+    for (let key of yamlKeys) {
+      key = key.trimEnd();
       if (key.endsWith(':')) {
         yamlKeys[index] = key.substring(0, key.length - 1);
+      } else if (key != yamlKeys[index]) {
+        yamlKeys[index] = key;
       }
 
       index++;


Test patch:
diff --git a/__tests__/yaml-key-sort.test.ts b/__tests__/yaml-key-sort.test.ts
index dcb90f4..7a75001 100644
--- a/__tests__/yaml-key-sort.test.ts
+++ b/__tests__/yaml-key-sort.test.ts
@@ -143,5 +143,61 @@ ruleTest({
         ],
       },
     },
+    { // accounts for https://github.com/platers/obsidian-linter/issues/1202
+      testName: 'Make sure that settings that end in whitespace have them trimmed so they are properly recognized',
+      before: dedent`
+        ---
+        stakeholders:${' '}
+        pr-type:${' '}
+        date-created: Thursday, August 22nd 2024, 6:41:04 pm
+        pr-pipeline-stage:${' '}
+        pr-priority:${' '}
+        modified: Tuesday, October 22nd 2024, 10:58:16 am
+        pr-size:${' '}
+        pr-urgency:${' '}
+        pr-okr:${' '}
+        pr-due-date:${' '}
+        pr-completed-date:${' '}
+        template: "[[Pro New Project Outcome Template]]"
+        related-companies:${' '}
+        created-date: '[[<% tp.file.creation_date("YYYY-MM-DD") %>]]'
+        ---
+      `,
+      after: dedent`
+        ---
+        related-companies:${' '}
+        stakeholders:${' '}
+        pr-pipeline-stage:${' '}
+        pr-priority:${' '}
+        pr-size:${' '}
+        pr-urgency:${' '}
+        pr-type:${' '}
+        pr-okr:${' '}
+        pr-due-date:${' '}
+        pr-completed-date:${' '}
+        template: "[[Pro New Project Outcome Template]]"
+        created-date: '[[<% tp.file.creation_date("YYYY-MM-DD") %>]]'
+        modified: Tuesday, October 22nd 2024, 10:58:16 am
+        date-created: Thursday, August 22nd 2024, 6:41:04 pm
+        ---
+      `,
+      options: {
+        yamlKeyPrioritySortOrder: [
+          'related-companies: ',
+          'stakeholders: ',
+          'pr-pipeline-stage:',
+          'pr-priority:',
+          'pr-size:',
+          'pr-urgency:',
+          'pr-type: ',
+          'pr-okr:',
+          'pr-due-date:',
+          'pr-completed-date: ',
+          'template:',
+          'created-date:',
+          'modified:',
+        ],
+      },
+    },
   ],
 });