2025-08-20 17:01:46 +05:30
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
|
|
2025-09-30 20:49:02 +05:30
|
|
|
import 'markdown_config.dart';
|
2025-10-02 20:21:21 +05:30
|
|
|
import 'markdown_preprocessor.dart';
|
2025-08-20 17:01:46 +05:30
|
|
|
|
2025-10-06 00:09:52 +05:30
|
|
|
// Pre-compiled regex for mermaid diagram detection (performance optimization)
|
|
|
|
|
final _mermaidRegex = RegExp(r'```mermaid\s*([\s\S]*?)```', multiLine: true);
|
|
|
|
|
|
2025-12-06 10:56:57 +05:30
|
|
|
// Pre-compiled regex for HTML code blocks that may contain ChartJS
|
|
|
|
|
final _htmlBlockRegex = RegExp(r'```html\s*([\s\S]*?)```', multiLine: true);
|
|
|
|
|
|
2025-10-02 15:21:44 +05:30
|
|
|
class StreamingMarkdownWidget extends StatelessWidget {
|
2025-08-20 17:01:46 +05:30
|
|
|
const StreamingMarkdownWidget({
|
|
|
|
|
super.key,
|
2025-09-30 20:49:02 +05:30
|
|
|
required this.content,
|
2025-08-20 17:01:46 +05:30
|
|
|
required this.isStreaming,
|
2025-09-30 20:49:02 +05:30
|
|
|
this.onTapLink,
|
2025-10-10 16:12:31 +05:30
|
|
|
this.imageBuilderOverride,
|
2025-08-20 17:01:46 +05:30
|
|
|
});
|
|
|
|
|
|
2025-09-30 20:49:02 +05:30
|
|
|
final String content;
|
|
|
|
|
final bool isStreaming;
|
2025-10-02 13:52:28 +05:30
|
|
|
final MarkdownLinkTapCallback? onTapLink;
|
2025-10-10 16:12:31 +05:30
|
|
|
final Widget Function(Uri uri, String? title, String? alt)?
|
|
|
|
|
imageBuilderOverride;
|
2025-08-20 17:01:46 +05:30
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
Widget build(BuildContext context) {
|
2025-09-30 20:49:02 +05:30
|
|
|
if (content.trim().isEmpty) {
|
2025-10-02 20:21:21 +05:30
|
|
|
return const SizedBox.shrink();
|
2025-08-20 17:01:46 +05:30
|
|
|
}
|
|
|
|
|
|
2025-10-02 20:21:21 +05:30
|
|
|
final normalized = ConduitMarkdownPreprocessor.normalize(content);
|
2025-12-06 10:56:57 +05:30
|
|
|
|
|
|
|
|
// Collect all special blocks (Mermaid and ChartJS)
|
|
|
|
|
final specialBlocks = <_SpecialBlock>[];
|
|
|
|
|
|
|
|
|
|
// Find mermaid blocks
|
|
|
|
|
for (final match in _mermaidRegex.allMatches(normalized)) {
|
|
|
|
|
final code = match.group(1)?.trim() ?? '';
|
|
|
|
|
if (code.isNotEmpty) {
|
|
|
|
|
specialBlocks.add(
|
|
|
|
|
_SpecialBlock(
|
|
|
|
|
start: match.start,
|
|
|
|
|
end: match.end,
|
|
|
|
|
type: _BlockType.mermaid,
|
|
|
|
|
content: code,
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Find HTML blocks that contain ChartJS
|
|
|
|
|
for (final match in _htmlBlockRegex.allMatches(normalized)) {
|
|
|
|
|
final html = match.group(1)?.trim() ?? '';
|
|
|
|
|
if (html.isNotEmpty && ConduitMarkdown.containsChartJs(html)) {
|
|
|
|
|
specialBlocks.add(
|
|
|
|
|
_SpecialBlock(
|
|
|
|
|
start: match.start,
|
|
|
|
|
end: match.end,
|
|
|
|
|
type: _BlockType.chartJs,
|
|
|
|
|
content: html,
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Sort by position
|
|
|
|
|
specialBlocks.sort((a, b) => a.start.compareTo(b.start));
|
2025-10-03 16:29:21 +05:30
|
|
|
|
2025-10-04 16:04:49 +05:30
|
|
|
Widget buildMarkdown(String data) {
|
2025-12-07 22:01:18 +05:30
|
|
|
return ConduitMarkdown.build(
|
2025-10-04 23:05:03 +05:30
|
|
|
context: context,
|
2025-10-04 16:04:49 +05:30
|
|
|
data: data,
|
2025-10-04 23:05:03 +05:30
|
|
|
onTapLink: onTapLink,
|
2025-10-10 16:12:31 +05:30
|
|
|
imageBuilderOverride: imageBuilderOverride,
|
2025-10-04 16:04:49 +05:30
|
|
|
);
|
2025-10-04 13:37:47 +05:30
|
|
|
}
|
2025-10-03 13:37:57 +05:30
|
|
|
|
2025-12-07 22:01:18 +05:30
|
|
|
Widget result;
|
2025-10-03 16:29:21 +05:30
|
|
|
|
2025-12-07 22:01:18 +05:30
|
|
|
if (specialBlocks.isEmpty) {
|
|
|
|
|
result = buildMarkdown(normalized);
|
|
|
|
|
} else {
|
|
|
|
|
final children = <Widget>[];
|
|
|
|
|
var currentIndex = 0;
|
|
|
|
|
for (final block in specialBlocks) {
|
|
|
|
|
// Skip overlapping blocks
|
|
|
|
|
if (block.start < currentIndex) continue;
|
|
|
|
|
|
|
|
|
|
final before = normalized.substring(currentIndex, block.start);
|
|
|
|
|
if (before.trim().isNotEmpty) {
|
|
|
|
|
children.add(buildMarkdown(before));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
switch (block.type) {
|
|
|
|
|
case _BlockType.mermaid:
|
|
|
|
|
children.add(
|
|
|
|
|
ConduitMarkdown.buildMermaidBlock(context, block.content),
|
|
|
|
|
);
|
|
|
|
|
case _BlockType.chartJs:
|
|
|
|
|
children.add(
|
|
|
|
|
ConduitMarkdown.buildChartJsBlock(context, block.content),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
currentIndex = block.end;
|
2025-10-03 16:29:21 +05:30
|
|
|
}
|
|
|
|
|
|
2025-12-07 22:01:18 +05:30
|
|
|
final tail = normalized.substring(currentIndex);
|
|
|
|
|
if (tail.trim().isNotEmpty) {
|
|
|
|
|
children.add(buildMarkdown(tail));
|
2025-10-03 16:29:21 +05:30
|
|
|
}
|
|
|
|
|
|
2025-12-07 22:01:18 +05:30
|
|
|
result = Column(
|
|
|
|
|
crossAxisAlignment: CrossAxisAlignment.stretch,
|
|
|
|
|
children: children,
|
|
|
|
|
);
|
2025-10-03 16:29:21 +05:30
|
|
|
}
|
|
|
|
|
|
2025-12-07 22:01:18 +05:30
|
|
|
// Only wrap in SelectionArea when not streaming to avoid concurrent
|
|
|
|
|
// modification errors in Flutter's selection system during rapid updates
|
|
|
|
|
if (isStreaming) {
|
|
|
|
|
return result;
|
2025-10-03 16:29:21 +05:30
|
|
|
}
|
|
|
|
|
|
2025-12-07 22:01:18 +05:30
|
|
|
return SelectionArea(child: result);
|
2025-08-20 17:01:46 +05:30
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-12-06 10:56:57 +05:30
|
|
|
/// Types of special blocks that need custom rendering
|
|
|
|
|
enum _BlockType { mermaid, chartJs }
|
|
|
|
|
|
|
|
|
|
/// Represents a special block in the content
|
|
|
|
|
class _SpecialBlock {
|
|
|
|
|
final int start;
|
|
|
|
|
final int end;
|
|
|
|
|
final _BlockType type;
|
|
|
|
|
final String content;
|
|
|
|
|
|
|
|
|
|
const _SpecialBlock({
|
|
|
|
|
required this.start,
|
|
|
|
|
required this.end,
|
|
|
|
|
required this.type,
|
|
|
|
|
required this.content,
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2025-08-20 17:01:46 +05:30
|
|
|
extension StreamingMarkdownExtension on String {
|
2025-10-02 20:21:21 +05:30
|
|
|
Widget toMarkdown({
|
|
|
|
|
required BuildContext context,
|
|
|
|
|
bool isStreaming = false,
|
|
|
|
|
MarkdownLinkTapCallback? onTapLink,
|
|
|
|
|
}) {
|
|
|
|
|
return StreamingMarkdownWidget(
|
|
|
|
|
content: this,
|
|
|
|
|
isStreaming: isStreaming,
|
|
|
|
|
onTapLink: onTapLink,
|
|
|
|
|
);
|
2025-08-20 17:01:46 +05:30
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class MarkdownWithLoading extends StatelessWidget {
|
2025-09-30 20:49:02 +05:30
|
|
|
const MarkdownWithLoading({super.key, this.content, required this.isLoading});
|
|
|
|
|
|
2025-08-20 17:01:46 +05:30
|
|
|
final String? content;
|
|
|
|
|
final bool isLoading;
|
|
|
|
|
|
|
|
|
|
@override
|
|
|
|
|
Widget build(BuildContext context) {
|
2025-09-30 20:49:02 +05:30
|
|
|
final value = content ?? '';
|
2025-10-02 20:21:21 +05:30
|
|
|
if (isLoading && value.trim().isEmpty) {
|
2025-09-30 16:02:34 +05:30
|
|
|
return const Center(child: CircularProgressIndicator());
|
2025-08-20 17:01:46 +05:30
|
|
|
}
|
|
|
|
|
|
2025-09-30 20:49:02 +05:30
|
|
|
return StreamingMarkdownWidget(content: value, isStreaming: isLoading);
|
2025-08-20 17:01:46 +05:30
|
|
|
}
|
2025-09-24 12:00:49 +05:30
|
|
|
}
|