Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move Default Auto-Correct Misspellings to Separate File #1185

Merged
merged 6 commits into from
Oct 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ jobs:
- name: Create Manual Install Zip
run: |
mkdir ${{ env.PLUGIN_NAME }}
cp main.js manifest.json src/styles.css ${{ env.PLUGIN_NAME }}
cp main.js manifest.json src/styles.css src/utils/default-misspellings.md ${{ env.PLUGIN_NAME }}
zip -r ${{ env.PLUGIN_NAME }}.zip ${{ env.PLUGIN_NAME }}

- name: Create release
Expand Down
12 changes: 11 additions & 1 deletion __tests__/auto-correct-common-misspellings.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import AutoCorrectCommonMisspellings from '../src/rules/auto-correct-common-misspellings';
import dedent from 'ts-dedent';
import {ruleTest} from './common';
import {defaultMisspellings, ruleTest} from './common';

ruleTest({
RuleBuilderClass: AutoCorrectCommonMisspellings,
Expand All @@ -15,6 +15,9 @@ ruleTest({
[[absoltely not a changed]]
[absoltely not a changed](absoltely.not.changed.md)
`,
options: {
misspellingToCorrection: defaultMisspellings(),
},
},
{
testName: 'Doesn\'t auto-correct markdown and wiki images',
Expand All @@ -26,6 +29,9 @@ ruleTest({
![[absoltely.not.a.changed.jpg]]
![absoltely not a changed](absoltely.not.changed.md)
`,
options: {
misspellingToCorrection: defaultMisspellings(),
},
},
{
testName: 'Doesn\'t auto-correct words that start with unicode specific characters when not in correction list',
Expand All @@ -35,6 +41,9 @@ ruleTest({
after: dedent`
être
`,
options: {
misspellingToCorrection: defaultMisspellings(),
},
},
{
testName: 'Custom replacements should work on file content',
Expand All @@ -45,6 +54,7 @@ ruleTest({
The cart is over there.
`,
options: {
misspellingToCorrection: defaultMisspellings(),
extraAutoCorrectFiles: [{
filePath: 'file_path',
customReplacements: new Map<string, string>([['cartt', 'cart'], ['theree', 'there']]),
Expand Down
13 changes: 13 additions & 0 deletions __tests__/common.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
import {readFileSync} from 'fs';
import {Options} from '../src/rules';
import RuleBuilder, {RuleBuilderBase} from '../src/rules/rule-builder';
import {parseCustomReplacements} from '../src/utils/strings';

let defaultMap: Map<string, string>;

export function defaultMisspellings(): Map<string, string> {
if (!defaultMap) {
const data = readFileSync('src/utils/default-misspellings.md', 'utf8');
defaultMap = parseCustomReplacements(data);
}

return defaultMap;
}

type TestCase<TOptions extends Options> = {
testName: string,
Expand Down
18 changes: 16 additions & 2 deletions __tests__/examples.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,19 @@ import dedent from 'ts-dedent';
import {Example, rules, RuleType} from '../src/rules';
import {yamlRegex, escapeRegExp} from '../src/utils/regex';
import '../src/rules-registry';
import {defaultMisspellings} from './common';

describe('Examples pass', () => {
for (const rule of rules) {
describe(rule.getName(), () => {
test.each(rule.examples)('$description', (example: Example) => {
expect(rule.apply(example.before, example.options)).toBe(example.after);
const options = example.options;
// add default misspellings for auto-correct
if (rule.alias == 'auto-correct-common-misspellings') {
options.misspellingToCorrection = defaultMisspellings();
}

expect(rule.apply(example.before, options)).toBe(example.after);
});
});
}
Expand All @@ -27,7 +34,14 @@ describe('Augmented examples pass', () => {
`;

const before = yaml + example.before;
expect(rule.apply(before, example.options)).toMatch(new RegExp(`${escapeRegExp(yaml)}\n?${escapeRegExp(example.after)}`));

const options = example.options;
// add default misspellings for auto-correct
if (rule.alias == 'auto-correct-common-misspellings') {
options.misspellingToCorrection = defaultMisspellings();
}

expect(rule.apply(before, options)).toMatch(new RegExp(`${escapeRegExp(yaml)}\n?${escapeRegExp(example.after)}`));
}
});
});
Expand Down
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion src/lang/locale/de.ts
Original file line number Diff line number Diff line change
Expand Up @@ -224,7 +224,7 @@ export default {
// auto-correct-common-misspellings.ts
'auto-correct-common-misspellings': {
'name': 'Häufige Rechtschreibfehler automatisch korrigieren',
'description': 'Verwendet ein Wörterbuch mit häufigen Rechtschreibfehlern, um sie automatisch in die richtige Schreibweise umzuwandeln. Siehe [Autokorrekturkarte](https://github.com/platers/obsidian-linter/tree/master/src/utils/auto-correct-misspellings.ts) für die vollständige Liste der automatisch korrigierten Wörter.',
'description': 'Verwendet ein Wörterbuch mit häufigen Rechtschreibfehlern, um sie automatisch in die richtige Schreibweise umzuwandeln. Siehe [Autokorrekturkarte](https://github.com/platers/obsidian-linter/tree/master/src/utils/default-misspellings.md) für die vollständige Liste der automatisch korrigierten Wörter.',
'ignore-words': {
'name': 'Ignorieren Sie Wörter',
'description': 'Eine durch Kommas getrennte Liste von Wörtern in Kleinbuchstaben, die bei der automatischen Korrektur ignoriert werden sollen',
Expand Down
5 changes: 4 additions & 1 deletion src/lang/locale/en.ts
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ export default {
// auto-correct-common-misspellings.ts
'auto-correct-common-misspellings': {
'name': 'Auto-correct Common Misspellings',
'description': 'Uses a dictionary of common misspellings to automatically convert them to their proper spellings. See [auto-correct map](https://github.com/platers/obsidian-linter/tree/master/src/utils/auto-correct-misspellings.ts) for the full list of auto-corrected words. **Note: this list can work on text from multiple languages, but this list is the same no matter what language is currently in use.**',
'description': 'Uses a dictionary of common misspellings to automatically convert them to their proper spellings. See [auto-correct map](https://github.com/platers/obsidian-linter/tree/master/src/utils/default-misspellings.md) for the full list of auto-corrected words. **Note: this list can work on text from multiple languages, but this list is the same no matter what language is currently in use.**',
'ignore-words': {
'name': 'Ignore Words',
'description': 'A comma separated list of lowercased words to ignore when auto-correcting',
Expand All @@ -276,6 +276,9 @@ export default {
'name': 'Skip Words with Multiple Capitals',
'description': 'Will skip any files that have a capital letter in them other than as the first letter of the word. Acronyms and some other words can benefit from this. It may cause issues with proper nouns being properly fixed.',
},
'default-install': 'You are using Auto-correct Common Misspellings. In order to do so, the default misspellings will be downloaded. This should only happen once. Please wait...',
'default-install-failed': 'Failed to download {URL}. Disabling Auto-correct Common Misspellings.',
'defaults-missing': 'Failed to find default common auto-correct file: {FILE}.',
},
// add-blank-line-after-yaml.ts
'add-blank-line-after-yaml': {
Expand Down
2 changes: 1 addition & 1 deletion src/lang/locale/es.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ export default {
'rules': {
'auto-correct-common-misspellings': {
'name': 'Corrección automática de errores ortográficos comunes',
'description': 'Utiliza un diccionario de errores ortográficos comunes para convertirlos automáticamente a su ortografía correcta. Consulte [mapa de autocorrección](https://github.com/platers/obsidian-linter/tree/master/src/utils/auto-correct-misspellings.ts) para obtener la lista completa de palabras corregidas automáticamente. **Nota: esta lista puede funcionar en texto de varios idiomas, pero esta lista es la misma sin importar qué idioma esté en uso actualmente.**',
'description': 'Utiliza un diccionario de errores ortográficos comunes para convertirlos automáticamente a su ortografía correcta. Consulte [mapa de autocorrección](https://github.com/platers/obsidian-linter/tree/master/src/utils/default-misspellings.md) para obtener la lista completa de palabras corregidas automáticamente. **Nota: esta lista puede funcionar en texto de varios idiomas, pero esta lista es la misma sin importar qué idioma esté en uso actualmente.**',
'ignore-words': {
'name': 'Ignorar palabras',
'description': 'Una lista separada por comas de palabras en minúsculas para ignorar al corregir automáticamente',
Expand Down
2 changes: 1 addition & 1 deletion src/lang/locale/tr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ export default {
// auto-correct-common-misspellings.ts
'auto-correct-common-misspellings': {
'name': 'Yaygın Yanlış Yazımları Otomatik Düzelt',
'description': 'Yaygın yanlış yazımların sözlüğünü kullanarak bunları doğru yazımlarına otomatik olarak dönüştürür. Otomatik düzeltilen kelimelerin tam listesi için [otomatik-düzeltme haritasına](https://github.com/platers/obsidian-linter/tree/master/src/utils/auto-correct-misspellings.ts) bakın.',
'description': 'Yaygın yanlış yazımların sözlüğünü kullanarak bunları doğru yazımlarına otomatik olarak dönüştürür. Otomatik düzeltilen kelimelerin tam listesi için [otomatik-düzeltme haritasına](https://github.com/platers/obsidian-linter/tree/master/src/utils/default-misspellings.md) bakın.',
'ignore-words': {
'name': 'Kelimeleri Yoksay',
'description': 'Otomatik düzeltme sırasında yoksayılacak küçük harfli kelimelerin virgülle ayrılmış listesi',
Expand Down
2 changes: 1 addition & 1 deletion src/lang/locale/zh-cn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ export default {
// auto-correct-common-misspellings.ts
'auto-correct-common-misspellings': {
'name': '自动更正常见的拼写错误',
'description': '通过常见拼写错误字典自动将错误拼写更正为正确拼写。有关自动更正单词的完整列表,请参阅 [auto-correct map](https://github.com/platers/obsidian-linter/tree/master/src/utils/auto-correct-misspellings.ts)',
'description': '通过常见拼写错误字典自动将错误拼写更正为正确拼写。有关自动更正单词的完整列表,请参阅 [auto-correct map](https://github.com/platers/obsidian-linter/tree/master/src/utils/default-misspellings.md)',
'ignore-words': {
'name': '忽略单词',
'description': '以逗号分隔的小写单词列表,在自动更正时会忽略',
Expand Down
75 changes: 48 additions & 27 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ import AsyncLock from 'async-lock';
import {warn} from 'loglevel';
import {CustomAutoCorrectContent} from './ui/linter-components/auto-correct-files-picker-option';
import {ChangeSpec} from '@codemirror/state';
import {downloadMisspellings, readInMisspellingsFile} from './utils/auto-correct-misspellings';

// https://github.com/liamcain/obsidian-calendar-ui/blob/03ceecbf6d88ef260dadf223ee5e483d98d24ffc/src/localization.ts#L20-L43
const langToMomentLocale = {
Expand Down Expand Up @@ -64,7 +65,6 @@ export default class LinterPlugin extends Plugin {
private lastActiveFile: TFile;
private overridePaste: boolean = false;
private hasCustomCommands: boolean = false;
private hasLoadedFiles: boolean = false;
private customCommandsLock = new AsyncLock();
private originalSaveCallback?: () => void = null;
// The amount of files you can use editor lint on at once is pretty small, so we will use an array
Expand All @@ -75,6 +75,8 @@ export default class LinterPlugin extends Plugin {
private customCommandsCallback: (file: TFile) => Promise<void> = null;
private currentlyOpeningSidebar: boolean = false;
private activeFileChangeDebouncer: Map<TFile, FileChangeUpdateInfo> = new Map();
private defaultAutoCorrectMisspellings: Map<string, string> = new Map();
private hasLoadedMisspellingFiles = false;

async onload() {
setLanguage(window.localStorage.getItem('language'));
Expand Down Expand Up @@ -171,6 +173,10 @@ export default class LinterPlugin extends Plugin {
}

async saveSettings() {
if (!this.hasLoadedMisspellingFiles) {
await this.loadAutoCorrectFiles();
}

await this.saveData(this.settings);
this.updatePasteOverrideStatus();
this.updateHasCustomCommandStatus();
Expand Down Expand Up @@ -310,6 +316,10 @@ export default class LinterPlugin extends Plugin {
this.registerEvent(eventRef);
this.eventRefs.push(eventRef);

this.app.workspace.onLayoutReady(async () => {
await this.loadAutoCorrectFiles();
});

// Source for save setting
// https://github.com/hipstersmoothie/obsidian-plugin-prettier/blob/main/src/main.ts
const saveCommandDefinition = this.app.commands?.commands?.[
Expand Down Expand Up @@ -354,6 +364,38 @@ export default class LinterPlugin extends Plugin {
}
}

async loadAutoCorrectFiles() {
const customAutoCorrectSettings = this.settings.ruleConfigs['auto-correct-common-misspellings'];
if (!customAutoCorrectSettings || !customAutoCorrectSettings.enabled) {
return;
}

await downloadMisspellings(this, async (message: string) => {
new Notice(message);

this.settings.ruleConfigs['auto-correct-common-misspellings'].enabled = false;
await this.saveSettings();
});

if (!this.settings.ruleConfigs['auto-correct-common-misspellings'].enabled) {
return;
}

this.defaultAutoCorrectMisspellings = parseCustomReplacements(stripCr(await readInMisspellingsFile(this)));

// load custom-auto-correct replacements if they exist
for (const replacementFileInfo of this.settings.ruleConfigs['auto-correct-common-misspellings']['extra-auto-correct-files'] ?? [] as CustomAutoCorrectContent[]) {
if (replacementFileInfo.filePath != '') {
const file = this.getFileFromPath(replacementFileInfo.filePath);
if (file) {
replacementFileInfo.customReplacements = parseCustomReplacements(stripCr(await this.app.vault.cachedRead(file)));
}
}
}

this.hasLoadedMisspellingFiles = true;
}

onMenuOpenCallback(menu: Menu, file: TAbstractFile, _source: string) {
if (file instanceof TFile && this.isMarkdownFile(file)) {
menu.addItem((item) => {
Expand Down Expand Up @@ -426,12 +468,8 @@ export default class LinterPlugin extends Plugin {
}

async runLinterFile(file: TFile, lintingLastActiveFile: boolean = false) {
if (!this.hasLoadedFiles) {
await this.loadCustomReplacements();
}

const oldText = stripCr(await this.app.vault.read(file));
const newText = this.rulesRunner.lintText(createRunLinterRulesOptions(oldText, file, this.momentLocale, this.settings));
const newText = this.rulesRunner.lintText(createRunLinterRulesOptions(oldText, file, this.momentLocale, this.settings, this.defaultAutoCorrectMisspellings));

if (oldText != newText) {
await this.app.vault.modify(file, newText);
Expand Down Expand Up @@ -520,15 +558,11 @@ export default class LinterPlugin extends Plugin {

logInfo(getTextInLanguage('logs.linter-run'));

if (!this.hasLoadedFiles) {
await this.loadCustomReplacements();
}

const file = this.app.workspace.getActiveFile();
const oldText = editor.getValue();
let newText: string;
try {
newText = this.rulesRunner.lintText(createRunLinterRulesOptions(oldText, file, this.momentLocale, this.settings));
newText = this.rulesRunner.lintText(createRunLinterRulesOptions(oldText, file, this.momentLocale, this.settings, this.defaultAutoCorrectMisspellings));
} catch (error) {
this.handleLintError(file, error, getTextInLanguage('commands.lint-file.error-message') + ' \'{FILE_PATH}\'', false);
return;
Expand Down Expand Up @@ -606,7 +640,7 @@ export default class LinterPlugin extends Plugin {
if (oldText != activeFileChangeInfo.originalText ) {
logInfo(getTextInLanguage('logs.file-change-yaml-lint-run'));
try {
newText = this.rulesRunner.runYAMLTimestampByItself(createRunLinterRulesOptions(oldText, file, this.momentLocale, this.settings));
newText = this.rulesRunner.runYAMLTimestampByItself(createRunLinterRulesOptions(oldText, file, this.momentLocale, this.settings, null));
} catch (error) {
this.handleLintError(file, error, getTextInLanguage('commands.lint-file.error-message') + ' \'{FILE_PATH}\'', false);
return;
Expand Down Expand Up @@ -730,7 +764,7 @@ export default class LinterPlugin extends Plugin {
const cursorSelection = cursorSelections[0];
clipboardText = this.rulesRunner.runPasteLint(this.getLineContent(editor, cursorSelection),
editor.getSelection() ?? '',
createRunLinterRulesOptions(clipboardText, null, this.momentLocale, this.settings),
createRunLinterRulesOptions(clipboardText, null, this.momentLocale, this.settings, null),
);

editor.replaceSelection(clipboardText);
Expand All @@ -750,7 +784,7 @@ export default class LinterPlugin extends Plugin {
const editorChange: EditorChange[] = [];

cursorSelections.forEach((cursorSelection: EditorSelection, index: number) => {
clipboardText = this.rulesRunner.runPasteLint(this.getLineContent(editor, cursorSelection), editor.getRange(cursorSelection.anchor, cursorSelection.head) ?? '', createRunLinterRulesOptions(pasteContentPerCursor[index], null, this.momentLocale, this.settings));
clipboardText = this.rulesRunner.runPasteLint(this.getLineContent(editor, cursorSelection), editor.getRange(cursorSelection.anchor, cursorSelection.head) ?? '', createRunLinterRulesOptions(pasteContentPerCursor[index], null, this.momentLocale, this.settings, null));
editorChange.push({
text: clipboardText,
from: cursorSelection.anchor,
Expand Down Expand Up @@ -953,19 +987,6 @@ export default class LinterPlugin extends Plugin {
return {line: lines.length - 1, ch: lines[lines.length - 1].length};
}

private async loadCustomReplacements() {
for (const replacementFileInfo of this.settings.ruleConfigs['auto-correct-common-misspellings']['extra-auto-correct-files'] ?? [] as CustomAutoCorrectContent[]) {
if (replacementFileInfo.filePath != '') {
const file = this.getFileFromPath(replacementFileInfo.filePath);
if (file) {
replacementFileInfo.customReplacements = parseCustomReplacements(stripCr(await this.app.vault.cachedRead(file)));
}
}
}

this.hasLoadedFiles = true;
}

private getFileFromPath(filePath: string): TFile {
const file = this.app.vault.getAbstractFileByPath(normalizePath(filePath));
if (file instanceof TFile) {
Expand Down
Loading
Loading