Skip to content

A fully open source & end-to-end encrypted note taking alternative to Evernote.

editor: disable spellcheck for inline code (#7036)

01zulfi93af79926d40
diff --git a/packages/editor/src/extensions/inline-code/index.ts b/packages/editor/src/extensions/inline-code/index.ts
new file mode 100644
index 000000000..30eb13058
--- /dev/null
+++ b/packages/editor/src/extensions/inline-code/index.ts
@@ -0,0 +1,21 @@
+/*
+This file is part of the Notesnook project (https://notesnook.com/)
+
+Copyright (C) 2023 Streetwriters (Private) Limited
+
+This program is free software: you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation, either version 3 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program. If not, see <http://www.gnu.org/licenses/>.
+*/
+import { InlineCode } from "./inline-code";
+
+export default InlineCode;
diff --git a/packages/editor/src/extensions/inline-code/inline-code.ts b/packages/editor/src/extensions/inline-code/inline-code.ts
new file mode 100644
index 000000000..9fddbc881
--- /dev/null
+++ b/packages/editor/src/extensions/inline-code/inline-code.ts
@@ -0,0 +1,22 @@
+import Code from "@tiptap/extension-code";
+
+export const InlineCode = Code.extend({
+ excludes: "link",
+ addAttributes() {
+ return {
+ ...this.parent?.(),
+ spellcheck: {
+ default: "false",
+ parseHTML: (element) => element.getAttribute("spellcheck"),
+ renderHTML: (attributes) => {
+ if (!attributes.spellcheck) {
+ return {};
+ }
+ return {
+ spellcheck: attributes.spellcheck
+ };
+ }
+ }
+ };
+ }
+});
diff --git a/packages/editor/src/extensions/inline-code/tests/inline-code.test.ts b/packages/editor/src/extensions/inline-code/tests/inline-code.test.ts
new file mode 100644
index 000000000..12736068d
--- /dev/null
+++ b/packages/editor/src/extensions/inline-code/tests/inline-code.test.ts
@@ -0,0 +1,37 @@
+/*
+This file is part of the Notesnook project (https://notesnook.com/)
+
+Copyright (C) 2023 Streetwriters (Private) Limited
+
+This program is free software: you can redistribute it and/or modify
+it under the terms of the GNU General Public License as published by
+the Free Software Foundation, either version 3 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+GNU General Public License for more details.
+
+You should have received a copy of the GNU General Public License
+along with this program. If not, see <http://www.gnu.org/licenses/>.
+*/
+
+import { expect, test } from "vitest";
+import { createEditor, h } from "../../../../test-utils/index.js";
+import { Link } from "../../link/link.js";
+import { InlineCode } from "../inline-code";
+
+test("inline code has spellcheck disabled", async () => {
+ const el = h("code", ["blazingly fast javascript"]);
+ const editor = createEditor({
+ initialContent: el.outerHTML,
+ extensions: {
+ link: Link,
+ code: InlineCode
+ }
+ });
+ expect(
+ editor.editor.view.dom.querySelector("code")?.getAttribute("spellcheck")
+ ).toBe("false");
+});
diff --git a/packages/editor/src/index.ts b/packages/editor/src/index.ts
index 4727f9215..951732507 100644
--- a/packages/editor/src/index.ts
+++ b/packages/editor/src/index.ts
@@ -23,7 +23,6 @@ import {
getHTMLFromFragment
} from "@tiptap/core";
import CharacterCount from "@tiptap/extension-character-count";
-import { Code } from "@tiptap/extension-code";
import Color from "@tiptap/extension-color";
import HorizontalRule from "@tiptap/extension-horizontal-rule";
import { Link, LinkAttributes } from "./extensions/link/index.js";
@@ -85,6 +84,7 @@ import { useEditorSearchStore } from "./toolbar/stores/search-store.js";
import { DiffHighlighter } from "./extensions/diff-highlighter/index.js";
import { getChangedNodes } from "./utils/prosemirror.js";
import { strings } from "@notesnook/intl";
+import { InlineCode } from "./extensions/inline-code/inline-code.js";
interface TiptapStorage {
dateFormat?: DateTimeOptions["dateFormat"];
@@ -302,7 +302,7 @@ const useTiptap = (
OutlineListItem,
OutlineList.configure({ keepAttributes: true, keepMarks: true }),
ListItem,
- Code.extend({ excludes: "link" }),
+ InlineCode,
Codemark,
MathInline,
MathBlock,