From 737a97ff30dd9f04c747dd669b562eacf4e75b6a Mon Sep 17 00:00:00 2001 From: Tom Moor Date: Mon, 17 Feb 2025 14:26:46 -0500 Subject: [PATCH] fix: Content with HTML images should never be considered as markdown --- app/editor/extensions/PasteHandler.tsx | 109 ++++++++++++++----------- 1 file changed, 61 insertions(+), 48 deletions(-) diff --git a/app/editor/extensions/PasteHandler.tsx b/app/editor/extensions/PasteHandler.tsx index e6a3323ca2..73c1c63400 100644 --- a/app/editor/extensions/PasteHandler.tsx +++ b/app/editor/extensions/PasteHandler.tsx @@ -25,52 +25,6 @@ import { isDocumentUrl, isUrl } from "@shared/utils/urls"; import stores from "~/stores"; import PasteMenu from "../components/PasteMenu"; -/** - * Checks if the HTML string is likely coming from Dropbox Paper. - * - * @param html The HTML string to check. - * @returns True if the HTML string is likely coming from Dropbox Paper. - */ -function isDropboxPaper(html: string): boolean { - return html?.includes("usually-unique-id"); -} - -function sliceSingleNode(slice: Slice) { - return slice.openStart === 0 && - slice.openEnd === 0 && - slice.content.childCount === 1 - ? slice.content.firstChild - : null; -} - -/** - * Parses the text contents of an HTML string and returns the src of the first - * iframe if it exists. - * - * @param text The HTML string to parse. - * @returns The src of the first iframe if it exists, or undefined. - */ -function parseSingleIframeSrc(html: string) { - try { - const parser = new DOMParser(); - const doc = parser.parseFromString(html, "text/html"); - - if ( - doc.body.children.length === 1 && - doc.body.firstElementChild?.tagName === "IFRAME" - ) { - const iframe = doc.body.firstElementChild; - const src = iframe.getAttribute("src"); - if (src) { - return src; - } - } - } catch (e) { - // Ignore the million ways parsing could fail. - } - return undefined; -} - export default class PasteHandler extends Extension { state: { open: boolean; @@ -261,9 +215,12 @@ export default class PasteHandler extends Extension { // If the text on the clipboard looks like Markdown OR there is no // html on the clipboard then try to parse content as Markdown if ( - (isMarkdown(text) && !isDropboxPaper(html)) || + (isMarkdown(text) && + !isDropboxPaper(html) && + !isContainingImage(html)) || pasteCodeLanguage === "markdown" || - this.shiftKey + this.shiftKey || + !html ) { event.preventDefault(); @@ -475,3 +432,59 @@ export default class PasteHandler extends Extension { /> ); } + +/** + * Checks if the HTML string is likely coming from Dropbox Paper. + * + * @param html The HTML string to check. + * @returns True if the HTML string is likely coming from Dropbox Paper. + */ +function isDropboxPaper(html: string): boolean { + return html?.includes("usually-unique-id"); +} + +/** + * Checks if the HTML string contains an image. + * + * @param html The HTML string to check. + * @returns True if the HTML string contains an image. + */ +function isContainingImage(html: string): boolean { + return html?.includes("