Skip to content

Commit dbb39a2

Browse files
authored
Merge pull request platers#1247 from pjkaufman/easier-ignore
Feat: Add Ability to Ignore Files and Folders from Popup Menu and Commands
2 parents e3ca4a1 + a6f2ae9 commit dbb39a2

File tree

3 files changed

+112
-20
lines changed

3 files changed

+112
-20
lines changed

src/lang/locale/en.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,24 @@ export default {
3232
'paste-as-plain-text': {
3333
'name': 'Paste as Plain Text & without Modifications',
3434
},
35+
'ignore-folder': {
36+
'name': 'Ignore folder',
37+
},
38+
'ignore-file': {
39+
'name': 'Ignore file',
40+
},
3541
'lint-file-pop-up-menu-text': {
3642
'name': 'Lint file',
3743
},
3844
'lint-folder-pop-up-menu-text': {
3945
'name': 'Lint folder',
4046
},
47+
'ignore-file-pop-up-menu-text': {
48+
'name': 'Ignore file in Linter',
49+
},
50+
'ignore-folder-pop-up-menu-text': {
51+
'name': 'Ignore folder in Linter',
52+
},
4153
},
4254

4355
'logs': {

src/main.ts

Lines changed: 83 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import {createRunLinterRulesOptions, RulesRunner} from './rules-runner';
1111
import {LinterError} from './linter-error';
1212
import {LintConfirmationModal} from './ui/modals/lint-confirmation-modal';
1313
import {SettingTab} from './ui/settings';
14-
import {urlRegex} from './utils/regex';
14+
import {escapeRegExp, urlRegex} from './utils/regex';
1515
import {getTextInLanguage, LanguageStringKey, setLanguage} from './lang/helpers';
1616
import {RuleAliasSuggest} from './cm6/rule-alias-suggester';
1717
import {AfterFileChangeLintTimes, DEFAULT_SETTINGS, LinterSettings} from './settings-data';
@@ -215,6 +215,36 @@ export default class LinterPlugin extends Plugin {
215215
void this.pasteAsPlainText(editor);
216216
},
217217
});
218+
219+
this.addCommand({
220+
id: 'ignore-folder',
221+
name: getTextInLanguage('commands.ignore-folder.name'),
222+
icon: iconInfo.ignoreFolder.id,
223+
editorCheckCallback: (checking: boolean, _, ctx) => {
224+
if (checking) {
225+
if (ctx && ctx.file && ctx.file instanceof TFile && ctx.file.parent) {
226+
return !ctx.file.parent.isRoot() && !this.settings.foldersToIgnore.includes(ctx.file.parent.path);
227+
}
228+
229+
return false;
230+
}
231+
232+
void this.addFolderToIgnoreList(ctx.file.parent);
233+
},
234+
});
235+
236+
this.addCommand({
237+
id: 'ignore-file',
238+
name: getTextInLanguage('commands.ignore-file.name'),
239+
editorCheckCallback(checking, _, ctx) {
240+
if (checking && ctx.file) {
241+
return that.isMarkdownFile(ctx.file) && !that.shouldIgnoreFile(ctx.file);
242+
}
243+
244+
void this.addFileToIgnoreList(ctx.file);
245+
},
246+
icon: iconInfo.ignoreFile.id,
247+
});
218248
}
219249

