From 6c05223b6e1f5884d93a5a5d215526945a190e12 Mon Sep 17 00:00:00 2001 From: Petyo Ivanov Date: Fri, 19 Apr 2024 09:37:09 +0300 Subject: [PATCH] fix: fallback to to default code block language if no descriptor found Fixes #423 --- src/plugins/codeblock/CodeBlockNode.tsx | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/plugins/codeblock/CodeBlockNode.tsx b/src/plugins/codeblock/CodeBlockNode.tsx index 875e0eec..dfa1c2bb 100644 --- a/src/plugins/codeblock/CodeBlockNode.tsx +++ b/src/plugins/codeblock/CodeBlockNode.tsx @@ -1,7 +1,7 @@ import { useCellValue } from '@mdxeditor/gurx' import { DecoratorNode, EditorConfig, LexicalEditor, LexicalNode, NodeKey, SerializedLexicalNode, Spread } from 'lexical' import React from 'react' -import { CodeBlockEditorProps } from '.' +import { CodeBlockEditorProps, defaultCodeBlockLanguage$ } from '.' import { voidEmitter } from '../../utils/voidEmitter' import { NESTED_EDITOR_UPDATED_COMMAND, codeBlockEditorDescriptors$ } from '../core' @@ -218,11 +218,16 @@ const CodeBlockEditorContainer: React.FC< } & CodeBlockEditorProps > = (props) => { const codeBlockEditorDescriptors = useCellValue(codeBlockEditorDescriptors$) + const defaultCodeBlockLanguage = useCellValue(defaultCodeBlockLanguage$) - const descriptor = codeBlockEditorDescriptors + let descriptor = codeBlockEditorDescriptors .sort((a, b) => b.priority - a.priority) .find((descriptor) => descriptor.match(props.language || '', props.meta || '')) + if (!descriptor) { + descriptor = codeBlockEditorDescriptors.find((descriptor) => descriptor.match(defaultCodeBlockLanguage || '', props.meta || '')) + } + if (!descriptor) { throw new Error(`No CodeBlockEditor registered for language=${props.language} meta=${props.meta}`) }