Skip to content

Commit f553c62

Browse files
authored
Fix spell checking for code blocks and inline code (#1244)
1 parent 31792a3 commit f553c62

File tree

3 files changed

+28
-12
lines changed

3 files changed

+28
-12
lines changed

web/cm_plugins/line_wrapper.ts

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ interface WrapElement {
77
selector: string;
88
class: string;
99
nesting?: boolean;
10-
disableSpellCheck?: boolean;
1110
}
1211

1312
export function lineWrapper(wrapElements: WrapElement[]) {
@@ -18,10 +17,6 @@ export function lineWrapper(wrapElements: WrapElement[]) {
1817
syntaxTree(state).iterate({
1918
enter: ({ type, from, to }) => {
2019
for (const wrapElement of wrapElements) {
21-
const spellCheckAttributes = wrapElement.disableSpellCheck
22-
? { attributes: { spellcheck: "false" } }
23-
: {};
24-
2520
if (type.name == wrapElement.selector) {
2621
if (wrapElement.nesting) {
2722
elementStack.push(type.name);
@@ -36,7 +31,6 @@ export function lineWrapper(wrapElements: WrapElement[]) {
3631
widgets.push(
3732
Decoration.line({
3833
class: cls,
39-
...spellCheckAttributes,
4034
}).range(doc.lineAt(idx).from),
4135
);
4236
idx += line.length + 1;

web/cm_plugins/spell_checking.ts

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import type { EditorState, Range } from "@codemirror/state";
2+
import { Decoration } from "@codemirror/view";
3+
import { syntaxTree } from "@codemirror/language";
4+
import { decoratorStateField } from "./util.ts";
5+
6+
export function disableSpellcheck(selectors: string[]) {
7+
return decoratorStateField((state: EditorState) => {
8+
const widgets: Range<Decoration>[] = [];
9+
syntaxTree(state).iterate({
10+
enter: ({ type, from, to }) => {
11+
for (const selector of selectors) {
12+
if (type.name === selector) {
13+
widgets.push(
14+
Decoration.mark({
15+
attributes: { spellcheck: "false" },
16+
}).range(from, to),
17+
);
18+
}
19+
}
20+
},
21+
});
22+
23+
return Decoration.set(widgets, true);
24+
});
25+
}

web/editor_state.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@ import { extendedMarkdownLanguage } from "$common/markdown_parser/parser.ts";
5353
import { parseCommand } from "$common/command.ts";
5454
import { safeRun } from "$lib/async.ts";
5555
import { codeCopyPlugin } from "./cm_plugins/code_copy.ts";
56+
import { disableSpellcheck } from "./cm_plugins/spell_checking.ts";
5657

5758
export function createEditorState(
5859
client: Client,
@@ -159,21 +160,17 @@ export function createEditorState(
159160
{ selector: "Blockquote", class: "sb-line-blockquote" },
160161
{ selector: "Task", class: "sb-line-task" },
161162
{ selector: "CodeBlock", class: "sb-line-code" },
162-
{
163-
selector: "FencedCode",
164-
class: "sb-line-fenced-code",
165-
disableSpellCheck: true,
166-
},
163+
{ selector: "FencedCode", class: "sb-line-fenced-code" },
167164
{ selector: "Comment", class: "sb-line-comment" },
168165
{ selector: "BulletList", class: "sb-line-ul" },
169166
{ selector: "OrderedList", class: "sb-line-ol" },
170167
{ selector: "TableHeader", class: "sb-line-tbl-header" },
171168
{
172169
selector: "FrontMatter",
173170
class: "sb-frontmatter",
174-
disableSpellCheck: true,
175171
},
176172
]),
173+
disableSpellcheck(["InlineCode", "CodeText", "FrontMatter"]),
177174
keyBindings,
178175
EditorView.domEventHandlers({
179176
// This may result in duplicated touch events on mobile devices

0 commit comments

Comments
 (0)