Repository: platers/obsidian-linter (ts)

Issue:
Bug: `heading-blank-lines` does not work properly if the line preceding a header has a `#`
- [x] I have verified that I am on the latest version of the Linter

## Describe the Bug
With `heading-blank-lines` on, the linter does not add an empty space above the header if in the line preceding the header contains a hashtag.

## How to Reproduce
Example to reproduce issue with

``` markdown
# Heading 1
Lorem ipsum.

## Heading 2
Woah, cool text here. #reallycool
## Heading 3

```

My bet would be that the cause is with the `heading-blank-lines` rule.

## Expected Behavior

I expect an empty line to be inserted inbetween `Woah, cool text here. #reallycool` and `## Heading 3`

Expected output if applicable:

``` markdown
# Heading 1
Lorem ipsum.

## Heading 2
Woah, cool text here. #reallycool

## Heading 3

```

## Device

- [x] Desktop
- [ ] Mobile

## Additional Context

It didn't seem to matter where the `#` is placed. At the beginning of the sentence, middle of it, at the end, with a tag, without a tag -- the linter doesn't seem to add a line.


Pull request description:
`Heading Blank Lines`: Make Sure Tags on The Line Before a Heading Gets a Blank Line Added when `Bottom=false`
Fixes #993

There was an issue where `Bottom=false` and the line before a heading had a `#` in it.
For example:
``` markdown
# Heading 1
Lorem ipsum.

## Heading 2
Woah, cool text here. #reallycool
## Heading 3
```
would not get changed.

After the change it now looks like:
``` markdown
# Heading 1
Lorem ipsum.

## Heading 2
Woah, cool text here. #reallycool

## Heading 3
```

Changes Made:
- Added a test for the scenario in question
- Updated the regex for when `Bottom=false` to make sure that it only does not allow a line to start with `#` for adding a blank line before a heading

Patch:
diff --git a/src/rules/heading-blank-lines.ts b/src/rules/heading-blank-lines.ts
index 6af47f4..25caa45 100644
--- a/src/rules/heading-blank-lines.ts
+++ b/src/rules/heading-blank-lines.ts
@@ -24,7 +24,7 @@ export default class HeadingBlankLines extends RuleBuilder<HeadingBlankLinesOpti
   }
   apply(text: string, options: HeadingBlankLinesOptions): string {
     if (!options.bottom) {
-      text = text.replace(/^([^#\n]+)\n+(#+\s.*)/gm, '$1\n\n$2');
+      text = text.replace(/^([^#\n][^\n]+)\n+(#+\s.*)/gm, '$1\n\n$2');
     } else {
       text = text.replace(/^(#+\s.*)/gm, '\n\n$1\n\n'); // add blank line before and after headings
       text = text.replace(/\n+(#+\s.*)/g, '\n\n$1'); // trim blank lines before headings


Test patch:
diff --git a/__tests__/heading-blank-lines.test.ts b/__tests__/heading-blank-lines.test.ts
index 02cb90f..31fa38b 100644
--- a/__tests__/heading-blank-lines.test.ts
+++ b/__tests__/heading-blank-lines.test.ts
@@ -180,5 +180,29 @@ ruleTest({
         emptyLineAfterYaml: false,
       },
     },
+    { // accounts for https://github.com/platers/obsidian-linter/issues/993
+      testName: 'Make sure that headings proceeded by a line with a hashtag get a blank line added when `Bottom=false`.',
+      before: dedent`
+        # Heading 1
+        Lorem ipsum.
+        ${''}
+        ## Heading 2
+        Woah, cool text here. #reallycool
+        ## Heading 3
+      `,
+      after: dedent`
+        # Heading 1
+        Lorem ipsum.
+        ${''}
+        ## Heading 2
+        Woah, cool text here. #reallycool
+        ${''}
+        ## Heading 3
+      `,
+      options: {
+        bottom: false,
+        emptyLineAfterYaml: false,
+      },
+    },
   ],
 });