From 96a642003c034a45987422462eab697418b1e836 Mon Sep 17 00:00:00 2001 From: Petyo Ivanov Date: Wed, 30 Aug 2023 11:06:23 +0300 Subject: [PATCH] fix: allow optional JSX source Fixes #50 Fixes #51 --- src/exportMarkdownFromLexical.ts | 3 +++ src/plugins/jsx/index.ts | 3 ++- src/test/jsx.test.tsx | 31 +++++++++++++++++++++++++++++++ 3 files changed, 36 insertions(+), 1 deletion(-) create mode 100644 src/test/jsx.test.tsx diff --git a/src/exportMarkdownFromLexical.ts b/src/exportMarkdownFromLexical.ts index fec82646..3a16c88f 100644 --- a/src/exportMarkdownFromLexical.ts +++ b/src/exportMarkdownFromLexical.ts @@ -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 { diff --git a/src/plugins/jsx/index.ts b/src/plugins/jsx/index.ts index 0d9c31f4..c54ae51e 100644 --- a/src/plugins/jsx/index.ts +++ b/src/plugins/jsx/index.ts @@ -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 */ diff --git a/src/test/jsx.test.tsx b/src/test/jsx.test.tsx new file mode 100644 index 00000000..d732de0d --- /dev/null +++ b/src/test/jsx.test.tsx @@ -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 = ` + + ` + const ref = React.createRef() + render() + const processedMarkdown = ref.current?.getMarkdown().trim() + expect(processedMarkdown).toEqual(markdown.trim()) + }) +})