2025-12-07 19:32:53 +02:00
|
|
|
import React from 'react';
|
2026-04-25 16:56:37 +03:00
|
|
|
import { lazyWithChunkRecovery } from '@/lib/chunkLoadRecovery';
|
2026-07-03 01:34:04 +03:00
|
|
|
import { isMobileSurfaceRuntime } from '@/lib/runtimeSurface';
|
|
|
|
|
import { cn } from '@/lib/utils';
|
|
|
|
|
import { loadMarkdownRendererModule } from './markdownRendererLoader';
|
2025-12-07 19:32:53 +02:00
|
|
|
|
2026-06-15 18:17:38 +03:00
|
|
|
// Thin lazy wrapper around the MarkdownRenderer implementation.
|
|
|
|
|
// The full implementation (marked + Shiki highlighting + KaTeX + morphdom
|
|
|
|
|
// DOM morphing, plus beautiful-mermaid) is loaded on demand, keeping the
|
2026-04-23 02:31:42 -07:00
|
|
|
// initial bundle lean.
|
2025-12-15 00:20:26 +02:00
|
|
|
|
2026-04-25 16:56:37 +03:00
|
|
|
const MarkdownRendererLazy = lazyWithChunkRecovery(() =>
|
2026-07-03 01:34:04 +03:00
|
|
|
loadMarkdownRendererModule().then((m) => ({ default: m.MarkdownRenderer }))
|
2026-04-23 02:31:42 -07:00
|
|
|
);
|
2026-03-20 19:11:45 +08:00
|
|
|
|
2026-04-25 16:56:37 +03:00
|
|
|
const SimpleMarkdownRendererLazy = lazyWithChunkRecovery(() =>
|
2026-07-03 01:34:04 +03:00
|
|
|
loadMarkdownRendererModule().then((m) => ({ default: m.SimpleMarkdownRenderer }))
|
2026-04-23 02:31:42 -07:00
|
|
|
);
|
2026-03-20 19:11:45 +08:00
|
|
|
|
2026-04-23 02:31:42 -07:00
|
|
|
const fallback = <div className="break-words w-full min-w-0" />;
|
2026-03-20 19:11:45 +08:00
|
|
|
|
2026-07-03 01:34:04 +03:00
|
|
|
const fallbackContentClassName = (variant: unknown): string => {
|
|
|
|
|
if (variant === 'tool') return 'markdown-content markdown-tool';
|
|
|
|
|
if (variant === 'reasoning') return 'markdown-content markdown-reasoning';
|
|
|
|
|
return 'markdown-content leading-relaxed';
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
const MobileMarkdownFallback = (props: { content?: unknown; className?: unknown; variant?: unknown }) => {
|
|
|
|
|
if (!isMobileSurfaceRuntime() || typeof props.content !== 'string' || props.content.length === 0) {
|
|
|
|
|
return fallback;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return (
|
|
|
|
|
<div className={cn('break-words w-full min-w-0 whitespace-pre-wrap', fallbackContentClassName(props.variant), typeof props.className === 'string' ? props.className : undefined)}>
|
|
|
|
|
{props.content}
|
|
|
|
|
</div>
|
|
|
|
|
);
|
|
|
|
|
};
|
|
|
|
|
|
2026-04-23 02:31:42 -07:00
|
|
|
export const MarkdownRenderer: React.FC<React.ComponentPropsWithoutRef<typeof MarkdownRendererLazy>> = (props) => (
|
2026-07-03 01:34:04 +03:00
|
|
|
<React.Suspense fallback={<MobileMarkdownFallback {...props} />}>
|
2026-04-23 02:31:42 -07:00
|
|
|
<MarkdownRendererLazy {...props} />
|
|
|
|
|
</React.Suspense>
|
|
|
|
|
);
|
2026-03-20 19:11:45 +08:00
|
|
|
|
2026-04-23 02:31:42 -07:00
|
|
|
export const SimpleMarkdownRenderer: React.FC<React.ComponentPropsWithoutRef<typeof SimpleMarkdownRendererLazy>> = (props) => (
|
2026-07-03 01:34:04 +03:00
|
|
|
<React.Suspense fallback={<MobileMarkdownFallback {...props} />}>
|
2026-04-23 02:31:42 -07:00
|
|
|
<SimpleMarkdownRendererLazy {...props} />
|
|
|
|
|
</React.Suspense>
|
|
|
|
|
);
|