Skip to content

Commit

Permalink
fix: fallback to to default code block language if no descriptor found
Browse files Browse the repository at this point in the history
Fixes #423
  • Loading branch information
petyosi committed Apr 19, 2024
1 parent 083f6fd commit 6c05223
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions src/plugins/codeblock/CodeBlockNode.tsx
Original file line number Diff line number Diff line change
@@ -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'

Expand Down Expand Up @@ -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}`)
}
Expand Down

0 comments on commit 6c05223

Please sign in to comment.