diff --git a/src/main.ts b/src/main.ts index c02b7063..46191571 100644 --- a/src/main.ts +++ b/src/main.ts @@ -180,6 +180,7 @@ export default class LinterPlugin extends Plugin { linterLocale: 'system-default', logLevel: log.levels.ERROR, lintCommands: [], + customRegexs: [], commonStyles: { aliasArrayStyle: NormalArrayFormats.SingleLine, tagArrayStyle: NormalArrayFormats.SingleLine, @@ -221,6 +222,9 @@ export default class LinterPlugin extends Plugin { if (Object.prototype.hasOwnProperty.call(storedSettings, 'lintCommands')) { this.settings.lintCommands = storedSettings.lintCommands; } + if (Object.prototype.hasOwnProperty.call(storedSettings, 'customRegexs')) { + this.settings.customRegexs = storedSettings.customRegexs; + } if (Object.prototype.hasOwnProperty.call(storedSettings, 'commonStyles')) { this.settings.commonStyles = storedSettings.commonStyles; } @@ -370,6 +374,8 @@ export default class LinterPlugin extends Plugin { } catch (error) { this.handleLintError(file, error, 'Lint File Error in File \'${file.path}\'', false); } + + this.rulesRunner.runCustomRegexReplacement(this.settings.customRegexs, editor); } // based on https://github.com/liamcain/obsidian-calendar-ui/blob/03ceecbf6d88ef260dadf223ee5e483d98d24ffc/src/localization.ts#L85-L109 diff --git a/src/rules-runner.ts b/src/rules-runner.ts index 3eb427f9..95e6f31c 100644 --- a/src/rules-runner.ts +++ b/src/rules-runner.ts @@ -1,6 +1,6 @@ -import {TFile, moment} from 'obsidian'; +import {TFile, moment, Editor} from 'obsidian'; import {logDebug, logWarn} from './logger'; -import {getDisabledRules, LinterSettings, rules, wrapLintError, LintCommand, RuleType} from './rules'; +import {getDisabledRules, LinterSettings, rules, wrapLintError, LintCommand, CustomRegex, RuleType} from './rules'; import BlockquotifyOnPaste from './rules/blockquotify-on-paste'; import EscapeYamlSpecialCharacters from './rules/escape-yaml-special-characters'; import FormatTagsInYaml from './rules/format-tags-in-yaml'; @@ -120,6 +120,22 @@ export class RulesRunner { } } + runCustomRegexReplacement(customRegexs: CustomRegex[], editor: Editor) { + logDebug(`Running Custom Lint Commands`); + for (const eachRegex of customRegexs) { + if (!eachRegex.find || eachRegex.find.trim() == '' || !eachRegex.replace || eachRegex.replace.trim() == '') { + continue; + } + const regex = new RegExp(`${eachRegex.find}`, 'gm'); + const oldText = editor.getValue(); + const newText = oldText.replace(regex, eachRegex.replace); + const lines = oldText.split('\n'); + const start = {line: 0, ch: 0}; + const end = {line: lines.length - 1, ch: lines[lines.length - 1].length}; + editor.replaceRange(newText, start, end, oldText); + } + } + runPasteLint(currentLine: string, runOptions: RunLinterRulesOptions): string { let newText = runOptions.oldText; diff --git a/src/rules.ts b/src/rules.ts index 0767b861..17af39c4 100644 --- a/src/rules.ts +++ b/src/rules.ts @@ -28,6 +28,8 @@ type ApplyFunction = (text: string, options?: Options) => string; export type LintCommand = {id: string, name: string}; +export type CustomRegex = {find: string, replace: string}; + export interface LinterSettings { ruleConfigs: { [ruleName: string]: Options; @@ -38,6 +40,7 @@ export interface LinterSettings { linterLocale: string; logLevel: number; lintCommands: LintCommand[]; + customRegexs: CustomRegex[]; commonStyles: CommonStyles; } diff --git a/src/ui/settings.ts b/src/ui/settings.ts index 2a9c4396..00835a01 100644 --- a/src/ui/settings.ts +++ b/src/ui/settings.ts @@ -59,7 +59,10 @@ export class SettingTab extends PluginSettingTab { this.addRuleToTab(tabTitle, rule); } - this.createTabAndContent('Custom', navEl, settingsEl, (el: HTMLElement, tabName: string) => this.generateCustomCommandSettings(tabName, el)); + this.createTabAndContent('Custom', navEl, settingsEl, (el: HTMLElement, tabName: string) => { + this.generateCustomCommandSettings(tabName, el); + this.generateCustomRegexReplacementSettings(tabName, el); + }); this.createSearchZeroState(settingsEl); } @@ -238,6 +241,54 @@ export class SettingTab extends PluginSettingTab { }); } + generateCustomRegexReplacementSettings(tabName: string, containerEl: HTMLElement): void { + containerEl.createEl(Platform.isMobile ? 'h4' : 'h3', {text: 'Custom Regex Replacement'}); + + new Setting(containerEl) + .addButton((cb)=>{ + cb.setButtonText('Add new regex') + .setCta() + .onClick(()=>{ + this.plugin.settings.customRegexs.push({find: '', replace: ''}); + this.plugin.saveSettings(); + this.display(); + const customRegexInputBox = document.getElementsByClassName('linter-custom-regex-replacement'); + // @ts-ignore + customRegexInputBox[customRegexInputBox.length-1].focus(); + }); + }); + + this.plugin.settings.customRegexs.forEach((regex, index) => { + new Setting(containerEl) + .addText((cb) => { + cb.setPlaceholder('regex to find') + .setValue(regex.find) + .onChange((value) => { + this.plugin.settings.customRegexs[index].find = value; + this.plugin.saveSettings(); + }); + cb.inputEl.setAttr('inputIndex', index); + cb.inputEl.addClass('linter-custom-regex-replacement'); + }) + .addText((cb) => { + cb.setPlaceholder('regex to replace') + .setValue(regex.replace) + .onChange((value) => { + this.plugin.settings.customRegexs[index].replace = value; + this.plugin.saveSettings(); + }); + }).addExtraButton((cb)=>{ + cb.setIcon('cross') + .setTooltip('Delete') + .onClick(()=>{ + this.plugin.settings.customRegexs.splice(index, 1); + this.plugin.saveSettings(); + this.display(); + }); + }); + }); + } + generateGeneralSettings(tabName: string, containerEl: HTMLElement) { let tempDiv = containerEl.createDiv(); let settingName = 'Lint on save';