import type { Node as ProseMirrorNode } from '@tiptap/pm/model'; /** * Spelling and grammar checking that never leaves the device: Harper is * WebAssembly in this browser, so no word of the manuscript is sent anywhere. * * This module holds the parts that need no Harper: the text the linter reads, * the offset to position map, the dialect preference and the stored dictionary. * `proofread.svelte.ts` owns the linter and the state the panel reads. */ /** * Harper's dialects, keyed by its `Dialect` enum, so the client can index that * enum without this module importing Harper. */ export const DIALECTS = [ { value: 'British', label: 'British English' }, { value: 'American', label: 'American English' }, { value: 'Australian', label: 'Australian English' }, { value: 'Canadian', label: 'Canadian English' }, { value: 'Indian', label: 'Indian English' } ] as const; export type ProofreadDialect = (typeof DIALECTS)[number]['value']; /** * The spelling the browser's language tag asks for. A language tag with no * English region gets American, Harper's default and the more common choice for * a writer who has not thought about it. */ export function dialectForLocale(locale: string): ProofreadDialect { const match = /^en[-_]([a-z]{2})/i.exec(locale.trim()); if (!match) return 'American'; switch (match[1].toLowerCase()) { case 'gb': case 'ie': case 'nz': return 'British'; case 'au': return 'Australian'; case 'ca': return 'Canadian'; case 'in': return 'Indian'; default: return 'American'; } } export function readSavedDialect(): ProofreadDialect | null { if (typeof localStorage === 'undefined') return null; try { const value = localStorage.getItem('fablewright:proofread:dialect'); return DIALECTS.find((dialect) => dialect.value === value)?.value ?? null; } catch { return null; } } export function saveDialect(dialect: ProofreadDialect) { try { localStorage.setItem('fablewright:proofread:dialect', dialect); } catch { // A browser that keeps nothing still uses the choice for this session. } } /** Whether checking was on last time. Off until a writer asks for it. */ export function readSavedProofread(): boolean { if (typeof localStorage === 'undefined') return false; try { return localStorage.getItem('fablewright:proofread') === 'on'; } catch { return false; } } export function saveProofread(enabled: boolean) { try { localStorage.setItem('fablewright:proofread', enabled ? 'on' : 'off'); } catch { // Remembering the switch is a convenience, not a guarantee. } } /** * Words the writer added to the dictionary. They belong to the browser, like the * colour mode, because they describe this writer, not the project. */ export function readSavedWords(): string[] { if (typeof localStorage === 'undefined') return []; try { const raw = localStorage.getItem('fablewright:proofread:words'); if (!raw) return []; const parsed: unknown = JSON.parse(raw); if (!Array.isArray(parsed)) return []; return parsed.filter((word): word is string => typeof word === 'string' && word.length > 0); } catch { return []; } } export function saveWords(words: string[]) { try { localStorage.setItem('fablewright:proofread:words', JSON.stringify(words)); } catch { // The dictionary is rebuilt from what the browser kept. } } /** * Ignored lints arrive as Harper's privacy-respecting list of hashes, so the * stored value is opaque JSON. This module never reads inside it. */ export function readSavedIgnoredLints(): string | null { if (typeof localStorage === 'undefined') return null; try { return localStorage.getItem('fablewright:proofread:ignored'); } catch { return null; } } export function saveIgnoredLints(json: string) { try { localStorage.setItem('fablewright:proofread:ignored', json); } catch { // An ignore that cannot be remembered still applies to this session. } } /** * What one lint found, converted out of Harper's WebAssembly objects. The * positions are document positions, so a renderer or an edit can work without * knowing about Harper. */ export interface ProofreadIssue { /** Unique for the page, so a stale popover cannot act on a different issue. */ id: number; from: number; to: number; /** Harper's category, such as `Spelling` or `Agreement`. */ kind: string; message: string; /** The words the lint points at. */ problem: string; suggestions: ProofreadSuggestion[]; } export interface ProofreadSuggestion { kind: 'replace' | 'remove' | 'insertAfter'; text: string; } /** * The categories a novelist asked to see. Harper also flags style, readability * and word choice, which are opinions about voice: leaving them out is the * difference between a proofreader and an editor with its own taste. */ const CHECKED_KINDS = new Set([ 'Agreement', 'BoundaryError', 'Capitalization', 'Eggcorn', 'Formatting', 'Grammar', 'Malapropism', 'Punctuation', 'Redundancy', 'Repetition', 'Spelling', 'Typo', 'WordOrder' ]); export function isCheckedKind(kind: string): boolean { return CHECKED_KINDS.has(kind); } /** * The words Harper reads, and where each character sits in the document. * * Harper counts spans in Unicode scalar values, which is not how JavaScript * counts a string or how ProseMirror counts a position. Both conversions happen * here, once per character, so a lint maps to a range without guessing. */ export interface LintSource { text: string; /** The document position of each character, by code point. -1 is a separator. */ starts: number[]; /** The document position just past each character, by code point. */ ends: number[]; } const SEPARATOR = '\n'; /** * Blocks are joined with a newline, so a lint cannot run from the end of one * paragraph into the start of the next. Those newlines map to -1, and every * range that crosses one is refused. */ export function lintSource(doc: ProseMirrorNode): LintSource { const chunks: { text: string; from: number }[] = []; let firstBlock = true; doc.descendants((node, pos) => { if (node.isText) { chunks.push({ text: node.text ?? '', from: pos }); return; } if (node.isBlock && node.isTextblock) { if (!firstBlock) chunks.push({ text: SEPARATOR, from: -1 }); firstBlock = false; return; } // A hard break or an inline leaf ends the line without starting a block. if (node.isInline && node.isLeaf) chunks.push({ text: SEPARATOR, from: -1 }); }); let text = ''; const starts: number[] = []; const ends: number[] = []; for (const chunk of chunks) { let offset = 0; for (const character of chunk.text) { const from = chunk.from < 0 ? -1 : chunk.from + offset; text += character; starts.push(from); ends.push(from < 0 ? -1 : from + character.length); offset += character.length; } } return { text, starts, ends }; } /** * The document range a lint points at, or null when it cannot be placed. A span * that touches a separator belongs to two blocks, and no range means "the end of * one paragraph and the start of the next": the lint is dropped, and the next * check finds it once the words sit inside one block. */ export function rangeForSpan( source: LintSource, start: number, end: number ): { from: number; to: number } | null { if (!Number.isInteger(start) || !Number.isInteger(end)) return null; if (start < 0 || end <= start || end > source.starts.length) return null; for (let index = start; index < end; index += 1) { if (source.starts[index] < 0) return null; } const from = source.starts[start]; const to = source.ends[end - 1]; if (from < 0 || to <= from) return null; return { from, to }; } export interface RawLint { id: number; kind: string; message: string; problem: string; start: number; end: number; suggestions: ProofreadSuggestion[]; } export function toIssues(source: LintSource, lints: RawLint[]): ProofreadIssue[] { const issues: ProofreadIssue[] = []; for (const lint of lints) { if (!isCheckedKind(lint.kind)) continue; const range = rangeForSpan(source, lint.start, lint.end); if (!range) continue; issues.push({ id: lint.id, ...range, kind: lint.kind, message: lint.message, problem: lint.problem, suggestions: lint.suggestions }); } return issues; } /** * Spelling and everything else take different colours, because "this is not a * word" and "this sentence does not agree with itself" are different problems. */ export function markClass(kind: string): string { return kind === 'Spelling' || kind === 'Typo' || kind === 'Malapropism' ? 'proofread-spelling' : 'proofread-grammar'; } /** A word a dictionary can hold: letters, with the apostrophes names use. */ export function looksLikeWord(text: string): boolean { return /^[\p{L}][\p{L}'’-]*$/u.test(text.trim()); } export function suggestionLabel(suggestion: ProofreadSuggestion): string { const quoted = `“${suggestion.text}”`; if (suggestion.kind === 'remove') return 'Remove'; if (suggestion.kind === 'insertAfter') return `Add ${quoted} after`; return `Use ${quoted}`; } export function kindLabel(kind: string): string { switch (kind) { case 'Spelling': return 'Spelling'; case 'Typo': return 'Typo'; case 'Malapropism': return 'Word'; case 'Capitalization': return 'Capitalisation'; case 'Punctuation': return 'Punctuation'; case 'Repetition': return 'Repetition'; case 'Redundancy': return 'Redundancy'; case 'WordOrder': return 'Word order'; case 'Eggcorn': return 'Word'; case 'Formatting': return 'Formatting'; case 'BoundaryError': return 'Space'; default: return 'Grammar'; } }