Skip to content

Commit

Permalink
Implemented custom regular replacement according to the UI of custom …
Browse files Browse the repository at this point in the history
…command
  • Loading branch information
HardwayLinka committed Oct 24, 2022
1 parent 5348abc commit 4c16e52
Show file tree
Hide file tree
Showing 4 changed files with 79 additions and 3 deletions.
6 changes: 6 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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
Expand Down
20 changes: 18 additions & 2 deletions src/rules-runner.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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;

Expand Down
3 changes: 3 additions & 0 deletions src/rules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -38,6 +40,7 @@ export interface LinterSettings {
linterLocale: string;
logLevel: number;
lintCommands: LintCommand[];
customRegexs: CustomRegex[];
commonStyles: CommonStyles;
}

Expand Down
53 changes: 52 additions & 1 deletion src/ui/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down Expand Up @@ -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';
Expand Down

0 comments on commit 4c16e52

Please sign in to comment.