Skip to content

Commit

Permalink
fix: allow optional JSX source
Browse files Browse the repository at this point in the history
Fixes #50
Fixes #51
  • Loading branch information
petyosi committed Aug 30, 2023
1 parent db60df3 commit 96a6420
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 1 deletion.
3 changes: 3 additions & 0 deletions src/exportMarkdownFromLexical.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,9 @@ export function exportLexicalTreeToMdast({
if (!descriptor) {
throw new Error(`Component ${componentName} is used but not imported`)
}
if (!descriptor.source) {
continue
}
if (descriptor.defaultExport) {
defaultImportsMap.set(componentName, descriptor.source)
} else {
Expand Down
3 changes: 2 additions & 1 deletion src/plugins/jsx/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,9 @@ export interface JsxComponentDescriptor {
kind: 'flow' | 'text'
/**
* The module path from which the component can be imported
* Omit to skip injecting an import statement
*/
source: string
source?: string
/**
* Wether the component is the default export of the module
*/
Expand Down
31 changes: 31 additions & 0 deletions src/test/jsx.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import React from 'react'
import { describe, expect, it } from 'vitest'
import { GenericJsxEditor, JsxComponentDescriptor, MDXEditor, MDXEditorMethods, jsxPlugin } from '../'
import { render } from '@testing-library/react'

// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
;(globalThis as any).IS_REACT_ACT_ENVIRONMENT = true

const jsxComponentDescriptors: JsxComponentDescriptor[] = [
{
name: 'Callout',
kind: 'text',
props: [
{ name: 'foo', type: 'string' },
{ name: 'bar', type: 'string' }
],
hasChildren: false,
Editor: GenericJsxEditor
}
]
describe('jsx markdown import export', () => {
it('skips jsx import if not specified', () => {
const markdown = `
<Callout />
`
const ref = React.createRef<MDXEditorMethods>()
render(<MDXEditor ref={ref} plugins={[jsxPlugin({ jsxComponentDescriptors })]} markdown={markdown} />)
const processedMarkdown = ref.current?.getMarkdown().trim()
expect(processedMarkdown).toEqual(markdown.trim())
})
})

0 comments on commit 96a6420

Please sign in to comment.