feat(blog): enhance markdown processing in blog, diet, plan, recipe, and my diet widgets to auto-convert URLs to markdown links and improve link handling

This commit is contained in:
mso7nueh
2025-12-03 18:20:44 +03:00
parent f3d4478c65
commit 0cd8e662d9
7 changed files with 485 additions and 63 deletions

View File

@@ -17,6 +17,7 @@ import 'package:flutter_svg/flutter_svg.dart';
import 'package:go_router/go_router.dart';
import 'package:markdown/markdown.dart' as md;
import 'package:share_plus/share_plus.dart';
import 'package:url_launcher/url_launcher.dart';
class MyRecipeWidget extends StatefulWidget {
final MyRecipeModel recipe;
@@ -94,12 +95,52 @@ class _MyRecipeWidgetState extends State<MyRecipeWidget> {
const SizedBox(height: 12),
Padding(
padding: const EdgeInsets.symmetric(horizontal: 16),
child: Html(
data: md.markdownToHtml(
widget.recipe.description.length > 200
? '${widget.recipe.description.substring(0, 200).replaceAll('\n', '<br>')}...'
: widget.recipe.description.replaceAll('\n', '<br>'),
),
child: Builder(
builder: (context) {
String description = widget.recipe.description.length > 200
? '${widget.recipe.description.substring(0, 200)}...'
: widget.recipe.description;
// Автоматически преобразуем URL в markdown ссылки
// Сначала исправляем неправильный синтаксис markdown ссылок
final wrongLinkRegex = RegExp(r'\[(https?://[^\]]+|www\.[^\]]+)\]\(([^\)]+)\)');
description = description.replaceAllMapped(wrongLinkRegex, (match) {
final urlInText = match.group(1)!;
if (!urlInText.startsWith('http://') && !urlInText.startsWith('https://') && !urlInText.startsWith('www.')) {
return match.group(0)!;
}
final fullUrl = urlInText.startsWith('www.') ? 'https://$urlInText' : urlInText;
return '[$urlInText]($fullUrl)';
});
final linkRegex = RegExp(r'\[([^\]]+)\]\(([^\)]+)\)');
final existingLinks = <_LinkRange>[];
for (final linkMatch in linkRegex.allMatches(description)) {
existingLinks.add(_LinkRange(linkMatch.start, linkMatch.end));
}
final urlRegex = RegExp(r'(https?://[^\s\)]+|www\.[^\s\)]+)', caseSensitive: false);
String result = description;
int offset = 0;
for (final urlMatch in urlRegex.allMatches(description)) {
bool isInsideLink = existingLinks.any((link) =>
urlMatch.start >= link.start && urlMatch.end <= link.end);
if (!isInsideLink) {
final url = urlMatch.group(0)!;
final fullUrl = url.startsWith('www.') ? 'https://$url' : url;
final replacement = '[$url]($fullUrl)';
result = result.replaceRange(
urlMatch.start + offset,
urlMatch.end + offset,
replacement,
);
offset += replacement.length - url.length;
}
}
description = result;
return Html(
data: md.markdownToHtml(
description.replaceAll('\n', '<br>'),
extensionSet: md.ExtensionSet.commonMark,
),
style: {
"body": Style(
margin: Margins.zero,
@@ -131,6 +172,16 @@ class _MyRecipeWidgetState extends State<MyRecipeWidget> {
"a": Style(color: Colors.blue, textDecoration: TextDecoration.underline),
"img": Style(width: Width(MediaQuery.of(context).size.width - 32)),
},
onLinkTap: (url, attributes, element) async {
if (url != null) {
final uri = Uri.parse(url);
if (await canLaunchUrl(uri)) {
await launchUrl(uri, mode: LaunchMode.externalApplication);
}
}
},
);
},
),
),
],
@@ -218,3 +269,9 @@ class _MyRecipeWidgetState extends State<MyRecipeWidget> {
);
}
}
class _LinkRange {
final int start;
final int end;
_LinkRange(this.start, this.end);
}