Offline, privacy-first grammar checker. Fast, open-source, Rust-powered
feat: created lint configuration settings page for Obsidian plugin
Elijah Potter670d7be5608d
diff --git a/Cargo.lock b/Cargo.lockindex 3cf8910b9..4521103de 100644--- a/Cargo.lock+++ b/Cargo.lock@@ -667,6 +667,7 @@ dependencies = ["console_error_panic_hook","harper-core","once_cell",+ "serde-wasm-bindgen","tracing","tracing-wasm","wasm-bindgen",@@ -1235,6 +1236,17 @@ dependencies = ["serde_derive",]+[[package]]+name = "serde-wasm-bindgen"+version = "0.6.5"+source = "registry+https://github.com/rust-lang/crates.io-index"+checksum = "8302e169f0eddcc139c70f139d19d6467353af16f9fce27e8c30158036a1e16b"+dependencies = [+ "js-sys",+ "serde",+ "wasm-bindgen",+]+[[package]]name = "serde_derive"version = "1.0.203"diff --git a/harper-wasm/Cargo.toml b/harper-wasm/Cargo.tomlindex 43c395402..7b0e4e783 100644--- a/harper-wasm/Cargo.toml+++ b/harper-wasm/Cargo.toml@@ -16,3 +16,4 @@ tracing-wasm = "0.2.1"wasm-bindgen = "0.2.91"harper-core = { path = "../harper-core", version = "0.9.0", features = ["concurrent"] }once_cell = "1.19.0"+serde-wasm-bindgen = "0.6.5"diff --git a/harper-wasm/src/lib.rs b/harper-wasm/src/lib.rsindex 72921e672..4ce6a5cec 100644--- a/harper-wasm/src/lib.rs+++ b/harper-wasm/src/lib.rs@@ -2,17 +2,20 @@use std::sync::Mutex;-use harper_core::linting::{LintGroup, Linter};+use harper_core::linting::{LintGroup, LintGroupConfig, Linter};use harper_core::parsers::Markdown;use harper_core::{remove_overlaps, Document, FullDictionary, Lrc};use once_cell::sync::Lazy;use wasm_bindgen::prelude::wasm_bindgen;+use wasm_bindgen::JsValue;static LINTER: Lazy<Mutex<LintGroup<Lrc<FullDictionary>>>> = Lazy::new(|| {- Mutex::new(LintGroup::new(- Default::default(),- FullDictionary::curated()- ))+ let lint_config = LintGroupConfig {+ spell_check: Some(false),+ ..Default::default()+ };++ Mutex::new(LintGroup::new(lint_config, FullDictionary::curated()))});/// Setup the WebAssembly module's logging.@@ -34,6 +37,19 @@ pub fn use_spell_check(set: bool) {linter.config.spell_check = Some(set);}+#[wasm_bindgen]+pub fn get_lint_config_as_object() -> JsValue {+ let linter = LINTER.lock().unwrap();+ serde_wasm_bindgen::to_value(&linter.config).unwrap()+}++#[wasm_bindgen]+pub fn set_lint_config_from_object(object: JsValue) -> Result<(), String> {+ let mut linter = LINTER.lock().unwrap();+ linter.config = serde_wasm_bindgen::from_value(object).map_err(|v| v.to_string())?;+ Ok(())+}+/// Perform the configured linting on the provided text.#[wasm_bindgen]pub fn lint(text: String) -> Vec<Lint> {diff --git a/packages/obsidian-plugin/src/HarperSettingTab.js b/packages/obsidian-plugin/src/HarperSettingTab.jsnew file mode 100644index 000000000..91568f560--- /dev/null+++ b/packages/obsidian-plugin/src/HarperSettingTab.js@@ -0,0 +1,80 @@+import HarperPlugin from './index.js';+import { App, PluginSettingTab, Setting } from 'obsidian';++export class HarperSettingTab extends PluginSettingTab {+ /** @type HarperPlugin+ * @private */+ plugin;++ /** @type Record<string, any> */+ settings;++ /** @param {App} app+ * @param {HarperPlugin} plugin */+ constructor(app, plugin) {+ super(app, plugin);+ this.plugin = plugin;++ this.updateSettings();+ }++ updateSettings() {+ this.plugin.getSettings().then((v) => (this.settings = v));+ }++ display() {+ const { containerEl } = this;+ containerEl.empty();++ console.log(this.settings.lintSettings);++ for (let setting of Object.keys(this.settings.lintSettings)) {+ let value = this.settings.lintSettings[setting];++ new Setting(containerEl)+ .setName(setting)+ .setDesc(`Whether to include the ${setting} grammar rule.`)+ .addDropdown((dropdown) =>+ dropdown+ .addOption('default', 'Default')+ .addOption('enable', 'On')+ .addOption('disable', 'Off')+ .setValue(valueToString(value))+ .onChange(async (value) => {+ this.settings.lintSettings[setting] = stringToValue(value);+ await this.plugin.setSettings(this.settings);+ })+ );+ }+ }+}++/** @param {boolean | undefined} value+ * @returns {string} */+function valueToString(value) {+ switch (value) {+ case true:+ return 'enable';+ case false:+ return 'disable';+ case undefined:+ return 'default';+ }++ throw 'Fell through case';+}++/** @param {str} value+ * @returns {boolean | undefined} */+function stringToValue(str) {+ switch (str) {+ case 'enable':+ return true;+ case 'disable':+ return false;+ case 'default':+ return undefined;+ }++ throw 'Fell through case';+}diff --git a/packages/obsidian-plugin/src/index.js b/packages/obsidian-plugin/src/index.jsindex dd1def73b..76582f470 100644--- a/packages/obsidian-plugin/src/index.js+++ b/packages/obsidian-plugin/src/index.js@@ -1,7 +1,13 @@import { linter } from './lint';import { Plugin } from 'obsidian';import wasm from 'wasm/harper_wasm_bg.wasm';-import init, { lint, use_spell_check } from 'wasm';+import init, {+ lint,+ use_spell_check,+ get_lint_config_as_object,+ set_lint_config_from_object+} from 'wasm';+import { HarperSettingTab } from './HarperSettingTab';function suggestionToLabel(sug) {if (sug.kind() == 'Remove') {@@ -60,7 +66,27 @@ const harperLinter = linter();export default class HarperPlugin extends Plugin {+ /** @public+ * @returns {Promise<Record<string, any>>} */+ async getSettings() {+ await init(await wasm());++ return { lintSettings: await get_lint_config_as_object() };+ }++ /** @public+ * @param {Record<string, any>} settings+ * @returns {Promise<void>} */+ async setSettings(settings) {+ await init(await wasm());+ await set_lint_config_from_object(settings.lintSettings);+ this.saveData(settings);+ }+async onload() {this.registerEditorExtension([harperLinter]);++ this.addSettingTab(new HarperSettingTab(this.app, this));+ await this.setSettings(await this.loadData());}}