Repository: platers/obsidian-linter (ts)

Issue:
Bug: Two lines around Code Fences.
## Describe the Bug

Will add two lines around Code Fences.

## How to Reproduce

Steps to reproduce the behavior:

1. Enable **Empty Line Around Code Fences**
2. Enable **Consecutive blank lines**

## Expected Behavior

A clear and concise description of what you expected to happen.

Expected output if applicable:

The formatted result:

~~~
``` markdown
1. 修改 php.ini


    ```
    upload_max_filesize = 20M
    post_max_size = 20M
    ```


2. 修改 Nginx 配置文件


    ```
    client_max_body_size    20m; # 文件最大大小
    ```


1111

```
int a = 0;
```

2222
```
~~~

The code block not in the list is correct, it is around with a blank line.
The code block in the list will be around two blank lines. I think the code block around with one line is correct.

## Device

- [x] Desktop



Pull request description:
Make Sure an Empty Line Around Content Is Respected when Content Is Indented
Fixes #456 

Changes Made:
- Added a UT for the scenario in question
- Made sure to allow empty lines to match empty line conditions when checking if there is an empty line around content even if it is indented
- Updated a JSDoc

Patch:
diff --git a/src/utils/strings.ts b/src/utils/strings.ts
index 34ace45..13d1106 100644
--- a/src/utils/strings.ts
+++ b/src/utils/strings.ts
@@ -39,7 +39,7 @@ export function stripCr(text: string): string {
  * @param {boolean} requireSameTrailingWhitespace - Whether or not to do an exact comparison or allow whitespace to differ
  * @return {boolean} Whether or not the text matched the expected value
  */
-function textMatches(expectedText: string, actualText: string, requireSameTrailingWhitespace: boolean): boolean {
+function textMatches(expectedText: string, actualText: string, requireSameTrailingWhitespace: boolean = false): boolean {
   if (requireSameTrailingWhitespace) {
     return expectedText == actualText;
   }
@@ -58,20 +58,17 @@ function textMatches(expectedText: string, actualText: string, requireSameTraili
 export function makeSureContentHasEmptyLinesAddedBeforeAndAfter(text: string, start: number, end: number, isForBlockquotes: boolean = false): string {
   const content = text.substring(start, end);
   let startOfLine = '';
-  let requireSameTrailingWhitespace = true;
   let contentPriorToContent = text.substring(0, start);
   if (contentPriorToContent.length > 0) {
     const contentLinesPriorToContent = contentPriorToContent.split('\n');
     startOfLine = contentLinesPriorToContent[contentLinesPriorToContent.length - 1] ?? '';
-    requireSameTrailingWhitespace = startOfLine.trim() == '';
-    if (!requireSameTrailingWhitespace) {
-      startOfLine = startOfLine.trimEnd();
-    }
+    startOfLine = startOfLine.trimEnd();
+
 
     let numberOfIndexesToRemove = 0;
     while (contentLinesPriorToContent.length - (2 + numberOfIndexesToRemove) >= 0) {
       const lineContent = contentLinesPriorToContent[contentLinesPriorToContent.length - (2 + numberOfIndexesToRemove)];
-      if (!textMatches(startOfLine, lineContent, requireSameTrailingWhitespace) && (!isForBlockquotes || !textMatches('', lineContent, true))) {
+      if (!textMatches(startOfLine, lineContent) && (!isForBlockquotes || !textMatches('', lineContent, true))) {
         break;
       }
 
@@ -82,7 +79,7 @@ export function makeSureContentHasEmptyLinesAddedBeforeAndAfter(text: string, st
 
     if (contentLinesPriorToContent.length > 1) {
       if ((isForBlockquotes && contentLinesPriorToContent[contentLinesPriorToContent.length - 2].match(/^> ?.*$/m)) ||
-      (!isForBlockquotes && !textMatches(startOfLine, contentLinesPriorToContent[contentLinesPriorToContent.length - 2], requireSameTrailingWhitespace))) {
+      (!isForBlockquotes && !textMatches(startOfLine, contentLinesPriorToContent[contentLinesPriorToContent.length - 2]))) {
         contentLinesPriorToContent.splice(contentLinesPriorToContent.length - 1, 0, startOfLine);
       } else if (!textMatches('', contentLinesPriorToContent[contentLinesPriorToContent.length - 2], true)) {
         contentLinesPriorToContent.splice(contentLinesPriorToContent.length - 1, 0, '');
@@ -98,7 +95,7 @@ export function makeSureContentHasEmptyLinesAddedBeforeAndAfter(text: string, st
     let numberOfIndexesToRemove = 0;
     while (numberOfIndexesToRemove + 1 < contentLinesAfterBlock.length) {
       const lineContent = contentLinesAfterBlock[1+numberOfIndexesToRemove];
-      if (!textMatches(startOfLine, lineContent, requireSameTrailingWhitespace) && (!isForBlockquotes || !textMatches('', lineContent, true))) {
+      if (!textMatches(startOfLine, lineContent) && (!isForBlockquotes || !textMatches('', lineContent, true))) {
         break;
       }
 
@@ -109,9 +106,9 @@ export function makeSureContentHasEmptyLinesAddedBeforeAndAfter(text: string, st
 
     if (contentLinesAfterBlock.length > 1) {
       if ((isForBlockquotes && contentLinesAfterBlock[1].match(/^> ?.*$/m)) ||
-      (!isForBlockquotes && !textMatches(startOfLine, contentLinesAfterBlock[1], requireSameTrailingWhitespace))) {
+      (!isForBlockquotes && !textMatches(startOfLine, contentLinesAfterBlock[1]))) {
         contentLinesAfterBlock.splice(1, 0, startOfLine);
-      } else if (isForBlockquotes && !textMatches('', contentLinesAfterBlock[1], requireSameTrailingWhitespace)) {
+      } else if (isForBlockquotes && !textMatches('', contentLinesAfterBlock[1])) {
         contentLinesAfterBlock.splice(1, 0, '');
       }
     }
diff --git a/src/utils/yaml.ts b/src/utils/yaml.ts
index fe10479..3f68889 100644
--- a/src/utils/yaml.ts
+++ b/src/utils/yaml.ts
@@ -8,7 +8,7 @@ export const LINTER_ALIASES_HELPER_KEY = 'linter-yaml-title-alias';
 /**
  * Adds an empty YAML block to the text if it doesn't already have one.
  * @param {string} text - The text to process
- * @return {string} The processed text with an YAML block
+ * @return {string} The processed text with a YAML block
  */
 export function initYAML(text: string): string {
   if (text.match(yamlRegex) === null) {


Test patch:
diff --git a/__tests__/empty-line-around-code-fences.test.ts b/__tests__/empty-line-around-code-fences.test.ts
index 833f0b0..6430bf4 100644
--- a/__tests__/empty-line-around-code-fences.test.ts
+++ b/__tests__/empty-line-around-code-fences.test.ts
@@ -39,5 +39,52 @@ ruleTest({
         > \`\`\`
       `,
     },
+    { // accounts for https://github.com/platers/obsidian-linter/issues/456
+      testName: 'Make sure that there is only 1 empty line be around a fenced code block even if the code block is indented',
+      before: dedent`
+        1. 修改 php.ini
+        ${''}
+            \`\`\`
+            upload_max_filesize = 20M
+            post_max_size = 20M
+            \`\`\`
+          ${''}
+        2. 修改 Nginx 配置文件
+        ${''}
+            \`\`\`
+            client_max_body_size    20m; # 文件最大大小
+            \`\`\`
+        ${''}
+        1111
+        ${''}
+          \`\`\`
+          int a = 0;
+          \`\`\`
+        ${''}
+        2222
+      `,
+      after: dedent`
+        1. 修改 php.ini
+        ${''}
+            \`\`\`
+            upload_max_filesize = 20M
+            post_max_size = 20M
+            \`\`\`
+        ${''}
+        2. 修改 Nginx 配置文件
+        ${''}
+            \`\`\`
+            client_max_body_size    20m; # 文件最大大小
+            \`\`\`
+        ${''}
+        1111
+        ${''}
+          \`\`\`
+          int a = 0;
+          \`\`\`
+        ${''}
+        2222
+      `,
+    },
   ],
 });