Skip to content

Commit 2df8820

Browse files
committed
CodeBlock fallback handling for unsupported languages
1 parent 5542a99 commit 2df8820

File tree

1 file changed

+59
-26
lines changed

1 file changed

+59
-26
lines changed

frontend/packages/ui/src/blocks-content.tsx

Lines changed: 59 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ import React, {
8787
useState,
8888
} from 'react'
8989
import {createPortal} from 'react-dom'
90+
import {ErrorBoundary} from 'react-error-boundary'
9091
import {contentLayoutUnit, contentTextUnit} from './blocks-content-constants'
9192
import './blocks-content.css'
9293
import {Button} from './button'
@@ -2541,12 +2542,34 @@ export function BlockContentWebEmbed({
25412542
)
25422543
}
25432544

2545+
function CodeHighlight({node}: {node: any}) {
2546+
if (node.type === 'text') {
2547+
return node.value
2548+
}
2549+
2550+
if (node.type === 'element') {
2551+
const {tagName, properties, children} = node
2552+
if (properties.className && Array.isArray(properties.className)) {
2553+
properties.className = properties.className[0]
2554+
}
2555+
return createElement(
2556+
tagName,
2557+
{...properties},
2558+
children &&
2559+
children.map((child: any, index: number) => (
2560+
<CodeHighlight key={index} node={child} />
2561+
)),
2562+
)
2563+
}
2564+
2565+
return null
2566+
}
2567+
25442568
export function BlockContentCode({
25452569
block,
25462570
parentBlockId,
25472571
...props
25482572
}: BlockContentProps<HMBlockCode>) {
2549-
const layoutUnit = useLayoutUnit()
25502573
const language =
25512574
block.type === 'Code'
25522575
? getBlockAttribute(block.attributes, 'language')
@@ -2562,37 +2585,48 @@ export function BlockContentCode({
25622585
/>
25632586
)
25642587
}
2588+
return (
2589+
<ErrorBoundary fallback={<FallbackCodeBlock value={block.text} />}>
2590+
<CodeContent language={language} value={block.text} />
2591+
</ErrorBoundary>
2592+
)
2593+
}
2594+
2595+
function FallbackCodeBlock({value}: {value: string}) {
2596+
const layoutUnit = useLayoutUnit()
2597+
return (
2598+
<pre
2599+
data-content-type="code"
2600+
className={cn(
2601+
blockStyles,
2602+
`border-border bg-background w-full overflow-auto rounded-md border`,
2603+
)}
2604+
style={
2605+
{
2606+
padding: layoutUnit / 2,
2607+
marginLeft: (-1 * layoutUnit) / 2,
2608+
marginRight: (-1 * layoutUnit) / 2,
2609+
} as React.CSSProperties
2610+
}
2611+
>
2612+
<code className="font-mono text-sm leading-relaxed whitespace-pre-wrap">
2613+
{value}
2614+
</code>
2615+
</pre>
2616+
)
2617+
}
2618+
2619+
function CodeContent({language, value}: {language: string; value: string}) {
2620+
const layoutUnit = useLayoutUnit()
25652621

25662622
function getHighlightNodes(result: any) {
25672623
return result.value || result.children || []
25682624
}
25692625

2570-
const CodeHighlight = ({node}: {node: any}) => {
2571-
if (node.type === 'text') {
2572-
return node.value
2573-
}
2574-
2575-
if (node.type === 'element') {
2576-
const {tagName, properties, children} = node
2577-
if (properties.className && Array.isArray(properties.className)) {
2578-
properties.className = properties.className[0]
2579-
}
2580-
return createElement(
2581-
tagName,
2582-
{...properties},
2583-
children &&
2584-
children.map((child: any, index: number) => (
2585-
<CodeHighlight key={index} node={child} />
2586-
)),
2587-
)
2588-
}
2589-
2590-
return null
2591-
}
25922626
const lowlight = useLowlight(common)
25932627
const nodes: any[] =
25942628
language && language.length > 0
2595-
? getHighlightNodes(lowlight.highlight(language, block.text))
2629+
? getHighlightNodes(lowlight.highlight(language, value))
25962630
: []
25972631

25982632
return (
@@ -2609,14 +2643,13 @@ export function BlockContentCode({
26092643
marginRight: (-1 * layoutUnit) / 2,
26102644
} as React.CSSProperties
26112645
}
2612-
{...props}
26132646
>
26142647
<code className="font-mono text-sm leading-relaxed whitespace-pre-wrap">
26152648
{nodes.length > 0
26162649
? nodes.map((node, index) => (
26172650
<CodeHighlight key={index} node={node} />
26182651
))
2619-
: block.text}
2652+
: value}
26202653
</code>
26212654
</pre>
26222655
)

0 commit comments

Comments
 (0)