220250
registerEventsAndSaveCallback() {
@@ -365,26 +395,43 @@ export default class LinterPlugin extends Plugin {
365395

366396
onMenuOpenCallback(menu: Menu, file: TAbstractFile, _source: string) {
367397
if (file instanceof TFile && this.isMarkdownFile(file)) {
368-
menu.addItem((item) => {
369-
item.setIcon(iconInfo.file.id)
370-
.setTitle(getTextInLanguage('commands.lint-file-pop-up-menu-text.name'))
371-
.onClick(() => {
372-
const activeFile = this.app.workspace.getActiveFile();
373-
const editor = this.getEditor();
374-
if (activeFile === file && editor && editor.cm) {
375-
void this.runLinterEditor(editor);
376-
} else {
377-
void this.runLinterFile(file);
378-
}
379-
});
380-
});
398+
if (!this.shouldIgnoreFile(file)) {
399+
menu.addItem((item) => {
400+
item.setIcon(iconInfo.file.id)
401+
.setTitle(getTextInLanguage('commands.lint-file-pop-up-menu-text.name'))
402+
.onClick(() => {
403+
const activeFile = this.app.workspace.getActiveFile();
404+
const editor = this.getEditor();
405+
if (activeFile === file && editor && editor.cm) {
406+
void this.runLinterEditor(editor);
407+
} else {
408+
void this.runLinterFile(file);
409+
}
410+
});
411+
});
412+
413+
menu.addItem((item) => {
414+
item.setIcon(iconInfo.ignoreFile.id)
415+
.setTitle(getTextInLanguage('commands.ignore-file-pop-up-menu-text.name'))
416+
.onClick(() => {
417+
void this.addFileToIgnoreList(file);
418+
});
419+
});
420+
}
381421
} else if (file instanceof TFolder) {
382-
menu.addItem((item) => {
383-
item
384-
.setTitle(getTextInLanguage('commands.lint-folder-pop-up-menu-text.name'))
385-
.setIcon(iconInfo.folder.id)
386-
.onClick(() => this.createFolderLintModal(file));
387-
});
422+
if (!this.settings.foldersToIgnore.includes(file.path)) {
423+
menu.addItem((item) => {
424+
item.setTitle(getTextInLanguage('commands.lint-folder-pop-up-menu-text.name'))
425+
.setIcon(iconInfo.folder.id)
426+
.onClick(() => this.createFolderLintModal(file));
427+
});
428+
429+
menu.addItem((item) => {
430+
item.setTitle(getTextInLanguage('commands.ignore-folder-pop-up-menu-text.name'))
431+
.setIcon(iconInfo.ignoreFolder.id)
432+
.onClick(() => void this.addFolderToIgnoreList(file));
433+
});
434+
}
388435
}
389436
}
390437

@@ -1118,4 +1165,20 @@ export default class LinterPlugin extends Plugin {
11181165
this.activeFileChangeDebouncer.get(file.path).originalText = newText;
11191166
}
11201167
}
1168+
1169+
private async addFileToIgnoreList(file: TFile) {
1170+
this.settings.filesToIgnore.push({
1171+
match: '^' + escapeRegExp(file.path) + '$',
1172+
flags: '',
1173+
label: '',
1174+
});
1175+
1176+
await this.saveSettings();
1177+
}
1178+
1179+
private async addFolderToIgnoreList(folder: TFolder) {
1180+
this.settings.foldersToIgnore.push(folder.path);
1181+
1182+
await this.saveSettings();
1183+
}
11211184
}

src/ui/icons.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,10 +136,27 @@ const successSVG = `<svg xmlns="http://www.w3.org/2000/svg" width="100" height="
136136
<polyline points="20 6 9 17 4 12"/>
137137
</svg>`;
138138

139+
// https://lucide.dev/icons/file-x
140+
const ignoreFileSVG = `<svg xmlns="http://www.w3.org/2000/svg" width="100" height="100" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-file-x">
141+
<path d="M15 2H6a2 2 0 0 0-2 2v16a2 2 0 0 0 2 2h12a2 2 0 0 0 2-2V7Z"/>
142+
<path d="M14 2v4a2 2 0 0 0 2 2h4"/>
143+
<path d="m14.5 12.5-5 5"/>
144+
<path d="m9.5 12.5 5 5"/>
145+
</svg>`;
146+
147+
// https://lucide.dev/icons/folder-x
148+
const ignoreFolderSVG = `<svg xmlns="http://www.w3.org/2000/svg" width="100" height="100" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round" stroke-linejoin="round" class="lucide lucide-folder-x">
149+
<path d="M20 20a2 2 0 0 0 2-2V8a2 2 0 0 0-2-2h-7.9a2 2 0 0 1-1.69-.9L9.6 3.9A2 2 0 0 0 7.93 3H4a2 2 0 0 0-2 2v13a2 2 0 0 0 2 2Z"/>
150+
<path d="m9.5 10.5 5 5"/>
151+
<path d="m14.5 10.5-5 5"/>
152+
</svg>`;
153+
139154
// exported SVG info
140155
export const iconInfo: Record<string, {id: string, source: string}> = {
141156
folder: {id: 'lint-folder', source: lintFolderSVG},
157+
ignoreFolder: {id: 'lint-ignore-folder', source: ignoreFolderSVG},
142158
file: {id: 'lint-file', source: lintFileSVG},
159+
ignoreFile: {id: 'lint-ignored-file', source: ignoreFileSVG},
143160
vault: {id: 'lint-vault', source: lintVaultSVG},
144161
whitespace: {id: 'lint-whitespace', source: whitespaceSVG},
145162
math: {id: 'lint-math', source: mathSVG},

0 commit comments

Comments
 (0)