Repository: platers/obsidian-linter (ts)

Issue:
Bug:  Repeated empty lines
## Describe the Bug

There are two blank lines at the end of the table.

## How to Reproduce

Use linter 
1. When I use linter v1.8.0 formatting, I get two blank rows between the table and title

``` markdown
### 常量

|  `[[Link]]`         |  A link to the file named "Link"  |
|:--------------------|:----------------------------------|
|  `[[Link]]`         |  A link to the file named "Link"  |
|  `[1, 2, 3]`        |  A list of numbers 1, 2, and 3    |
|  `[[1, 2],[3, 4]]`  |  A list of lists                  |
|  `{ a: 1, b: 2 }`   |  An object                        |
|  `date()`           |                                   |
|  `dur()`            |                                   |

### 表达式

```

## Expected Behavior

There should be only one blank line between the table and the other blocks.

## Screenshots

After formatting, two spacing lines are added:
![image](https://user-images.githubusercontent.com/6396748/204182046-4b459cf4-752f-4096-b33c-eca0f1618ad5.png)

Linter settings:
![image](https://user-images.githubusercontent.com/6396748/204181828-99e8a7e8-79aa-4048-8758-9fceff1e23ad.png)
![image](https://user-images.githubusercontent.com/6396748/204181872-f48084e6-7424-4fdf-94bd-29575ad4a6f6.png)
![image](https://user-images.githubusercontent.com/6396748/204181898-e48532b1-5dd4-496a-a285-6a050daac9a9.png)
![image](https://user-images.githubusercontent.com/6396748/204181908-b2084902-cb67-4ed3-8093-9886061a47e8.png)


## Device

- [x] Desktop


Bug: [Consecutive Blank Lines]: Whitespace in consecutive empty lines results in them not being removed upon linting 
## Describe the Bug


The rule [Consecutive Blank lines](https://github.com/platers/obsidian-linter/blob/master/docs/rules.md#consecutive-blank-lines) is supposed to condense any number of blank lines into one. "Blank" is an arbitrary word in this case, because - at least to me - it suggests that lines containing solely whitespace (spaces, tabs, etc) should be treated as empty - after all, they don't contain printable characters.
## How to Reproduce

Steps to reproduce the behavior:
1. Create a note
2. Paste the contents of this codeblock
``` markdown
A

B
				 

the above "emtpy" lines **are not** merged into one line because the upper one contains spaces (but no printable characters)



the above empty lines _do_ get reduced to one line because they are truely empty, aka don't contain whitespace

```
3. run the linter with the above-mentioned setting activated.

## Expected Behavior

I'd expect that a line containing pure whitespace is treated as an empty line (and is considered for the purpose of the rule). This means that any number of lines (≥2) that contain either 

1. no characters at all, or 
2. any combination of whitespace characters, e.g. tabs, spaces, newlines, line feeds, carriage returns, ...

are to be considered empty, and therefore removed by the linter. 
Expected output if applicable:

``` markdown
A

B

the above "emtpy" lines are not merged into one line because the upper one contains spaces (but no printable characters)

the above empty lines **do get reduced** to one line because they are truely empty, aka don't contain whitespace

```
## Actual output:

``` markdown
A

B
				 

the above "emtpy" lines are not merged into one line because the upper one contains spaces (but no printable characters)

the above empty lines _do_ get reduced to one line because they are truely empty, aka don't contain whitespace
```

## Screenshots

/

## Device

- [x] Desktop
- [ ] ~~Mobile~~, not checked on mobile

## Additional Context


Version/Installer Version: 1.0.3  
Plugin Version: 1.8.0

Pull request:
Fix 3 Bugs and Add a New NPM Command
Fixes #529 
Fixes #517 
Relates to #516 

Changes Made:
- Updated consecutive blank lines to consider a line to be empty if it is solely whitespace
- Added UTs to test out several new scenarios for whitespace removal
- Added a command to allow for the removal of the jest cache to better help when renaming rules or when the jest cache causes UTs to fail
- Added ability to ignore footnotes after a task for moving footnote to after punctuation
- Updated table regex to make sure it does not end with a new line character in order to help fix some issues with new lines around it

Patch:
diff --git a/package.json b/package.json
index 4f4dde2..bc767f8 100644
--- a/package.json
+++ b/package.json
@@ -10,7 +10,8 @@
     "test-suite": "jest -t \"$1\"",
     "docs": "node docs.js",
     "compile": "npm run build && npm run docs && npm run lint && npm run test",
-    "lint": "eslint . --ext .ts --fix"
+    "lint": "eslint . --ext .ts --fix",
+    "clear-jest": "jest --clearCache"
   },
   "keywords": [],
   "author": "",
diff --git a/src/rules/consecutive-blank-lines.ts b/src/rules/consecutive-blank-lines.ts
index af8dd5b..c7d68fc 100644
--- a/src/rules/consecutive-blank-lines.ts
+++ b/src/rules/consecutive-blank-lines.ts
@@ -22,7 +22,8 @@ export default class ConsecutiveBlankLines extends RuleBuilder<ConsecutiveBlankL
   }
   apply(text: string, options: ConsecutiveBlankLinesOptions): string {
     return ignoreListOfTypes([IgnoreTypes.code, IgnoreTypes.yaml, IgnoreTypes.link, IgnoreTypes.wikiLink, IgnoreTypes.tag], text, (text) => {
-      return text.replace(/\n{2,}/g, '\n\n');
+      // make sure to account for lines that are purely whitespace as well https://stackoverflow.com/a/3873354/8353749
+      return text.replace(/(\n([\t\v\f\r \u00a0\u2000-\u200b\u2028-\u2029\u3000]+)?){2,}/g, '\n\n');
     });
   }
   get exampleBuilders(): ExampleBuilder<ConsecutiveBlankLinesOptions>[] {
diff --git a/src/rules/footnote-after-punctuation.ts b/src/rules/footnote-after-punctuation.ts
index fb3e9cb..9e1d455 100644
--- a/src/rules/footnote-after-punctuation.ts
+++ b/src/rules/footnote-after-punctuation.ts
@@ -21,10 +21,8 @@ export default class FootnoteAfterPunctuation extends RuleBuilder<FootnoteAfterP
     return RuleType.FOOTNOTE;
   }
   apply(text: string, options: FootnoteAfterPunctuationOptions): string {
-    return ignoreListOfTypes([IgnoreTypes.code, IgnoreTypes.yaml, IgnoreTypes.link, IgnoreTypes.wikiLink, IgnoreTypes.tag], text, (text) => {
-    // regex uses hack to treat lookahead as lookaround https://stackoverflow.com/a/43232659
-    // needed to ensure that no footnote text followed by ":" is matched
-      return text.replace(/(?!^)(\[\^\w+\]) ?([,.;!:?])/gm, '$2$1');
+    return ignoreListOfTypes([IgnoreTypes.code, IgnoreTypes.yaml, IgnoreTypes.link, IgnoreTypes.wikiLink, IgnoreTypes.tag, IgnoreTypes.footnoteAtStartOfLine, IgnoreTypes.footnoteAfterATask], text, (text) => {
+      return text.replace(/(\[\^\w+\]) ?([,.;!:?])/gm, '$2$1');
     });
   }
   get exampleBuilders(): ExampleBuilder<FootnoteAfterPunctuationOptions>[] {
@@ -38,6 +36,19 @@ export default class FootnoteAfterPunctuation extends RuleBuilder<FootnoteAfterP
           Lorem.[^1] Ipsum,[^2] doletes.
         `,
       }),
+      new ExampleBuilder({
+        description: 'A footnote at the start of a task is not moved to after the punctuation',
+        before: dedent`
+          - [ ] [^1]: This is a footnote and a task.
+          - [ ] This is a footnote and a task that gets swapped with the punctuation[^2]!
+          [^2]: This footnote got modified
+        `,
+        after: dedent`
+          - [ ] [^1]: This is a footnote and a task.
+          - [ ] This is a footnote and a task that gets swapped with the punctuation![^2]
+          [^2]: This footnote got modified
+        `,
+      }),
     ];
   }
   get optionBuilders(): OptionBuilderBase<FootnoteAfterPunctuationOptions>[] {
diff --git a/src/utils/ignore-types.ts b/src/utils/ignore-types.ts
index 0f43190..67c83c4 100644
--- a/src/utils/ignore-types.ts
+++ b/src/utils/ignore-types.ts
@@ -27,6 +27,8 @@ export const IgnoreTypes: Record<string, IgnoreType> = {
   tag: {replaceAction: tagRegex, placeholder: '#tag-placeholder'},
   obsidianMultiLineComments: {replaceAction: obsidianMultilineCommentRegex, placeholder: '{OBSIDIAN_COMMENT_PLACEHOLDER}'},
   table: {replaceAction: tableRegex, placeholder: '{TABLE_PLACEHOLDER}'},
+  footnoteAtStartOfLine: {replaceAction: /^(\[\^\w+\]) ?([,.;!:?])/gm, placeholder: '{FOOTNOTE_AT_START_OF_LINE_PLACEHOLDER}'},
+  footnoteAfterATask: {replaceAction: /- \[.] (\[\^\w+\]) ?([,.;!:?])/gm, placeholder: '{FOOTNOTE_AFTER_A_TASK_PLACEHOLDER}'},
   // custom functions
   link: {replaceAction: replaceMarkdownLinks, placeholder: '{REGULAR_LINK_PLACEHOLDER}'},
 } as const;
diff --git a/src/utils/regex.ts b/src/utils/regex.ts
index 22646f4..fecfa08 100644
--- a/src/utils/regex.ts
+++ b/src/utils/regex.ts
@@ -21,7 +21,7 @@ export const ellipsisRegex = /(\. ?){2}\./g;
 export const lineStartingWithWhitespaceOrBlockquoteTemplate = `\\s*(>\\s*)*`;
 // Note that the following regex has an issue where if the table is followed by another table with only 1 blank line between them, it considers them to be one table
 // if this becomes an issue, we can address it then
-export const tableRegex = /((((>[ ]?)*)|([ ]{0,3}))\[.*?\][ \t]*\n)?((((>[ ]?)*)|([ ]{0,3}))\S+.*?\|.*?\n([^\n]*?\|[^\n]*?\n)*?)?(((>[ ]?)*)|([ ]{0,3}))[|\-+:.][ \-+|:.]*?\|[ \-+|:.]*(?:\n?[^\n]*?\|([^\n]*?)*(\n)?)+/g;
+export const tableRegex = /((((>[ ]?)*)|([ ]{0,3}))\[.*?\][ \t]*\n)?((((>[ ]?)*)|([ ]{0,3}))\S+.*?\|.*?\n([^\n]*?\|[^\n]*?\n)*?)?(((>[ ]?)*)|([ ]{0,3}))[|\-+:.][ \-+|:.]*?\|[ \-+|:.]*(?:\n?[^\n]*?\|([^\n]*?)*)+/g;
 export const urlRegex = /(https?:\/\/(?:www\.|(?!www))[a-zA-Z0-9][a-zA-Z0-9-]+[a-zA-Z0-9]\.[^\s`\]'"‘’“”>]{2,}|www\.[a-zA-Z0-9][a-zA-Z0-9-]+[a-zA-Z0-9]\.[^\s`\]'"‘’“”>]{2,}|https?:\/\/(?:www\.|(?!www))[a-zA-Z0-9]+\.[^\s`\]'"‘’“”>]{2,}|www\.[a-zA-Z0-9]+\.[^\s`\]'"‘’“”>]{2,})/gi;
 
 // https://stackoverflow.com/questions/38866071/javascript-replace-method-dollar-signs
@@ -70,7 +70,7 @@ export function ensureEmptyLinesAroundTables(text: string): string {
 
   for (const table of tableMatches) {
     let start = text.indexOf(table);
-    const end = start + table.trimEnd().length;
+    const end = start + table.length;
     if (table.trim().startsWith('>')) {
       while (text.charAt(start).trim() === '' || text.charAt(start) === '>') {
         start++;


Test patch:
diff --git a/__tests__/consecutive-blank-lines.test.ts b/__tests__/consecutive-blank-lines.test.ts
index 1f86605..3caa9bc 100644
--- a/__tests__/consecutive-blank-lines.test.ts
+++ b/__tests__/consecutive-blank-lines.test.ts
@@ -27,5 +27,104 @@ ruleTest({
         \`\`\`
       `,
     },
+    { // accounts for https://github.com/platers/obsidian-linter/issues/529
+      testName: 'Removes multiple empty lines when one of the lines just has spaces in it',
+      before: dedent`
+        Line 1
+        ${''}
+        ${'  '} 
+        Line 2
+      `,
+      after: dedent`
+        Line 1
+        ${''}
+        Line 2
+      `,
+    },
+    { // accounts for https://github.com/platers/obsidian-linter/issues/529
+      testName: 'Removes multiple empty lines when one of the lines just has a tab in it',
+      before: dedent`
+        Line 1
+        ${''}
+        ${''}\t
+        Line 2
+      `,
+      after: dedent`
+        Line 1
+        ${''}
+        Line 2
+      `,
+    },
+    { // accounts for https://github.com/platers/obsidian-linter/issues/529
+      testName: 'Removes multiple empty lines when one of the lines just has a carriage return in it',
+      before: dedent`
+        Line 1
+        ${''}
+        ${''}\r
+        ${''}\v
+        Line 2
+      `,
+      after: dedent`
+        Line 1
+        ${''}
+        Line 2
+      `,
+    },
+    { // accounts for https://github.com/platers/obsidian-linter/issues/529
+      testName: 'Removes multiple empty lines when one of the lines just has a vertical tab in it',
+      before: dedent`
+        Line 1
+        ${''}
+        ${''}\v
+        Line 2
+      `,
+      after: dedent`
+        Line 1
+        ${''}
+        Line 2
+      `,
+    },
+    { // accounts for https://github.com/platers/obsidian-linter/issues/529
+      testName: 'Removes multiple empty lines when one of the lines just has a page break in it',
+      before: dedent`
+        Line 1
+        ${''}
+        ${''}\f
+        Line 2
+      `,
+      after: dedent`
+        Line 1
+        ${''}
+        Line 2
+      `,
+    },
+    { // accounts for https://github.com/platers/obsidian-linter/issues/529
+      testName: 'Removes multiple empty lines when one of the lines just has a page break in it',
+      before: dedent`
+        Line 1
+        ${''}
+        ${''}\f
+        Line 2
+      `,
+      after: dedent`
+        Line 1
+        ${''}
+        Line 2
+      `,
+    },
+    { // accounts for https://github.com/platers/obsidian-linter/issues/529
+      testName: 'Removes multiple empty lines when one of the lines just has multiple kinds of whitespace in it in it',
+      before: dedent`
+        Line 1
+        ${''}
+        ${''}\f \r\v\t\t\v
+        Line 2
+      `,
+      after: dedent`
+        Line 1
+        ${''}
+        Line 2
+      `,
+    },
   ],
 });
diff --git a/__tests__/paragraph-blank-lines.test.ts b/__tests__/paragraph-blank-lines.test.ts
index b4644e9..368b1f4 100644
--- a/__tests__/paragraph-blank-lines.test.ts
+++ b/__tests__/paragraph-blank-lines.test.ts
@@ -265,5 +265,36 @@ ruleTest({
         %%
       `,
     },
+    { // accounts for https://github.com/platers/obsidian-linter/issues/517
+      testName: 'Table followed by header should only have 1 line after it',
+      before: dedent`
+        ### 常量
+
+        |  \`[[Link]]\`         |  A link to the file named "Link"  |
+        |:--------------------|:----------------------------------|
+        |  \`[[Link]]\`         |  A link to the file named "Link"  |
+        |  \`[1, 2, 3]\`        |  A list of numbers 1, 2, and 3    |
+        |  \`[[1, 2],[3, 4]]\`  |  A list of lists                  |
+        |  \`{ a: 1, b: 2 }\`   |  An object                        |
+        |  \`date()\`           |                                   |
+        |  \`dur()\`            |                                   |
+
+        ### 表达式
+      `,
+      after: dedent`
+        ### 常量
+
+        |  \`[[Link]]\`         |  A link to the file named "Link"  |
+        |:--------------------|:----------------------------------|
+        |  \`[[Link]]\`         |  A link to the file named "Link"  |
+        |  \`[1, 2, 3]\`        |  A list of numbers 1, 2, and 3    |
+        |  \`[[1, 2],[3, 4]]\`  |  A list of lists                  |
+        |  \`{ a: 1, b: 2 }\`   |  An object                        |
+        |  \`date()\`           |                                   |
+        |  \`dur()\`            |                                   |
+
+        ### 表达式
+      `,
+    },
   ],
 });