Repository: platers/obsidian-linter (ts)

Issue:
Incorrect `file-name-heading` behaviour
Problem: linting inserts a file title incorrectly.

Steps to reproduce:

1. Create a note with the following content

    ```
    Text 1
    
    ---
    
    Text 2
    
    ```

2. Apply `file-name-heading`  linting rule
3. Expected result

    ```
    # Title
    
    Text 1
    
    ---
    
    Text 2
    
    ```

4. Actual (incorrect) result

    ```
    
    Text 1
    
    ---
    # Title
    
    Text 2
    
    ```

I believe this case should be added as an exception for documents with no YAML variables.

Pull request:
fix yaml detection in file name heading
Fixes #47 

Patch:
diff --git a/src/rules.ts b/src/rules.ts
index 4d5a521..27fb900 100644
--- a/src/rules.ts
+++ b/src/rules.ts
@@ -474,7 +474,7 @@ export const rules: Rule[] = [
         const fileName = options['metadata: file name'];
         // insert H1 heading after front matter
         let yaml_end = text.indexOf('\n---');
-        yaml_end = yaml_end == -1 ? 0 : yaml_end + 5;
+        yaml_end = yaml_end == -1 || !text.startsWith('---\n') ? 0 : yaml_end + 5;
         return insert(text, yaml_end, `# ${fileName}\n`);
       },
       [


Test patch:
diff --git a/src/test.ts b/src/test.ts
index c3b0051..573f02a 100644
--- a/src/test.ts
+++ b/src/test.ts
@@ -131,6 +131,26 @@ describe('Rules tests', () => {
       expect(rulesDict['capitalize-headings'].apply(before, {titleCase: 'true'})).toBe(after);
     });
   });
+  describe('File Name Heading', () => {
+    it('Handles stray dashes', () => {
+      const before = dedent`
+      Text 1
+
+      ---
+      
+      Text 2
+        `;
+      const after = dedent`
+      # File Name
+      Text 1
+      
+      ---
+      
+      Text 2
+        `;
+      expect(rulesDict['file-name-heading'].apply(before, {'metadata: file name': 'File Name'})).toBe(after);
+    });
+  });
 });
 
 describe('Option parsing', () => {