2025-08-20 18:39:30 +05:30
|
|
|
import 'dart:convert';
|
2025-09-30 20:49:02 +05:30
|
|
|
|
2025-08-20 17:01:46 +05:30
|
|
|
import 'package:cached_network_image/cached_network_image.dart';
|
2025-09-30 20:49:02 +05:30
|
|
|
import 'package:flutter/material.dart';
|
|
|
|
|
import 'package:flutter_markdown_plus/flutter_markdown_plus.dart';
|
|
|
|
|
import 'package:markdown/markdown.dart' as md;
|
|
|
|
|
|
2025-08-23 20:09:43 +05:30
|
|
|
import 'package:conduit/l10n/app_localizations.dart';
|
2025-08-20 17:01:46 +05:30
|
|
|
|
2025-09-30 20:49:02 +05:30
|
|
|
import '../../theme/theme_extensions.dart';
|
2025-09-30 16:02:34 +05:30
|
|
|
|
2025-09-30 20:49:02 +05:30
|
|
|
class ConduitMarkdownTheme {
|
|
|
|
|
const ConduitMarkdownTheme({
|
|
|
|
|
required this.styleSheet,
|
|
|
|
|
required this.builders,
|
|
|
|
|
required this.imageBuilder,
|
|
|
|
|
this.inlineSyntaxes = const <md.InlineSyntax>[],
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
final MarkdownStyleSheet styleSheet;
|
|
|
|
|
final Map<String, MarkdownElementBuilder> builders;
|
|
|
|
|
final MarkdownImageBuilder imageBuilder;
|
|
|
|
|
final List<md.InlineSyntax> inlineSyntaxes;
|
2025-09-30 16:02:34 +05:30
|
|
|
}
|
|
|
|
|
|
2025-08-20 17:01:46 +05:30
|
|
|
class ConduitMarkdownConfig {
|
2025-09-30 20:49:02 +05:30
|
|
|
static ConduitMarkdownTheme resolve(BuildContext context) {
|
2025-08-20 17:01:46 +05:30
|
|
|
final theme = context.conduitTheme;
|
2025-09-30 20:49:02 +05:30
|
|
|
final materialTheme = Theme.of(context);
|
|
|
|
|
|
|
|
|
|
final baseSheet = MarkdownStyleSheet.fromTheme(materialTheme);
|
|
|
|
|
final bodyStyle = AppTypography.bodyMediumStyle.copyWith(
|
|
|
|
|
color: theme.textPrimary,
|
|
|
|
|
height: 1.45,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
final codeColor = theme.code?.color ?? theme.textSecondary;
|
2025-08-20 17:01:46 +05:30
|
|
|
|
2025-09-30 20:49:02 +05:30
|
|
|
final styleSheet = baseSheet.copyWith(
|
|
|
|
|
p: bodyStyle,
|
|
|
|
|
h1: AppTypography.headlineLargeStyle.copyWith(color: theme.textPrimary),
|
|
|
|
|
h2: AppTypography.headlineMediumStyle.copyWith(color: theme.textPrimary),
|
|
|
|
|
h3: AppTypography.headlineSmallStyle.copyWith(color: theme.textPrimary),
|
|
|
|
|
strong: bodyStyle.copyWith(fontWeight: FontWeight.w600),
|
|
|
|
|
em: bodyStyle.copyWith(fontStyle: FontStyle.italic),
|
|
|
|
|
blockquote: bodyStyle.copyWith(
|
|
|
|
|
color: theme.textSecondary,
|
|
|
|
|
fontStyle: FontStyle.italic,
|
2025-09-30 16:02:34 +05:30
|
|
|
),
|
2025-09-30 20:49:02 +05:30
|
|
|
code: AppTypography.codeStyle.copyWith(color: codeColor),
|
|
|
|
|
listBullet: bodyStyle,
|
|
|
|
|
tableBody: bodyStyle,
|
|
|
|
|
tableHead: bodyStyle.copyWith(fontWeight: FontWeight.w600),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
final builders = <String, MarkdownElementBuilder>{
|
|
|
|
|
'codeblock': _ConduitCodeBlockBuilder(theme),
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
return ConduitMarkdownTheme(
|
|
|
|
|
styleSheet: styleSheet,
|
|
|
|
|
builders: builders,
|
|
|
|
|
imageBuilder: (uri, title, alt) {
|
|
|
|
|
final scheme = uri.scheme;
|
|
|
|
|
|
|
|
|
|
if (scheme == 'data') {
|
|
|
|
|
return buildBase64Image(uri.toString(), context, theme);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (scheme.isEmpty || scheme == 'http' || scheme == 'https') {
|
|
|
|
|
return buildNetworkImage(uri.toString(), context, theme);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return const SizedBox.shrink();
|
|
|
|
|
},
|
2025-08-20 17:01:46 +05:30
|
|
|
);
|
|
|
|
|
}
|
2025-08-20 18:39:30 +05:30
|
|
|
|
2025-09-30 16:02:34 +05:30
|
|
|
static Widget buildBase64Image(
|
2025-09-24 12:00:49 +05:30
|
|
|
String dataUrl,
|
|
|
|
|
BuildContext context,
|
|
|
|
|
ConduitThemeExtension theme,
|
|
|
|
|
) {
|
2025-08-20 18:39:30 +05:30
|
|
|
try {
|
|
|
|
|
final commaIndex = dataUrl.indexOf(',');
|
|
|
|
|
if (commaIndex == -1) {
|
|
|
|
|
throw Exception('Invalid data URL format');
|
|
|
|
|
}
|
2025-08-20 22:15:26 +05:30
|
|
|
|
2025-08-20 18:39:30 +05:30
|
|
|
final base64String = dataUrl.substring(commaIndex + 1);
|
|
|
|
|
final imageBytes = base64.decode(base64String);
|
2025-08-20 22:15:26 +05:30
|
|
|
|
2025-08-20 18:39:30 +05:30
|
|
|
return Container(
|
|
|
|
|
margin: const EdgeInsets.symmetric(vertical: Spacing.sm),
|
2025-08-20 22:15:26 +05:30
|
|
|
constraints: const BoxConstraints(maxWidth: 500, maxHeight: 500),
|
2025-08-20 18:39:30 +05:30
|
|
|
child: ClipRRect(
|
|
|
|
|
borderRadius: BorderRadius.circular(AppBorderRadius.md),
|
|
|
|
|
child: Image.memory(
|
|
|
|
|
imageBytes,
|
|
|
|
|
fit: BoxFit.contain,
|
|
|
|
|
errorBuilder: (context, error, stackTrace) {
|
2025-09-30 20:49:02 +05:30
|
|
|
return _buildImageError(context, theme);
|
2025-08-20 18:39:30 +05:30
|
|
|
},
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
);
|
|
|
|
|
} catch (e) {
|
2025-09-30 20:49:02 +05:30
|
|
|
return _buildImageError(context, theme);
|
2025-08-20 18:39:30 +05:30
|
|
|
}
|
|
|
|
|
}
|
2025-09-30 16:02:34 +05:30
|
|
|
|
|
|
|
|
static Widget buildNetworkImage(
|
|
|
|
|
String url,
|
|
|
|
|
BuildContext context,
|
|
|
|
|
ConduitThemeExtension theme,
|
|
|
|
|
) {
|
|
|
|
|
return CachedNetworkImage(
|
|
|
|
|
imageUrl: url,
|
|
|
|
|
placeholder: (context, url) => Container(
|
|
|
|
|
height: 200,
|
|
|
|
|
decoration: BoxDecoration(
|
|
|
|
|
color: theme.surfaceBackground.withValues(alpha: 0.5),
|
|
|
|
|
borderRadius: BorderRadius.circular(AppBorderRadius.md),
|
|
|
|
|
),
|
|
|
|
|
child: Center(
|
|
|
|
|
child: CircularProgressIndicator(
|
|
|
|
|
color: theme.loadingIndicator,
|
|
|
|
|
strokeWidth: 2,
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
),
|
2025-09-30 20:49:02 +05:30
|
|
|
errorWidget: (context, url, error) => _buildImageError(context, theme),
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static Widget _buildImageError(
|
|
|
|
|
BuildContext context,
|
|
|
|
|
ConduitThemeExtension theme,
|
|
|
|
|
) {
|
|
|
|
|
return Container(
|
|
|
|
|
height: 100,
|
|
|
|
|
decoration: BoxDecoration(
|
|
|
|
|
color: theme.surfaceBackground.withValues(alpha: 0.3),
|
|
|
|
|
borderRadius: BorderRadius.circular(AppBorderRadius.md),
|
|
|
|
|
border: Border.all(
|
|
|
|
|
color: theme.error.withValues(alpha: 0.3),
|
|
|
|
|
width: BorderWidth.thin,
|
2025-09-30 16:02:34 +05:30
|
|
|
),
|
|
|
|
|
),
|
2025-09-30 20:49:02 +05:30
|
|
|
child: Column(
|
|
|
|
|
mainAxisAlignment: MainAxisAlignment.center,
|
|
|
|
|
children: [
|
|
|
|
|
Icon(Icons.broken_image_outlined, color: theme.error, size: 32),
|
|
|
|
|
const SizedBox(height: Spacing.xs),
|
|
|
|
|
Text(
|
|
|
|
|
AppLocalizations.of(context)!.failedToLoadImage(''),
|
|
|
|
|
style: TextStyle(color: theme.error, fontSize: 12),
|
|
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
),
|
2025-09-30 16:02:34 +05:30
|
|
|
);
|
|
|
|
|
}
|
2025-08-20 17:01:46 +05:30
|
|
|
}
|
|
|
|
|
|
2025-09-30 20:49:02 +05:30
|
|
|
class _ConduitCodeBlockBuilder extends MarkdownElementBuilder {
|
|
|
|
|
_ConduitCodeBlockBuilder(this.theme);
|
|
|
|
|
|
2025-08-20 17:01:46 +05:30
|
|
|
final ConduitThemeExtension theme;
|
|
|
|
|
|
2025-09-30 20:49:02 +05:30
|
|
|
@override
|
|
|
|
|
Widget? visitElementAfter(md.Element element, TextStyle? preferredStyle) {
|
|
|
|
|
final rawText = element.textContent;
|
|
|
|
|
final classAttribute = element.attributes['class'];
|
|
|
|
|
String? language;
|
|
|
|
|
if (classAttribute != null && classAttribute.startsWith('language-')) {
|
|
|
|
|
language = classAttribute.substring('language-'.length);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
final textStyle = (preferredStyle ?? AppTypography.codeStyle).copyWith(
|
|
|
|
|
color: theme.code?.color ?? theme.textSecondary,
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
final container = Container(
|
|
|
|
|
width: double.infinity,
|
|
|
|
|
margin: const EdgeInsets.symmetric(vertical: Spacing.xs),
|
|
|
|
|
padding: const EdgeInsets.all(Spacing.sm),
|
|
|
|
|
decoration: BoxDecoration(
|
|
|
|
|
color: theme.surfaceBackground.withValues(alpha: 0.6),
|
|
|
|
|
borderRadius: BorderRadius.circular(AppBorderRadius.md),
|
|
|
|
|
border: Border.all(
|
|
|
|
|
color: theme.cardBorder.withValues(alpha: 0.2),
|
|
|
|
|
width: BorderWidth.micro,
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
child: SelectableText(rawText, style: textStyle),
|
|
|
|
|
);
|
|
|
|
|
|
|
|
|
|
return CodeBlockWrapper(
|
|
|
|
|
code: rawText,
|
|
|
|
|
language: language,
|
|
|
|
|
theme: theme,
|
|
|
|
|
child: container,
|
|
|
|
|
);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
class CodeBlockWrapper extends StatelessWidget {
|
2025-08-20 17:01:46 +05:30
|
|
|
const CodeBlockWrapper({
|
|
|
|
|
super.key,
|
|
|
|
|
required this.child,
|
|
|
|
|
required this.code,
|
|
|
|
|
this.language,
|
|
|
|
|
required this.theme,
|
|
|
|
|
});
|
|
|
|
|
|
2025-09-30 20:49:02 +05:30
|
|
|
final Widget child;
|
|
|
|
|
final String code;
|
|
|
|
|
final String? language;
|
|
|
|
|
final ConduitThemeExtension theme;
|
|
|
|
|
|
2025-08-20 17:01:46 +05:30
|
|
|
@override
|
|
|
|
|
Widget build(BuildContext context) {
|
|
|
|
|
return Stack(
|
|
|
|
|
children: [
|
|
|
|
|
child,
|
|
|
|
|
Positioned(
|
|
|
|
|
top: 8,
|
|
|
|
|
right: 8,
|
|
|
|
|
child: Material(
|
2025-08-20 18:39:30 +05:30
|
|
|
color: theme.surfaceBackground.withValues(alpha: 0.0),
|
2025-08-20 17:01:46 +05:30
|
|
|
child: InkWell(
|
|
|
|
|
borderRadius: BorderRadius.circular(AppBorderRadius.sm),
|
|
|
|
|
onTap: () {
|
2025-09-30 20:49:02 +05:30
|
|
|
// Copy implementation provided by higher level clipboard service.
|
2025-08-20 17:01:46 +05:30
|
|
|
},
|
|
|
|
|
child: Container(
|
|
|
|
|
padding: const EdgeInsets.all(Spacing.xs),
|
|
|
|
|
decoration: BoxDecoration(
|
|
|
|
|
color: theme.surfaceBackground.withValues(alpha: 0.8),
|
|
|
|
|
borderRadius: BorderRadius.circular(AppBorderRadius.sm),
|
|
|
|
|
),
|
|
|
|
|
child: Icon(
|
|
|
|
|
Icons.copy,
|
|
|
|
|
size: IconSize.sm,
|
|
|
|
|
color: theme.iconSecondary,
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
if (language != null)
|
|
|
|
|
Positioned(
|
|
|
|
|
top: 8,
|
|
|
|
|
left: 8,
|
|
|
|
|
child: Container(
|
|
|
|
|
padding: const EdgeInsets.symmetric(
|
|
|
|
|
horizontal: Spacing.sm,
|
|
|
|
|
vertical: Spacing.xxs,
|
|
|
|
|
),
|
|
|
|
|
decoration: BoxDecoration(
|
|
|
|
|
color: theme.surfaceBackground.withValues(alpha: 0.8),
|
|
|
|
|
borderRadius: BorderRadius.circular(AppBorderRadius.xs),
|
|
|
|
|
),
|
|
|
|
|
child: Text(
|
|
|
|
|
language!,
|
2025-09-30 20:49:02 +05:30
|
|
|
style: AppTypography.bodySmallStyle.copyWith(
|
2025-08-20 17:01:46 +05:30
|
|
|
color: theme.textSecondary,
|
2025-09-30 20:49:02 +05:30
|
|
|
fontFamily: AppTypography.monospaceFontFamily,
|
2025-08-20 17:01:46 +05:30
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
),
|
|
|
|
|
],
|
|
|
|
|
);
|
|
|
|
|
}
|
2025-08-20 22:15:26 +05:30
|
|
|
}
|