Repository: platers/obsidian-linter (ts)

Issue:
Bug: Erroneous Table Match Causes Content Duplication when Table Match Is Not Start of Line
## Describe the Bug

WHEN SAVE AND LINT, DUPLICATES SOME LINE AUTOMATICLY.

## How to Reproduce

Steps to reproduce the behavior:

1. Create a new note
2. Paste `magneto - | maɡˈnedō |` to the editor
3. Save it
4. Will see the content becomes to:
   ```
   magneto

   magneto - | maɡˈnedō |

   magneto
   ```
5. If placed `magneto - | maɡˈnedō |` into code block, will not generate duplicated lines.

Please make sure to include logs and the rule(s) that cause the issue if applicable. You can include logs by going to the debug tab of the Linter and setting the log level to debug. Then enable the capturing of logs and either lint on save or lint the current file. Then go back to the debug tab and you can grab Linter logs from the last run.

If you are not sure which rule(s) may cause the issue, please attach the `data.json` file located at `<vault>/.obsidian/plugins/obsidian-linter/` or copy the config from the debug tab.

[data.json.zip](https://github.com/platers/obsidian-linter/files/10365663/data.json.zip)

## Expected Behavior

DO NOT DUPLICATE THE CONTENT.

## Screenshots

![obsidian-linter-bug](https://user-images.githubusercontent.com/275233/211135422-b9c83069-1836-4b2f-85a5-61406005477f.gif)

## Device

- [x] Desktop

## Additional Context

macOS 13.1
Obsidian: 1.1.9
Obsidian Linter: 1.10.1



Pull request:
Make Sure Table Regex Only Matches Content Starting a Line
Fixes #577 

Changes Made:
- Added a UT for the scenario in question
- Made it so the table regex only matches on things that start a line

Patch:
diff --git a/src/utils/regex.ts b/src/utils/regex.ts
index e35900f..23664d9 100644
--- a/src/utils/regex.ts
+++ b/src/utils/regex.ts
@@ -20,7 +20,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]*?)*)+/g;
+export const tableRegex = /^((((>[ ]?)*)|([ ]{0,3}))\[.*?\][ \t]*\n)?((((>[ ]?)*)|([ ]{0,3}))\S+.*?\|.*?\n([^\n]*?\|[^\n]*?\n)*?)?(((>[ ]?)*)|([ ]{0,3}))[|\-+:.][ \-+|:.]*?\|[ \-+|:.]*(?:\n?[^\n]*?\|([^\n]*?)*)+/gm;
 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


Test patch:
diff --git a/__tests__/empty-line-around-tables.test.ts b/__tests__/empty-line-around-tables.test.ts
index cedcb32..ced0a50 100644
--- a/__tests__/empty-line-around-tables.test.ts
+++ b/__tests__/empty-line-around-tables.test.ts
@@ -108,7 +108,18 @@ ruleTest({
         - $math2$
       `,
     },
-
-
+    { // accounts for https://github.com/platers/obsidian-linter/issues/577
+      testName: 'Make sure that we prevent some erroneous table matches where the value is not at the start of a line',
+      before: dedent`
+        content
+        magneto - | maɡˈnedō |
+        content
+      `,
+      after: dedent`
+        content
+        magneto - | maɡˈnedō |
+        content
+      `,
+    },
   ],
 });