Skip to content

Commit

Permalink
Merge pull request #1122 from sevmonster/patch-1
Browse files Browse the repository at this point in the history
Support Obsidian-style comment for inline linter toggle
  • Loading branch information
pjkaufman authored Jul 19, 2024
2 parents 7314564 + f3d72a5 commit f7a5ada
Show file tree
Hide file tree
Showing 5 changed files with 158 additions and 3 deletions.
96 changes: 95 additions & 1 deletion __tests__/get-all-custom-ignore-sections-in-text.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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`
Expand All @@ -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`
Expand All @@ -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`
Expand All @@ -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`
Expand Down Expand Up @@ -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
${''}
Expand Down Expand Up @@ -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', () => {
Expand Down
38 changes: 38 additions & 0 deletions __tests__/ignore-list-of-types.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down
20 changes: 20 additions & 0 deletions __tests__/rules-runner.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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', () => {
Expand Down
5 changes: 4 additions & 1 deletion docs/docs/usage/disabling-rules.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 `<!-- linter-disable -->` with an optional `<!-- linter-enable -->` where you want the Linter to start back up with its linting.
is `<!-- linter-disable -->` or `%%linter-disable%%` with an optional `<!-- linter-enable -->` 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
Expand All @@ -62,6 +62,9 @@ Here is some text
This area will not be formatted
<!-- linter-enable -->
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:
Expand Down
2 changes: 1 addition & 1 deletion src/utils/regex.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ export function matchTagRegex(text: string): string[] {
}

export function generateHTMLLinterCommentWithSpecificTextAndWhitespaceRegexMatch(isStart: boolean): RegExp {
const regexTemplate = '<!-{2,} *linter-{ENDING_TEXT} *-{2,}>';
const regexTemplate = '(?:<!-{2,}|%%) *linter-{ENDING_TEXT} *(?:-{2,}>|%%)';
let endingText = '';

if (isStart) {
Expand Down

0 comments on commit f7a5ada

Please sign in to comment.