From ca24b889cf3ad0e169e9d91476a541849ee70555 Mon Sep 17 00:00:00 2001 From: sev Date: Thu, 11 Jul 2024 16:18:43 +0000 Subject: [PATCH 1/3] support Obsidian-style comment for inline linter toggle --- src/utils/regex.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/utils/regex.ts b/src/utils/regex.ts index 7560a313..59f2df44 100644 --- a/src/utils/regex.ts +++ b/src/utils/regex.ts @@ -133,7 +133,7 @@ export function matchTagRegex(text: string): string[] { } export function generateHTMLLinterCommentWithSpecificTextAndWhitespaceRegexMatch(isStart: boolean): RegExp { - const regexTemplate = ''; + const regexTemplate = '(?:|%%)'; let endingText = ''; if (isStart) { From 53c3448e0a79b9be408283e0a2ad815268e32e14 Mon Sep 17 00:00:00 2001 From: Peter Kaufman Date: Fri, 19 Jul 2024 17:25:38 -0400 Subject: [PATCH 2/3] added tests to verify that the additional format does indeed work --- ...all-custom-ignore-sections-in-text.test.ts | 96 ++++++++++++++++++- __tests__/ignore-list-of-types.test.ts | 38 ++++++++ __tests__/rules-runner.test.ts | 20 ++++ 3 files changed, 153 insertions(+), 1 deletion(-) diff --git a/__tests__/get-all-custom-ignore-sections-in-text.test.ts b/__tests__/get-all-custom-ignore-sections-in-text.test.ts index 0dbda3f1..ce32014d 100644 --- a/__tests__/get-all-custom-ignore-sections-in-text.test.ts +++ b/__tests__/get-all-custom-ignore-sections-in-text.test.ts @@ -28,6 +28,16 @@ const getCustomIgnoreSectionsInTextTestCases: customIgnoresInTextTestCase[] = [ expectedCustomIgnoresInText: 0, expectedPositions: [], }, + { + name: 'when no custom ignore start indicator is present, no positions are returned even if custom ignore end indicator is present when Obsidian comment format is used', + text: dedent` + Here is some text + %% linter-enable %% + Here is some more text + `, + expectedCustomIgnoresInText: 0, + expectedPositions: [], + }, { name: 'a simple example of a start and end custom ignore indicator results in the proper start and end positions for the ignore section', text: dedent` @@ -41,6 +51,19 @@ const getCustomIgnoreSectionsInTextTestCases: customIgnoresInTextTestCase[] = [ expectedCustomIgnoresInText: 1, expectedPositions: [{startIndex: 18, endIndex: 135}], }, + { + name: 'a simple example of a start and end custom ignore indicator results in the proper start and end positions for the ignore section when Obsidian comment format is used', + text: dedent` + Here is some text + %% linter-disable %% + This content will be ignored + So any format put here gets to stay as is + %% linter-enable %% + More text here... + `, + expectedCustomIgnoresInText: 1, + expectedPositions: [{startIndex: 18, endIndex: 129}], + }, { name: 'when a custom ignore start indicator is not followed by a custom ignore end indicator in the text, the end is considered to be the end of the text', text: dedent` @@ -53,6 +76,18 @@ const getCustomIgnoreSectionsInTextTestCases: customIgnoresInTextTestCase[] = [ expectedCustomIgnoresInText: 1, expectedPositions: [{startIndex: 18, endIndex: 129}], }, + { + name: 'when a custom ignore start indicator is not followed by a custom ignore end indicator in the text, the end is considered to be the end of the text when Obsidian comment format is used', + text: dedent` + Here is some text + %% linter-disable %% + This content will be ignored + So any format put here gets to stay as is + More text here... + `, + expectedCustomIgnoresInText: 1, + expectedPositions: [{startIndex: 18, endIndex: 126}], + }, { name: 'when a custom ignore start indicator shows up midline, it ignores the part in question', text: dedent` @@ -64,6 +99,17 @@ const getCustomIgnoreSectionsInTextTestCases: customIgnoresInTextTestCase[] = [ expectedCustomIgnoresInText: 1, expectedPositions: [{startIndex: 17, endIndex: 87}], }, + { + name: 'when a custom ignore start indicator shows up midline, it ignores the part in question when Obsidian comment format is used', + text: dedent` + Here is some text%% linter-disable %%here is some ignored text%% linter-enable %% + This content will be ignored + So any format put here gets to stay as is + More text here... + `, + expectedCustomIgnoresInText: 1, + expectedPositions: [{startIndex: 17, endIndex: 81}], + }, { name: 'when a custom ignore start indicator does not follow the exact syntax, it is counted as existing when it is a single-line comment', text: dedent` @@ -93,8 +139,26 @@ const getCustomIgnoreSectionsInTextTestCases: customIgnoresInTextTestCase[] = [ expectedCustomIgnoresInText: 2, expectedPositions: [{startIndex: 178, endIndex: 316}, {startIndex: 17, endIndex: 87}], }, + { + name: 'multiple matches can be returned when Obsidian comment format is used', + text: dedent` + Here is some text%% linter-disable %%here is some ignored text%% linter-enable %% + This content will be ignored + So any format put here gets to stay as is + More text here... + ${''} + %% linter-disable %% + We want to ignore the following as we want to preserve its format + -> level 1 + -> level 1.3 + -> level 2 + Finish + `, + expectedCustomIgnoresInText: 2, + expectedPositions: [{startIndex: 172, endIndex: 307}, {startIndex: 17, endIndex: 81}], + }, { // relates to https://github.com/platers/obsidian-linter/issues/733 - name: 'multiple matches can be returned', + name: 'multiple matches can be returned with math blocks', text: dedent` content ${''} @@ -123,6 +187,36 @@ const getCustomIgnoreSectionsInTextTestCases: customIgnoresInTextTestCase[] = [ expectedCustomIgnoresInText: 2, expectedPositions: [{startIndex: 86, endIndex: 152}, {startIndex: 9, endIndex: 75}], }, + { + name: 'multiple matches can be returned with math blocks when Obsidian comment format is used', + text: dedent` + content + ${''} + %% linter-disable %% + ${''} + $$ + abc + $$ + {#eq:a} + ${''} + %% linter-enable %% + ${''} + content + ${''} + %% linter-disable %% + ${''} + $$ + abc + $$ + {#eq:b} + ${''} + %% linter-enable %% + ${''} + content + `, + expectedCustomIgnoresInText: 2, + expectedPositions: [{startIndex: 80, endIndex: 140}, {startIndex: 9, endIndex: 69}], + }, ]; describe('Get All Custom Ignore Sections in Text', () => { diff --git a/__tests__/ignore-list-of-types.test.ts b/__tests__/ignore-list-of-types.test.ts index 5fcd7cf1..b912afa3 100644 --- a/__tests__/ignore-list-of-types.test.ts +++ b/__tests__/ignore-list-of-types.test.ts @@ -59,6 +59,44 @@ const ignoreListOfTypesTestCases: customIgnoresInTextTestCase[] = [ `, ignoreTypes: [IgnoreTypes.customIgnore], }, + { + name: 'when no custom ignore ranges are used and multiple times, the text is properly replaced and put back together when Obsidian comment format used', + text: dedent` + content + ${''} + %% linter-disable %% + ${''} + $$ + abc + $$ + ${''} + %% linter-enable %% + ${''} + content + ${''} + %% linter-disable %% + ${''} + $$ + abc + $$ + ${''} + %% linter-enable %% + ${''} + content + `, + expectedTextAfterIgnore: dedent` + content + ${''} + {CUSTOM_IGNORE_PLACEHOLDER} + ${''} + content + ${''} + {CUSTOM_IGNORE_PLACEHOLDER} + ${''} + content + `, + ignoreTypes: [IgnoreTypes.customIgnore], + }, ]; describe('Ignore List of Types', () => { diff --git a/__tests__/rules-runner.test.ts b/__tests__/rules-runner.test.ts index 25eee360..6dfaec02 100644 --- a/__tests__/rules-runner.test.ts +++ b/__tests__/rules-runner.test.ts @@ -224,6 +224,26 @@ const customReplaceTestCases: CustomReplaceTestCase[] = [ Did it stay the same? `, }, + { // relates for https://github.com/platers/obsidian-linter/issues/1121 + testName: 'A custom replace should respect linter ignore ranges that use the Obsidian comment format', + listOfRegexReplacements: [ + { + label: 'Replace Did at the start of a line or look? at the end of a line', find: '(^Did)|(look\\?$)', replace: 'swapped', flags: 'gm', + }, + ], + before: dedent` + How does this look? + %% linter-disable %% + Did it stay the same? + %% linter-enable %% + `, + after: dedent` + How does this swapped + %% linter-disable %% + Did it stay the same? + %% linter-enable %% + `, + }, ]; describe('Rules Runner', () => { From f3d72a57e9582627dce41745f52c17083d19a99b Mon Sep 17 00:00:00 2001 From: Peter Kaufman Date: Fri, 19 Jul 2024 17:31:21 -0400 Subject: [PATCH 3/3] added docs changes for the ranged ignores --- docs/docs/usage/disabling-rules.md | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/docs/docs/usage/disabling-rules.md b/docs/docs/usage/disabling-rules.md index c3d6a445..a2b4bd49 100644 --- a/docs/docs/usage/disabling-rules.md +++ b/docs/docs/usage/disabling-rules.md @@ -49,7 +49,7 @@ disabled rules: [all] ### Range Ignore When there is a need to disable the Linter for part of a file, ranged ignores can be used. The syntax for a ranged ignore -is `` with an optional `` where you want the Linter to start back up with its linting. +is `` or `%%linter-disable%%` with an optional `` or `%%linter-disable%%` where you want the Linter to start back up with its linting. Leaving off the ending of a range ignore will assume you want to ignore the file contents from the start of the range ignore to the end of the file. So be careful when not ending a range ignore. !!! warning @@ -62,6 +62,9 @@ Here is some text This area will not be formatted More content goes here... +%%linter-disable %% + This area will not be formatted +%%linter-enable%% ``` Here is another example that shows a ranged ignore without an ending indicator: