Skip to content

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

feature/obsidian-colored lints (#2931)

Stanley1def99c1e355
diff --git a/packages/obsidian-plugin/src/State.ts b/packages/obsidian-plugin/src/State.ts
index f3a431747..ee5d41dde 100644
--- a/packages/obsidian-plugin/src/State.ts
+++ b/packages/obsidian-plugin/src/State.ts
@@ -3,7 +3,14 @@ import type { Lint, LintConfig, Linter, Suggestion } from 'harper.js';
import { binaryInlined, type Dialect, LocalLinter, SuggestionKind, WorkerLinter } from 'harper.js';
import { minimatch } from 'minimatch';
import type { MarkdownFileInfo, Workspace } from 'obsidian';
-import { linter } from './lint';
+import {
+ type CustomReplacements,
+ cloneCustomReplacements,
+ getCustomSuggestions,
+ normalizeCustomReplacements,
+} from './customSuggestions';
+import { type Action, linter } from './lint';
+import { lintKindClass } from './lintKindColor';
export type Settings = {
ignoredLints?: string;
@@ -213,6 +220,7 @@ export default class State {
to: span.end,
source: linterName,
severity: 'error',
+ markClass: lintKindClass(lint.lint_kind()),
title: lint.lint_kind_pretty(),
renderMessage: (_view) => {
const node = document.createElement('template');
diff --git a/packages/obsidian-plugin/src/lint.ts b/packages/obsidian-plugin/src/lint.ts
index 5c2748a3e..801db128f 100644
--- a/packages/obsidian-plugin/src/lint.ts
+++ b/packages/obsidian-plugin/src/lint.ts
@@ -23,6 +23,7 @@ import {
WidgetType,
} from '@codemirror/view';
import elt from 'crelt';
+import { LINT_KIND_COLORS } from './lintKindColor';
type Severity = 'hint' | 'info' | 'warning' | 'error';
@@ -420,7 +421,9 @@ function renderDiagnostic(view: EditorView, diagnostic: Diagnostic, inPanel: boo
const keys = inPanel ? assignKeys(diagnostic.actions) : [];
return elt(
'li',
- { class: `cm-diagnostic cm-diagnostic-${diagnostic.severity}` },
+ {
+ class: `cm-diagnostic cm-diagnostic-${diagnostic.severity}${diagnostic.markClass ? ` ${diagnostic.markClass}` : ''}`,
+ },
elt('span', { class: 'cm-diagnosticTitle' }, diagnostic.title),
elt(
'span',
@@ -506,7 +509,9 @@ class DiagnosticWidget extends WidgetType {
}
toDOM() {
- return elt('span', { class: `cm-lintPoint cm-lintPoint-${this.diagnostic.severity}` });
+ return elt('span', {
+ class: `cm-lintPoint cm-lintPoint-${this.diagnostic.severity}${this.diagnostic.markClass ? ` ${this.diagnostic.markClass}` : ''}`,
+ });
}
}
@@ -521,6 +526,27 @@ function underline(color: string) {
);
}
+const lintKindRangeTheme = Object.fromEntries(
+ Object.entries(LINT_KIND_COLORS).map(([lintKind, color]) => [
+ `.cm-lintRange.harper-lintRange-${lintKind}`,
+ { backgroundImage: underline(color) },
+ ]),
+);
+
+const lintKindDiagnosticTheme = Object.fromEntries(
+ Object.entries(LINT_KIND_COLORS).map(([lintKind, color]) => [
+ `.cm-diagnostic.harper-lintRange-${lintKind} .cm-diagnosticTitle`,
+ { boxShadow: `inset 0 -2px ${color}` },
+ ]),
+);
+
+const lintKindPointTheme = Object.fromEntries(
+ Object.entries(LINT_KIND_COLORS).map(([lintKind, color]) => [
+ `.cm-lintPoint.harper-lintRange-${lintKind}`,
+ { '&:after': { borderBottomColor: color } },
+ ]),
+);
+
const baseTheme = EditorView.baseTheme({
'.cm-diagnostic': {
padding: '4px',
@@ -628,6 +654,7 @@ const baseTheme = EditorView.baseTheme({
'.cm-lintRange-warning': { backgroundImage: underline('orange') },
'.cm-lintRange-info': { backgroundImage: underline('#999') },
'.cm-lintRange-hint': { backgroundImage: underline('#66d') },
+ ...lintKindRangeTheme,
'.cm-lintRange-active': { backgroundColor: '#ffdd9980' },
'.cm-tooltip-lint': {
@@ -658,6 +685,8 @@ const baseTheme = EditorView.baseTheme({
'.cm-lintPoint-hint': {
'&:after': { borderBottomColor: '#66d' },
},
+ ...lintKindPointTheme,
+ ...lintKindDiagnosticTheme,
'.cm-panel.cm-panel-lint': {
position: 'relative',
diff --git a/packages/obsidian-plugin/src/lintKindColor.test.ts b/packages/obsidian-plugin/src/lintKindColor.test.ts
new file mode 100644
index 000000000..939d578ac
--- /dev/null
+++ b/packages/obsidian-plugin/src/lintKindColor.test.ts
@@ -0,0 +1,13 @@
+import { expect, test } from 'vitest';
+import { lintKindClass, lintKindColor } from './lintKindColor';
+
+test('lintKindColor matches Harper web colors for common kinds', () => {
+ expect(lintKindColor('Spelling')).toBe('#EE4266');
+ expect(lintKindColor('Style')).toBe('#FFD23F');
+ expect(lintKindColor('Grammar')).toBe('#9B59B6');
+});
+
+test('lint kind helpers produce safe fallback values', () => {
+ expect(lintKindColor('DoesNotExist')).toBe('#d11');
+ expect(lintKindClass('Style')).toBe('harper-lintRange-Style');
+});
diff --git a/packages/obsidian-plugin/src/lintKindColor.ts b/packages/obsidian-plugin/src/lintKindColor.ts
new file mode 100644
index 000000000..8ca1fe8a9
--- /dev/null
+++ b/packages/obsidian-plugin/src/lintKindColor.ts
@@ -0,0 +1,32 @@
+const FALLBACK_LINT_COLOR = '#d11';
+
+export const LINT_KIND_COLORS = {
+ Agreement: '#228B22',
+ BoundaryError: '#8B4513',
+ Capitalization: '#540D6E',
+ Eggcorn: '#FF8C00',
+ Enhancement: '#0EAD69',
+ Formatting: '#7D3C98',
+ Grammar: '#9B59B6',
+ Malapropism: '#C71585',
+ Miscellaneous: '#3BCEAC',
+ Nonstandard: '#008B8B',
+ Punctuation: '#D4850F',
+ Readability: '#2E8B57',
+ Redundancy: '#4682B4',
+ Regionalism: '#C061CB',
+ Repetition: '#00A67C',
+ Spelling: '#EE4266',
+ Style: '#FFD23F',
+ Typo: '#FF6B35',
+ Usage: '#1E90FF',
+ WordChoice: '#228B22',
+} as const;
+
+export function lintKindColor(lintKindKey: string): string {
+ return LINT_KIND_COLORS[lintKindKey as keyof typeof LINT_KIND_COLORS] ?? FALLBACK_LINT_COLOR;
+}
+
+export function lintKindClass(lintKindKey: string): string {
+ return `harper-lintRange-${lintKindKey}`;
+}