Skip to content

Offline, privacy-first grammar checker. Fast, open-source, Rust-powered

feat(obsidian): added "add to dictionary" button

Elijah Potter9ff843f8e4ce
diff --git a/packages/harper.js/package.json b/packages/harper.js/package.json
index f910c3aed..888f74ded 100644
--- a/packages/harper.js/package.json
+++ b/packages/harper.js/package.json
@@ -1,36 +1,36 @@
{
- "name": "harper.js",
- "version": "0.21.1",
- "license": "Apache-2.0",
- "author": "Elijah Potter",
- "description": "The grammar checker for developers.",
- "repository": {
- "type": "git",
- "url": "git+https://github.com/automattic/harper.git",
- "directory": "packages/harper.js"
- },
- "bugs": {
- "url": "https://github.com/automattic/harper/issues"
- },
- "homepage": "https://writewithharper.com",
- "type": "module",
- "scripts": {
- "dev": "vite",
- "build": "tsc && vite build",
- "test": "vitest run --browser chromium"
- },
- "devDependencies": {
- "@microsoft/api-documenter": "^7.26.5",
- "@microsoft/api-extractor": "^7.49.1",
- "@vitest/browser": "^3.0.5",
- "playwright": "^1.49.1",
- "typescript": "~5.7.3",
- "vite": "^6.1.0",
- "vite-plugin-dts": "^4.5.0",
- "vite-plugin-virtual": "^0.3.0",
- "vitest": "^3.0.5",
- "wasm": "link:../../harper-wasm/pkg"
- },
- "main": "dist/harper.js",
- "types": "dist/harper.d.ts"
+ "name": "harper.js",
+ "version": "0.21.1",
+ "license": "Apache-2.0",
+ "author": "Elijah Potter",
+ "description": "The grammar checker for developers.",
+ "repository": {
+ "type": "git",
+ "url": "git+https://github.com/automattic/harper.git",
+ "directory": "packages/harper.js"
+ },
+ "bugs": {
+ "url": "https://github.com/automattic/harper/issues"
+ },
+ "homepage": "https://writewithharper.com",
+ "type": "module",
+ "scripts": {
+ "dev": "vite",
+ "build": "tsc && vite build",
+ "test": "vitest run --browser chromium"
+ },
+ "devDependencies": {
+ "@microsoft/api-documenter": "^7.26.5",
+ "@microsoft/api-extractor": "^7.49.1",
+ "@vitest/browser": "^3.0.5",
+ "playwright": "^1.49.1",
+ "typescript": "~5.7.3",
+ "vite": "^6.1.0",
+ "vite-plugin-dts": "^4.5.0",
+ "vite-plugin-virtual": "^0.3.0",
+ "vitest": "^3.0.5",
+ "wasm": "link:../../harper-wasm/pkg"
+ },
+ "main": "dist/harper.js",
+ "types": "dist/harper.d.ts"
}
diff --git a/packages/obsidian-plugin/src/index.ts b/packages/obsidian-plugin/src/index.ts
index 195ed081c..ad7bd289c 100644
--- a/packages/obsidian-plugin/src/index.ts
+++ b/packages/obsidian-plugin/src/index.ts
@@ -11,9 +11,9 @@ function suggestionToLabel(sug: Suggestion) {
if (sug.kind() == SuggestionKind.Remove) {
return 'Remove';
} else if (sug.kind() == SuggestionKind.Replace) {
- return `Replace with "${sug.get_replacement_text()}"`;
+ return `Replace with “${sug.get_replacement_text()}”`;
} else if (sug.kind() == SuggestionKind.InsertAfter) {
- return `Insert "${sug.get_replacement_text()}" after this.`;
+ return `Insert “${sug.get_replacement_text()}” after this.`;
}
}
@@ -21,6 +21,7 @@ export type Settings = {
ignoredLints?: string;
useWebWorker: boolean;
lintSettings: LintConfig;
+ userDictionary?: string[];
};
export default class HarperPlugin extends Plugin {
@@ -54,6 +55,10 @@ export default class HarperPlugin extends Plugin {
await this.harper.importIgnoredLints(settings.ignoredLints);
}
+ if (settings.userDictionary != null && settings.userDictionary.length > 0) {
+ await this.harper.importWords(settings.userDictionary);
+ }
+
await this.harper.setLintConfig(settings.lintSettings);
this.harper.setup();
@@ -77,7 +82,8 @@ export default class HarperPlugin extends Plugin {
return {
ignoredLints: await this.harper.exportIgnoredLints(),
useWebWorker: usingWebWorker,
- lintSettings: await this.harper.getLintConfig()
+ lintSettings: await this.harper.getLintConfig(),
+ userDictionary: await this.harper.exportWords()
};
}
@@ -178,6 +184,51 @@ export default class HarperPlugin extends Plugin {
span.start = charIndexToCodePointIndex(span.start, chars);
span.end = charIndexToCodePointIndex(span.end, chars);
+ const actions = lint.suggestions().map((sug) => {
+ return {
+ name: suggestionToLabel(sug),
+ apply: (view) => {
+ if (sug.kind() === SuggestionKind.Remove) {
+ view.dispatch({
+ changes: {
+ from: span.start,
+ to: span.end,
+ insert: ''
+ }
+ });
+ } else if (sug.kind() === SuggestionKind.Replace) {
+ view.dispatch({
+ changes: {
+ from: span.start,
+ to: span.end,
+ insert: sug.get_replacement_text()
+ }
+ });
+ } else if (sug.kind() === SuggestionKind.InsertAfter) {
+ view.dispatch({
+ changes: {
+ from: span.end,
+ to: span.end,
+ insert: sug.get_replacement_text()
+ }
+ });
+ }
+ }
+ };
+ });
+
+ if (lint.lint_kind() == 'Spelling') {
+ const word = lint.get_problem_text();
+
+ actions.push({
+ name: `Add “${word}” to your dictionary`,
+ apply: (view) => {
+ this.harper.importWords([word]);
+ this.reinitialize();
+ }
+ });
+ }
+
return {
from: span.start,
to: span.end,
@@ -188,38 +239,7 @@ export default class HarperPlugin extends Plugin {
await this.harper.ignoreLint(lint);
await this.reinitialize();
},
- actions: lint.suggestions().map((sug) => {
- return {
- name: suggestionToLabel(sug),
- apply: (view) => {
- if (sug.kind() === SuggestionKind.Remove) {
- view.dispatch({
- changes: {
- from: span.start,
- to: span.end,
- insert: ''
- }
- });
- } else if (sug.kind() === SuggestionKind.Replace) {
- view.dispatch({
- changes: {
- from: span.start,
- to: span.end,
- insert: sug.get_replacement_text()
- }
- });
- } else if (sug.kind() === SuggestionKind.InsertAfter) {
- view.dispatch({
- changes: {
- from: span.end,
- to: span.end,
- insert: sug.get_replacement_text()
- }
- });
- }
- }
- };
- })
+ actions
};
});
},