Repository: platers/obsidian-linter (ts)

Issue:
Bug: Re-Index footnotes error while using link format(e.g., `[text](url)` )
- [x] I have verified that I am on the latest version of the Linter.

## Describe the Bug

While using **Link** in footnotes, the re-index footnotes can't work correctly and mess up all the orders. It caused the key and value of footnotes to be misaligned.

**Tips**: The **Link** I mentioned is something like `[Google](www.google.com)`.
## How to Reproduce

**Settings**: To ensure reproducibility, I have created a brand new vault to reproduce the Bug. Specifically, 

- I use the *latest* Obsidian(v1.5.3).
- I *only* installed the *latest* version of **Linter**(1.22.0) *without modifying any other settings or installing any other plugins*.
- For **Linter**, I *only* opened all 3 features associated with *Footnotes*(`Foot after punctuation`, `Move Footnotes to the buttons`, `Re-Index Footnotes`) as well as `Lint on save`, *keeping other settings as default*.
- And I make a simple example to reproduce issue. The original content is:

``` markdown
Paragraph 1. [^4]

Paragraph 2. [^1]

Paragraph 3. [^2]

Paragraph 4. [^3]

[^1]: [1111](111)
[^2]: [2222](222)
[^3]: [3333](333)
[^4]: [4444](4444)
```

After pressing `Ctrl + S` and linting the file, The result was:

```markdown
Paragraph 1. [^1]

Paragraph 2. [^2]

Paragraph 3. [^3]

Paragraph 4. [^4]

[^4]: [4444](4444)
[^1]: [1111](111)
[^2]: [2222](222)
[^3]: [3333](333)
```

Clearly, the keys are not arranged in order after first lint. I can press `Ctrl + S` again to lint it. Then the result became:

```markdown
Paragraph 1. [^1]

Paragraph 2. [^2]

Paragraph 3. [^3]

Paragraph 4. [^4]

[^1]: [1111](111)
[^2]: [2222](222)
[^3]: [3333](333)
[^4]: [4444](4444)
```

Now the key's order is right but the key-value pair mess up. We can see the original text that `Paragraph 1`'s footnote should be `[4444](4444)` while the result is `[1111](111)`.

As a contrast, the whole ground truth should be:

```markdown
Paragraph 1. [^1]

Paragraph 2. [^2]

Paragraph 3. [^3]

Paragraph 4. [^4]

[^1]: [4444](4444)
[^2]: [1111](111)
[^3]: [2222](222)
[^4]: [3333](333)
```

## Log 

The Log of first lint:

```
Running linter
rules before regular rules: 0 ms
Running Footnote after Punctuation
footnote-after-punctuation: 0.2999999988824129 ms
Paragraph 1. [^4]

Paragraph 2. [^1]

Paragraph 3. [^2]

Paragraph 4. [^3]

[^1]: [1111](111)
[^2]: [2222](222)
[^3]: [3333](333)
[^4]: [4444](4444)
Running Move Footnotes to the bottom
move-footnotes-to-the-bottom: 0.30000000074505806 ms
Paragraph 1. [^4]

Paragraph 2. [^1]

Paragraph 3. [^2]

Paragraph 4. [^3]

[^4]: [4444](4444)
[^1]: [1111](111)
[^2]: [2222](222)
[^3]: [3333](333)
Running Re-Index Footnotes
re-index-footnotes: 0.09999999962747097 ms
Paragraph 1. [^1]

Paragraph 2. [^2]

Paragraph 3. [^3]

Paragraph 4. [^4]

[^4]: [4444](4444)
[^1]: [1111](111)
[^2]: [2222](222)
[^3]: [3333](333)
Running Custom Regex
custom regex rules: 0 ms
rules after regular rules: 0.19999999925494194 ms
rules running: 1 ms
```

And the second lint:

```
Running linter
rules before regular rules: 0 ms
Running Footnote after Punctuation
footnote-after-punctuation: 0.19999999925494194 ms
Paragraph 1. [^1]

Paragraph 2. [^2]

Paragraph 3. [^3]

Paragraph 4. [^4]

[^4]: [4444](4444)
[^1]: [1111](111)
[^2]: [2222](222)
[^3]: [3333](333)
Running Move Footnotes to the bottom
move-footnotes-to-the-bottom: 0 ms
Paragraph 1. [^1]

Paragraph 2. [^2]

Paragraph 3. [^3]

Paragraph 4. [^4]

[^1]: [1111](111)
[^2]: [2222](222)
[^3]: [3333](333)
[^4]: [4444](4444)
Running Re-Index Footnotes
re-index-footnotes: 0.09999999962747097 ms
Paragraph 1. [^1]

Paragraph 2. [^2]

Paragraph 3. [^3]

Paragraph 4. [^4]

[^1]: [1111](111)
[^2]: [2222](222)
[^3]: [3333](333)
[^4]: [4444](4444)
Running Custom Regex
custom regex rules: 0 ms
rules after regular rules: 0.19999999925494194 ms
rules running: 0.8999999985098839 ms
```


