Skip to content

fix: Force pasting plain text in code block #1663

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
May 9, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion packages/core/src/api/clipboard/fromClipboard/pasteExtension.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import type {
BlockNoteEditor,
BlockNoteEditorOptions,
} from "../../../editor/BlockNoteEditor";
import { isMarkdown } from "../../parsers/markdown/detectMarkdown.js";
import {
BlockSchema,
InlineContentSchema,
Expand All @@ -13,7 +14,6 @@ import {
import { acceptedMIMETypes } from "./acceptedMIMETypes.js";
import { handleFileInsertion } from "./handleFileInsertion.js";
import { handleVSCodePaste } from "./handleVSCodePaste.js";
import { isMarkdown } from "../../parsers/markdown/detectMarkdown.js";

function defaultPasteHandler({
event,
Expand All @@ -26,6 +26,24 @@ function defaultPasteHandler({
prioritizeMarkdownOverHTML: boolean;
plainTextAsMarkdown: boolean;
}) {
// Special case for code blocks, as they do not support any rich text
// formatting, so we force pasting plain text.
const isInCodeBlock = editor.transact(
(tr) =>
tr.selection.$from.parent.type.spec.code &&
tr.selection.$to.parent.type.spec.code
);

if (isInCodeBlock) {
const data = event.clipboardData?.getData("text/plain");

if (data) {
editor.pasteText(data);

return true;
}
}

let format: (typeof acceptedMIMETypes)[number] | undefined;
for (const mimeType of acceptedMIMETypes) {
if (event.clipboardData!.types.includes(mimeType)) {
Expand Down
Loading