Skip to content

Commit

Permalink
feat: support fallback descriptors for JSX
Browse files Browse the repository at this point in the history
  • Loading branch information
petyosi committed Nov 5, 2024
1 parent 44f461c commit c6d1067
Show file tree
Hide file tree
Showing 4 changed files with 12 additions and 4 deletions.
4 changes: 3 additions & 1 deletion src/exportMarkdownFromLexical.ts
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,9 @@ export function exportLexicalTreeToMdast({
const defaultImportsMap = new Map<string, string>()

for (const componentName of referredComponents) {
const descriptor = jsxComponentDescriptors.find((descriptor) => descriptor.name === componentName)
const descriptor =
jsxComponentDescriptors.find((descriptor) => descriptor.name === componentName) ??
jsxComponentDescriptors.find((descriptor) => descriptor.name === '*')
if (!descriptor) {
throw new Error(`Component ${componentName} is used but not imported`)
}
Expand Down
5 changes: 4 additions & 1 deletion src/plugins/jsx/LexicalJsxNode.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,10 @@ export function JsxEditorContainer(props: {
}) {
const { mdastNode } = props
const jsxComponentDescriptors = useCellValue(jsxComponentDescriptors$)
const descriptor = jsxComponentDescriptors.find((descriptor) => descriptor.name === mdastNode.name)
const descriptor =
jsxComponentDescriptors.find((descriptor) => descriptor.name === mdastNode.name) ??
jsxComponentDescriptors.find((descriptor) => descriptor.name === '*')

if (!descriptor) {
throw new Error(`No JSX descriptor found for ${mdastNode.name}`)
}
Expand Down
4 changes: 3 additions & 1 deletion src/plugins/jsx/MdastMdxJsxElementVisitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@ import { MdastImportVisitor } from '../../importMarkdownToLexical'
export const MdastMdxJsxElementVisitor: MdastImportVisitor<MdxJsxTextElement> = {
testNode: (node, { jsxComponentDescriptors }) => {
if (node.type === 'mdxJsxTextElement' || node.type === 'mdxJsxFlowElement') {
const descriptor = jsxComponentDescriptors.find((descriptor) => descriptor.name === node.name)
const descriptor =
jsxComponentDescriptors.find((descriptor) => descriptor.name === node.name) ??
jsxComponentDescriptors.find((descriptor) => descriptor.name === '*')
return descriptor !== undefined
}
return false
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 @@ -56,8 +56,9 @@ export interface JsxPropertyDescriptor {
*/
export interface JsxComponentDescriptor {
/**
* The tag name. For example: 'div', 'span', 'MyComponent'.
* The tag name. For example: 'div', 'span', 'MyComponent'. Use '*' for any tag.
* Note: For fragments, use null.
*
*/
name: string | null
/**
Expand Down

0 comments on commit c6d1067

Please sign in to comment.