Skip to content
Closed
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
4 changes: 3 additions & 1 deletion src/MDXEditor.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -408,7 +408,9 @@ export const MDXEditor = React.forwardRef<MDXEditorMethods, MDXEditorProps>((pro
const realmMethods = React.useRef<MDXEditorMethods | null>(null)
const replayingRealmMethods = React.useRef<MDXEditorMethods | null>(null)
const pendingMethodCalls = React.useRef<((methods: MDXEditorMethods) => void)[]>([])
const preReadyMarkdown = React.useRef(props.trim === false ? props.markdown : props.markdown.trim())
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition -- runtime markdown can be undefined/null
const initialMarkdown = props.markdown ?? ''
const preReadyMarkdown = React.useRef(props.trim === false ? initialMarkdown : initialMarkdown.trim())
const callWhenReady = React.useCallback((call: (methods: MDXEditorMethods) => void) => {
const methods = realmMethods.current
if (methods && replayingRealmMethods.current === null) {
Expand Down
9 changes: 6 additions & 3 deletions src/plugins/core/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -370,15 +370,18 @@ export const setMarkdown$ = Signal<string>((r) => {
setMarkdown$,
withLatestFrom(markdown$, rootEditor$, inFocus$),
filter(([newMarkdown, oldMarkdown]) => {
return newMarkdown.trim() !== oldMarkdown.trim()
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition -- runtime values can be undefined
return (newMarkdown ?? '').trim() !== (oldMarkdown ?? '').trim()
})
),
([theNewMarkdownValue, , editor, inFocus]) => {
// eslint-disable-next-line @typescript-eslint/no-unnecessary-condition -- runtime values can be undefined
const markdown = theNewMarkdownValue ?? ''
r.pub(muteChange$, true)
editor?.update(
() => {
$getRoot().clear()
tryImportingMarkdown(r, $getRoot(), theNewMarkdownValue)
tryImportingMarkdown(r, $getRoot(), markdown)

if (!inFocus) {
$setSelection(null)
Expand Down Expand Up @@ -1012,7 +1015,7 @@ export const corePlugin = realmPlugin<{
params?.editorState === null
? null
: () => {
const markdown = params?.initialMarkdown.trim() ?? ''
const markdown = (params?.initialMarkdown ?? '').trim()
tryImportingMarkdown(r, $getRoot(), markdown)
}
})
Expand Down
27 changes: 26 additions & 1 deletion src/test/core.test.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import React from 'react'
import { describe, expect, it, test, vi } from 'vitest'
import { codeBlockPlugin, codeMirrorPlugin, MDXEditor, MDXEditorMethods, thematicBreakPlugin } from '../'
import { render } from '@testing-library/react'
import { act, render, waitFor } from '@testing-library/react'
import { $getRoot, createEditor, ParagraphNode, TextNode } from 'lexical'
import { QuoteNode } from '@lexical/rich-text'
import { importMarkdownToLexical, type MarkdownParseOptions } from '../importMarkdownToLexical'
Expand Down Expand Up @@ -227,6 +227,31 @@ After fence.
})
})

describe('undefined markdown resilience', () => {
it('renders an empty editor when markdown is undefined', () => {
const ref = React.createRef<MDXEditorMethods>()
render(<MDXEditor ref={ref} markdown={undefined as unknown as string} />)
expect(ref.current?.getMarkdown().trim()).toEqual('')
})

it('renders an empty editor when markdown is null', () => {
const ref = React.createRef<MDXEditorMethods>()
render(<MDXEditor ref={ref} markdown={null as unknown as string} />)
expect(ref.current?.getMarkdown().trim()).toEqual('')
})

it('treats setMarkdown(undefined) as empty without throwing', async () => {
const ref = React.createRef<MDXEditorMethods>()
render(<MDXEditor ref={ref} markdown="hello" />)
act(() => {
ref.current?.setMarkdown(undefined as unknown as string)
})
await waitFor(() => {
expect(ref.current?.getMarkdown().trim()).toEqual('')
})
})
})

describe('List parsing and serialization', () => {
const parseAndExport = (markdown: string) => {
const mdastVisitors = [
Expand Down