Repository: platers/obsidian-linter (ts)

Issue:
Bug: In No Bare URLs, Percent-Encoding in URLs to be formatted incorrectly.
- [X] I have verified that I am on the latest version of the Linter

Version: 1.23.1

## Describe the Bug

The percent sign is a legal charater in URI, but after formatting, the percent sign is placed outside of the Angle brackets.

>    A percent-encoding mechanism is used to represent a data octet in a
   component when that octet's corresponding character is outside the
   allowed set or is being used as a delimiter of, or within, the
   component. [^1]

[^1]: RFC 3986 - Uniform Resource Identifier (URI): Generic Syntax: https://datatracker.ietf.org/doc/html/rfc3986#section-2.1

## How to Reproduce

Enable No Bare URLs.

Original sentence

```markdown
https://zh.wikipedia.org/wiki/%E5%85%AC%E5%8E%86
https://baike.baidu.com/item/%E7%99%BE%E7%A7%91
https://www.google.com/search?q=%E7%A9%BA%E6%A0%BC
https://cn.bing.com/search?pglt=163&q=%E6%A0%BC%E5%BC%8F%E5%8C%96
https://www.google.com/search?q=%E3%81%A1%E3%82%85%E3%81%86%E3%81%94%E3%81%8F%E3%81%94
```

After formatting

```markdown
<https://zh.wikipedia.org/wiki/>%E5%85%AC%E5%8E%86
<https://baike.baidu.com/item/>%E7%99%BE%E7%A7%91
# Here the equal sign is also excluded.
<https://www.google.com/search?q>=%E7%A9%BA%E6%A0%BC
<https://cn.bing.com/search?pglt=163&q>=%E6%A0%BC%E5%BC%8F%E5%8C%96
<https://www.google.com/search?q>=%E3%81%A1%E3%82%85%E3%81%86%E3%81%94%E3%81%8F%E3%81%94
```

But the correct result should be


```markdown
<https://zh.wikipedia.org/wiki/%E5%85%AC%E5%8E%86>
<https://baike.baidu.com/item/%E7%99%BE%E7%A7%91>
<https://www.google.com/search?q=%E7%A9%BA%E6%A0%BC>
<https://cn.bing.com/search?pglt=163&q=%E6%A0%BC%E5%BC%8F%E5%8C%96>
<https://www.google.com/search?q=%E3%81%A1%E3%82%85%E3%81%86%E3%81%94%E3%81%8F%E3%81%94>
```

## Screenshots

![01](https://github.com/platers/obsidian-linter/assets/61381278/e0e3d8c0-48cb-4b40-b1f3-1d9787f3f014)

## Device

- [X] Desktop
- [ ] Mobile

## Additional Context

May be related to: #900 

Pull request description:
Fix: `No Bare URLs` Not Including `%` in URL
Fixes #1029

There was an issue where URLs with `%` in the URL for encoding would not get properly matched by the URL regex which caused weird bare URL results.

Changes Made:
- Updated regex to allow percent signs in the no bare urls url matches

Patch:
diff --git a/src/utils/regex.ts b/src/utils/regex.ts
index ac16a45..f6c0cc1 100644
--- a/src/utils/regex.ts
+++ b/src/utils/regex.ts
@@ -26,7 +26,7 @@ export const tableRow = /[^\n]*?\|[^\n]*?(\n|$)/m;
 // based on https://gist.github.com/skeller88/5eb73dc0090d4ff1249a
 export const simpleURIRegex = /(([a-z\-0-9]+:)\/{2})([^\s/?#]*[^\s")'.?!/]|[/])?(([/?#][^\s")']*[^\s")'.?!])|[/])?/gi;
 // generated from https://github.com/spamscanner/url-regex-safe using strict: true, returnString: true, and re2: false as options
-export const urlRegex = /(?:(?:(?:[a-z]+:)?\/\/)|www\.)(?:localhost|(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)(?:\.(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)){3}|(?:(?:[a-fA-F\d]{1,4}:){7}(?:[a-fA-F\d]{1,4}|:)|(?:[a-fA-F\d]{1,4}:){6}(?:(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)(?:\.(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)){3}|:[a-fA-F\d]{1,4}|:)|(?:[a-fA-F\d]{1,4}:){5}(?::(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)(?:\.(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)){3}|(?::[a-fA-F\d]{1,4}){1,2}|:)|(?:[a-fA-F\d]{1,4}:){4}(?:(?::[a-fA-F\d]{1,4}){0,1}:(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\d|\d)(?:\.(?:25[0-5]|2[0-4]\d|1\d\d|[1-9]\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a-z0-9][-_]*)*[a-z0-9]+)(?:\.(?:[a-z0-9]-*)*[a-z0-9]+)*(?:\.(?:[a-z]{2,})))(?::\d{2,5})?(?:(?:[/?#][a-z0-9-_%/&=?$.+!*‘(,#]*[a-z0-9-%_/$+!*‘(,])|[/])?/gi;
 export const anchorTagRegex = /<a[\s]+([^>]+)>((?:.(?!<\/a>))*.)<\/a>/g;
 export const wordRegex = /[\p{L}\p{N}\p{Pc}\p{M}\-'’`]+/gu;
 // regex from https://stackoverflow.com/a/26128757/8353749


Test patch:
diff --git a/__tests__/no-bare-urls.test.ts b/__tests__/no-bare-urls.test.ts
index 4fce83f..e68082c 100644
--- a/__tests__/no-bare-urls.test.ts
+++ b/__tests__/no-bare-urls.test.ts
@@ -160,6 +160,23 @@ ruleTest({
         <https://m3.bbz-dormagen-moodle.de/tag/index.php?tc=1&tag=Nachschreibtermin&from=4817>
       `,
     },
+    {// accounts for https://github.com/platers/obsidian-linter/issues/1029
+      testName: 'Make sure that URLs with percents are properly matched',
+      before: dedent`
+        https://zh.wikipedia.org/wiki/%E5%85%AC%E5%8E%86
+        https://baike.baidu.com/item/%E7%99%BE%E7%A7%91
+        https://www.google.com/search?q=%E7%A9%BA%E6%A0%BC
+        https://cn.bing.com/search?pglt=163&q=%E6%A0%BC%E5%BC%8F%E5%8C%96
+        https://www.google.com/search?q=%E3%81%A1%E3%82%85%E3%81%86%E3%81%94%E3%81%8F%E3%81%94
+      `,
+      after: dedent`
+        <https://zh.wikipedia.org/wiki/%E5%85%AC%E5%8E%86>
+        <https://baike.baidu.com/item/%E7%99%BE%E7%A7%91>
+        <https://www.google.com/search?q=%E7%A9%BA%E6%A0%BC>
+        <https://cn.bing.com/search?pglt=163&q=%E6%A0%BC%E5%BC%8F%E5%8C%96>
+        <https://www.google.com/search?q=%E3%81%A1%E3%82%85%E3%81%86%E3%81%94%E3%81%8F%E3%81%94>
+      `,
+    },
   ],
 });