Repository: platers/obsidian-linter (ts)

Issue:
Bug: Add space between CJK & EN Not Ignoring the Ignoring of HTML
```
Before
<u>自己想说的</u>

After
<u> 自己想说的 </u>
```

The underline will contain the extra SPACEs.

Pull request description:
Allow HTML Tags to Not Have Space Added Around it When Used Beside CJK Characters
Fixes #667 

Changes Made:
- Updated CJK with spaces between it and English characters to not consider `{` and `}` as characters to add space around if present
- Made sure that there was ignore list to allow adding whitespace around links, inline math, and inline code blocks
- Added a test for the scenario in question

Patch:
diff --git a/src/rules/space-between-chinese-japanese-or-korean-and-english-or-numbers.ts b/src/rules/space-between-chinese-japanese-or-korean-and-english-or-numbers.ts
index c4a339b..a52f812 100644
--- a/src/rules/space-between-chinese-japanese-or-korean-and-english-or-numbers.ts
+++ b/src/rules/space-between-chinese-japanese-or-korean-and-english-or-numbers.ts
@@ -22,21 +22,24 @@ export default class SpaceBetweenChineseJapaneseOrKoreanAndEnglishOrNumbers exte
       text: string,
       options: SpaceBetweenChineseJapaneseOrKoreanAndEnglishOrNumbersOptions,
   ): string {
-    const head =
-      /(\p{sc=Han}|\p{sc=Katakana}|\p{sc=Hiragana}|\p{sc=Hangul})( *)(\[[^[]*\]\(.*\)|`[^`]*`|\w+|[-+'"([{¥$]|\*[^*])/gmu;
-    const tail =
-      /(\[[^[]*\]\(.*\)|`[^`]*`|\w+|[-+;:'"°%$)\]}]|[^*]\*)( *)(\p{sc=Han}|\p{sc=Katakana}|\p{sc=Hiragana}|\p{sc=Hangul})/gmu;
+    const head = /(\p{sc=Han}|\p{sc=Katakana}|\p{sc=Hiragana}|\p{sc=Hangul})( *)(\[[^[]*\]\(.*\)|`[^`]*`|\w+|[-+'"([¥$]|\*[^*])/gmu;
+    const tail = /(\[[^[]*\]\(.*\)|`[^`]*`|\w+|[-+;:'"°%$)\]]|[^*]\*)( *)(\p{sc=Han}|\p{sc=Katakana}|\p{sc=Hiragana}|\p{sc=Hangul})/gmu;
+    // inline math, inline code, makrdown links, and wiki links are an exception in that even though they are to be ignored we want to keep a space around these types when surrounded by CJK characters
+    const regexEscapedIgnoreExceptionPlaceHolders = `${IgnoreTypes.link.placeholder}|${IgnoreTypes.inlineMath.placeholder}|${IgnoreTypes.inlineCode.placeholder}|${IgnoreTypes.wikiLink.placeholder}`.replaceAll('{', '\\{').replaceAll('}', '\\}');
+    const ignoreExceptionsHead = new RegExp(`(\\p{sc=Han}|\\p{sc=Katakana}|\\p{sc=Hiragana}|\\p{sc=Hangul})( *)(${regexEscapedIgnoreExceptionPlaceHolders})`, 'gmu');
+    const ignoreExceptionsTail = new RegExp(`(${regexEscapedIgnoreExceptionPlaceHolders})( *)(\\p{sc=Han}|\\p{sc=Katakana}|\\p{sc=Hiragana}|\\p{sc=Hangul})`, 'gmu');
     const addSpaceAroundChineseJapaneseKoreanAndEnglish = function(
         text: string,
     ): string {
       return text.replace(head, '$1 $3').replace(tail, '$1 $3');
     };
 
-
     return ignoreListOfTypes([IgnoreTypes.code, IgnoreTypes.inlineCode, IgnoreTypes.yaml, IgnoreTypes.image, IgnoreTypes.link,
       IgnoreTypes.wikiLink, IgnoreTypes.tag, IgnoreTypes.math, IgnoreTypes.inlineMath, IgnoreTypes.html], text, (text: string) => {
       let newText = ignoreListOfTypes([IgnoreTypes.italics, IgnoreTypes.bold], text, addSpaceAroundChineseJapaneseKoreanAndEnglish);
 
+      newText = newText.replace(ignoreExceptionsHead, '$1 $3').replace(ignoreExceptionsTail, '$1 $3');
+
       newText = updateItalicsText(
           newText,
           addSpaceAroundChineseJapaneseKoreanAndEnglish,
@@ -46,6 +49,7 @@ export default class SpaceBetweenChineseJapaneseOrKoreanAndEnglishOrNumbers exte
           newText,
           addSpaceAroundChineseJapaneseKoreanAndEnglish,
       );
+
       return newText;
     });
   }


Test patch:
diff --git a/__tests__/space-between-chinese-japanese-or-korean-and-english-or-numbers.test.ts b/__tests__/space-between-chinese-japanese-or-korean-and-english-or-numbers.test.ts
index 10c8dc6..47eb202 100644
--- a/__tests__/space-between-chinese-japanese-or-korean-and-english-or-numbers.test.ts
+++ b/__tests__/space-between-chinese-japanese-or-korean-and-english-or-numbers.test.ts
@@ -104,6 +104,14 @@ ruleTest({
         <img src="中文small.png" />
       `,
     },
-
+    { // accounts for https://github.com/platers/obsidian-linter/issues/667
+      testName: 'Make sure that html elements do not affect the surrounding text',
+      before: dedent`
+        <u>自己想说的</u>
+      `,
+      after: dedent`
+        <u>自己想说的</u>
+      `,
+    },
   ],
 });