Pull request:
Fix `Re-Index Footnotes` Not Working for Markdown Link Values
Fixes #1006 

There was an issue where `Re-Index Footnotes` would not properly recognize footnote references if they had markdown or wiki links in them.

For example:
``` markdown
 Paragraph 1. [^4]

Paragraph 2. [^1]

Paragraph 3. [^2]

Paragraph 4. [^3]

[^4]: [4444](4444)
[^1]: [1111](111)
[^2]: [2222](222)
[^3]: [3333](333)
```
was left as is.

To fix this, we needed to stop ignoring markdown and wiki links.

Changes Made:
- Added tests around the move and re-index footnotes
- Removed ignoring wiki and markdown links for footnotes

Patch:
diff --git a/src/rules/re-index-footnotes.ts b/src/rules/re-index-footnotes.ts
index 07dc0a7..5ea5f1d 100644
--- a/src/rules/re-index-footnotes.ts
+++ b/src/rules/re-index-footnotes.ts
@@ -13,7 +13,7 @@ export default class ReIndexFootnotes extends RuleBuilder<ReIndexFootnotesOption
       nameKey: 'rules.re-index-footnotes.name',
       descriptionKey: 'rules.re-index-footnotes.description',
       type: RuleType.FOOTNOTE,
-      ruleIgnoreTypes: [IgnoreTypes.code, IgnoreTypes.inlineCode, IgnoreTypes.math, IgnoreTypes.yaml, IgnoreTypes.link, IgnoreTypes.wikiLink, IgnoreTypes.tag],
+      ruleIgnoreTypes: [IgnoreTypes.code, IgnoreTypes.inlineCode, IgnoreTypes.math, IgnoreTypes.yaml, IgnoreTypes.tag],
     });
   }
   get OptionsClass(): new () => ReIndexFootnotesOptions {


Test patch:
diff --git a/__tests__/move-footnotes-to-the-bottom.test.ts b/__tests__/move-footnotes-to-the-bottom.test.ts
index 2938d7c..179b0ff 100644
--- a/__tests__/move-footnotes-to-the-bottom.test.ts
+++ b/__tests__/move-footnotes-to-the-bottom.test.ts
@@ -274,5 +274,36 @@ ruleTest({
         [^3]: D
       `,
     },
+    { // accounts for https://github.com/platers/obsidian-linter/issues/1006
+      testName: 'Moving footnotes to the bottom of the file should properly order values even when links are present',
+      before: dedent`
+        Paragraph 1. [^4]
+        ${''}
+        Paragraph 2. [^1]
+        ${''}
+        Paragraph 3. [^2]
+        ${''}
+        Paragraph 4. [^3]
+        ${''}
+        [^1]: [1111](111)
+        [^2]: [2222](222)
+        [^3]: [3333](333)
+        [^4]: [4444](4444)
+      `,
+      after: dedent`
+        Paragraph 1. [^4]
+        ${''}
+        Paragraph 2. [^1]
+        ${''}
+        Paragraph 3. [^2]
+        ${''}
+        Paragraph 4. [^3]
+        ${''}
+        [^4]: [4444](4444)
+        [^1]: [1111](111)
+        [^2]: [2222](222)
+        [^3]: [3333](333)
+      `,
+    },
   ],
 });
diff --git a/__tests__/re-index-footnotes.test.ts b/__tests__/re-index-footnotes.test.ts
index 37b4eb6..db4f47c 100644
--- a/__tests__/re-index-footnotes.test.ts
+++ b/__tests__/re-index-footnotes.test.ts
@@ -93,6 +93,36 @@ ruleTest({
         More text.
       `,
     },
+    { // accounts for https://github.com/platers/obsidian-linter/issues/1006
+      testName: 'Re-indexing footnotes with links as the referenced values should keep the references to text correlation',
+      before: dedent`
+        Paragraph 1. [^4]
+        ${''}
+        Paragraph 2. [^1]
+        ${''}
+        Paragraph 3. [^2]
+        ${''}
+        Paragraph 4. [^3]
+        ${''}
+        [^4]: [4444](4444)
+        [^1]: [1111](111)
+        [^2]: [2222](222)
+        [^3]: [3333](333)
+      `,
+      after: dedent`
+        Paragraph 1. [^1]
+        ${''}
+        Paragraph 2. [^2]
+        ${''}
+        Paragraph 3. [^3]
+        ${''}
+        Paragraph 4. [^4]
+        ${''}
+        [^1]: [4444](4444)
+        [^2]: [1111](111)
+        [^3]: [2222](222)
+        [^4]: [3333](333)
+      `,
+    },
   ],
 });
-