Skip to content
Merged
Show file tree
Hide file tree
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
19 changes: 14 additions & 5 deletions packages/editor/src/core/helpers/editor-ref.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,27 @@ import type { HocuspocusProvider } from "@hocuspocus/provider";
import type { Editor } from "@tiptap/core";
import { DOMSerializer } from "@tiptap/pm/model";
import * as Y from "yjs";
// plane imports
import { convertHTMLToMarkdown } from "@plane/utils";
// components
import { getEditorMenuItems } from "@/components/menus";
// constants
import { CORE_EXTENSIONS } from "@/constants/extension";
import { CORE_EDITOR_META } from "@/constants/meta";
// types
import type { EditorRefApi, TEditorCommands } from "@/types";
import type { EditorRefApi, IEditorProps, TEditorCommands } from "@/types";
// local imports
import { getParagraphCount } from "./common";
import { insertContentAtSavedSelection } from "./insert-content-at-cursor-position";
import { scrollSummary, scrollToNodeViaDOMCoordinates } from "./scroll-to-node";

type TArgs = {
type TArgs = Pick<IEditorProps, "getEditorMetaData"> & {
editor: Editor | null;
provider: HocuspocusProvider | undefined;
};

export const getEditorRefHelpers = (args: TArgs): EditorRefApi => {
const { editor, provider } = args;
const { editor, getEditorMetaData, provider } = args;

return {
blur: () => editor?.commands.blur(),
Expand Down Expand Up @@ -77,8 +79,15 @@ export const getEditorRefHelpers = (args: TArgs): EditorRefApi => {
}),
getHeadings: () => (editor ? editor.storage.headingsList?.headings : []),
getMarkDown: () => {
const markdownOutput = editor?.storage?.markdown?.getMarkdown?.() ?? "";
return markdownOutput;
if (!editor) return "";
const editorHTML = editor.getHTML();
const metaData = getEditorMetaData(editorHTML);
// convert to markdown
const markdown = convertHTMLToMarkdown({
description_html: editorHTML,
metaData,
});
return markdown;
},
Comment on lines 81 to 91
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Potential issue | 🟠 Major

Add error handling for the markdown conversion flow.

The refactored getMarkDown implementation doesn't handle potential errors from getEditorMetaData or convertHTMLToMarkdown. If either function throws, it could break the copy markdown functionality and impact user experience.

Apply this diff to add defensive error handling:

 getMarkDown: () => {
   if (!editor) return "";
-  const editorHTML = editor.getHTML();
-  const metaData = getEditorMetaData(editorHTML);
-  // convert to markdown
-  const markdown = convertHTMLToMarkdown({
-    description_html: editorHTML,
-    metaData,
-  });
-  return markdown;
+  try {
+    const editorHTML = editor.getHTML();
+    const metaData = getEditorMetaData(editorHTML);
+    // convert to markdown
+    const markdown = convertHTMLToMarkdown({
+      description_html: editorHTML,
+      metaData,
+    });
+    return markdown;
+  } catch (error) {
+    console.error("Error converting HTML to markdown:", error);
+    return "";
+  }
 },
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
getMarkDown: () => {
const markdownOutput = editor?.storage?.markdown?.getMarkdown?.() ?? "";
return markdownOutput;
if (!editor) return "";
const editorHTML = editor.getHTML();
const metaData = getEditorMetaData(editorHTML);
// convert to markdown
const markdown = convertHTMLToMarkdown({
description_html: editorHTML,
metaData,
});
return markdown;
},
getMarkDown: () => {
if (!editor) return "";
try {
const editorHTML = editor.getHTML();
const metaData = getEditorMetaData(editorHTML);
// convert to markdown
const markdown = convertHTMLToMarkdown({
description_html: editorHTML,
metaData,
});
return markdown;
} catch (error) {
console.error("Error converting HTML to markdown:", error);
return "";
}
},
🤖 Prompt for AI Agents
In packages/editor/src/core/helpers/editor-ref.ts around lines 81 to 91, the
getMarkDown function currently calls getEditorMetaData and convertHTMLToMarkdown
without protection; wrap the conversion flow in a try/catch, catch any errors
from metadata extraction or markdown conversion, log the error with the project
logger (or console.error if no logger is available), and return an empty string
(or a safe fallback) so the UI doesn't break; ensure the catch block avoids
rethrowing and keeps the function deterministic.

isAnyDropbarOpen: () => {
if (!editor) return false;
Expand Down
11 changes: 10 additions & 1 deletion packages/editor/src/core/hooks/use-editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,16 @@ export const useEditor = (props: TEditorHookProps) => {
onAssetChange(assets);
}, [assetsList?.assets, onAssetChange]);

useImperativeHandle(forwardedRef, () => getEditorRefHelpers({ editor, provider }), [editor, provider]);
useImperativeHandle(
forwardedRef,
() =>
getEditorRefHelpers({
editor,
getEditorMetaData,
provider,
}),
[editor, getEditorMetaData, provider]
);

if (!editor) {
return null;
Expand Down
Loading