Skip to content

Commit

Permalink
fix(obsidian-plugin): use harper.js
Browse files Browse the repository at this point in the history
  • Loading branch information
elijah-potter committed Dec 19, 2024
1 parent d49cbe8 commit 0ba8595
Show file tree
Hide file tree
Showing 4 changed files with 11 additions and 13 deletions.
1 change: 1 addition & 0 deletions justfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ format:
build-wasm target:
cd "{{justfile_directory()}}/harper-wasm" && wasm-pack build --target {{target}}

# Build `harper.js` with all size optimizations available.
build-harperjs:
#! /bin/bash
set -eo pipefail
Expand Down
2 changes: 1 addition & 1 deletion packages/obsidian-plugin/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,6 @@
"dependencies": {
"crelt": "^1.0.5",
"lodash-es": "^4.17.21",
"wasm": "link:../../harper-wasm/pkg"
"harper.js": "link:../harper.js"
}
}
4 changes: 2 additions & 2 deletions packages/obsidian-plugin/src/HarperSettingTab.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ function valueToString(value) {
return 'enable';
case false:
return 'disable';
case undefined:
case null:
return 'default';
}

Expand All @@ -74,7 +74,7 @@ function stringToValue(str) {
case 'disable':
return false;
case 'default':
return undefined;
return null;
}

throw 'Fell through case';
Expand Down
17 changes: 7 additions & 10 deletions packages/obsidian-plugin/src/index.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import logoSvg from '../logo.svg';
import { linter } from './lint';
import { Plugin, addIcon, Menu } from 'obsidian';
import wasm from 'wasm/harper_wasm_bg.wasm';
import init, { lint, get_lint_config_as_object, set_lint_config_from_object } from 'wasm';
import { WorkerLinter } from 'harper.js';
import { HarperSettingTab } from './HarperSettingTab';

function suggestionToLabel(sug) {
Expand All @@ -13,6 +12,9 @@ function suggestionToLabel(sug) {
}
}

let harper = new WorkerLinter();
harper.setup();

const harperLinter = (plugin) =>
linter(
async (view) => {
Expand All @@ -22,9 +24,7 @@ const harperLinter = (plugin) =>

const text = view.state.doc.sliceString(-1);

await init(await wasm());

const lints = lint(text);
const lints = await harper.lint(text);

return lints.map((lint) => {
let span = lint.span();
Expand Down Expand Up @@ -81,10 +81,9 @@ export default class HarperPlugin extends Plugin {
/** @public
* @returns {Promise<Record<string, any>>} */
async getSettings() {
await init(await wasm());
this.lintSettingChanged();

let lintSettings = await get_lint_config_as_object();
let lintSettings = await harper.getLintConfig();

return { lintSettings };
}
Expand All @@ -93,8 +92,6 @@ export default class HarperPlugin extends Plugin {
* @param {Record<string, any>} settings
* @returns {Promise<void>} */
async setSettings(settings) {
await init(await wasm());

if (settings == null) {
settings = {};
}
Expand All @@ -107,7 +104,7 @@ export default class HarperPlugin extends Plugin {
settings.lintSettings.spell_check = false;
}

set_lint_config_from_object(settings.lintSettings);
await harper.setLintConfig(settings.lintSettings);
this.lintSettingChanged();
this.saveData(settings);
}
Expand Down

0 comments on commit 0ba8595

Please sign in to comment.