diff --git a/com.glyph.spellcheck-fa/main.js b/com.glyph.spellcheck-fa/main.js new file mode 100644 index 0000000..c1f9fe2 --- /dev/null +++ b/com.glyph.spellcheck-fa/main.js @@ -0,0 +1,30 @@ +// Persian (Farsi) spell-check dictionary for Glyph. +// +// Registers فارسی in Settings → Editor → Spell Check. The Hunspell dictionary +// is the Lilak project's, published as `dictionary-fa` (Apache-2.0) in the +// wooorm/dictionaries collection, fetched from jsDelivr the first time the +// language is selected; Glyph caches the parsed checker for the session. +// Pinned to an exact version so the fetched content never changes under us. +const DICTIONARY_BASE = "https://cdn.jsdelivr.net/npm/dictionary-fa@2.0.0"; + +async function fetchText(url) { + const res = await fetch(url); + if (!res.ok) throw new Error(`Failed to load ${url}: ${res.status}`); + return res.text(); +} + +export default { + activate(ctx) { + ctx.spellcheck.registerDictionary({ + language: "fa", + label: "فارسی (Persian)", + async load() { + const [aff, dic] = await Promise.all([ + fetchText(`${DICTIONARY_BASE}/index.aff`), + fetchText(`${DICTIONARY_BASE}/index.dic`), + ]); + return { aff, dic }; + }, + }); + }, +}; diff --git a/com.glyph.spellcheck-fa/manifest.json b/com.glyph.spellcheck-fa/manifest.json new file mode 100644 index 0000000..05d00af --- /dev/null +++ b/com.glyph.spellcheck-fa/manifest.json @@ -0,0 +1,8 @@ +{ + "id": "com.glyph.spellcheck-fa", + "name": "Persian Spell Check", + "version": "1.0.0", + "apiVersion": "^1.3.0", + "description": "Persian (Farsi) spell-check dictionary: adds فارسی to Settings → Editor → Spell Check.", + "permissions": ["network:cdn.jsdelivr.net"] +} diff --git a/docs/api-reference.md b/docs/api-reference.md index 66877af..dd05abe 100644 --- a/docs/api-reference.md +++ b/docs/api-reference.md @@ -100,6 +100,26 @@ ctx.exporters.register({ }); ``` +## `ctx.spellcheck` (API 1.3) + +Contribute a spell-check dictionary. The language appears (by `label`) in Settings → Editor → Spell Check → Language, and `load` runs only the first time the user selects it, so a fetched dictionary costs nothing until then. Registering a code Glyph already knows (including the built-in `en`) replaces it; unloading the plugin removes the language and drops any cached checker built from it. + +```ts +ctx.spellcheck.registerDictionary({ + language: "fa", + label: "فارسی (Persian)", + async load() { + // Return Hunspell sources as text, from wherever suits your plugin: + // inlined strings, or a fetch under a `network:` permission. + return { aff, dic }; + }, +}); +``` + +- `load` must resolve to `{ aff, dic }`: the two Hunspell files as UTF-8 text. +- Main-context only; the sandbox does not expose `ctx.spellcheck` (the loader would have to cross the worker bridge). +- A rejected `load` is not cached: the next time the user selects the language, Glyph calls it again. + ## `ctx.markdown` Extend how documents render. diff --git a/docs/plugin-catalog.md b/docs/plugin-catalog.md index ea55a65..26e8fa4 100644 --- a/docs/plugin-catalog.md +++ b/docs/plugin-catalog.md @@ -15,3 +15,13 @@ The sample plugin: adds a status bar greeting and a "Hello Status: Say Hello" co - **Permissions**: none - **Sandbox**: no - **Source**: [com.glyph.hello-status/](../com.glyph.hello-status/) in this repo + +## Persian Spell Check + +`com.glyph.spellcheck-fa` + +Adds a Persian (Farsi) dictionary to the editor's spell check: once installed, فارسی appears under Settings → Editor → Spell Check → Language. The Hunspell dictionary comes from the [Lilak](https://github.com/b00f/lilak) project (Apache-2.0), published as [`dictionary-fa`](https://www.npmjs.com/package/dictionary-fa); it is fetched from jsDelivr the first time you select the language (about 1 MB, once per session), so the first activation needs a network connection. Requires Glyph plugin API 1.3 (spell-check dictionaries). + +- **Permissions**: `network:cdn.jsdelivr.net` +- **Sandbox**: no (dictionary contributions are main-context only) +- **Source**: [com.glyph.spellcheck-fa/](../com.glyph.spellcheck-fa/) in this repo diff --git a/index.json b/index.json index a28f7fa..2971217 100644 --- a/index.json +++ b/index.json @@ -8,6 +8,15 @@ "version": "1.0.0", "apiVersion": "^1.0.0", "mainUrl": "https://raw.githubusercontent.com/glyph-md/plugins/main/com.glyph.hello-status/main.js" + }, + { + "id": "com.glyph.spellcheck-fa", + "name": "Persian Spell Check", + "description": "Persian (Farsi) spell-check dictionary: adds فارسی to Settings → Editor → Spell Check.", + "version": "1.0.0", + "apiVersion": "^1.3.0", + "permissions": ["network:cdn.jsdelivr.net"], + "mainUrl": "https://raw.githubusercontent.com/glyph-md/plugins/main/com.glyph.spellcheck-fa/main.js" } ] }