diff --git a/app/lib/widgets/item/details/item_details_reddit.dart b/app/lib/widgets/item/details/item_details_reddit.dart index c3a4907..323db09 100644 --- a/app/lib/widgets/item/details/item_details_reddit.dart +++ b/app/lib/widgets/item/details/item_details_reddit.dart @@ -5,6 +5,7 @@ import 'package:feeddeck/models/source.dart'; import 'package:feeddeck/widgets/item/details/utils/item_description.dart'; import 'package:feeddeck/widgets/item/details/utils/item_subtitle.dart'; import 'package:feeddeck/widgets/item/details/utils/item_title.dart'; +import 'package:feeddeck/widgets/item/details/utils/item_youtube/item_youtube_video.dart'; class ItemDetailsReddit extends StatelessWidget { const ItemDetailsReddit({ @@ -16,6 +17,60 @@ class ItemDetailsReddit extends StatelessWidget { final FDItem item; final FDSource source; + /// [_getYoutubeUrl] returns a YouTube url when the provided [description] + /// contains a YouTube link. If the [description] does not contain a YouTube + /// link, the function returns `null`. + String? _getYoutubeUrl(String description) { + final exp = RegExp(r'(?:(?:https?|ftp):\/\/)?[\w/\-?=%.]+\.[\w/\-?=%.]+'); + final matches = exp.allMatches(description); + + for (var match in matches) { + final url = description.substring(match.start, match.end); + if (url.startsWith('https://youtu.be/') || + url.startsWith('https://www.youtube.com/watch?') || + url.startsWith('https://m.youtube.com/watch?')) { + return url; + } + } + + return null; + } + + /// [_buildDescription] builds the description widget for the item. If the + /// description contains a YouTube link, we render the [ItemYoutubeVideo] + /// and the [ItemDescription] widgets. If the description does not contain a + /// YouTube link, we only render the [ItemDescription] widget. + /// + /// If the description containes a YouTube link we also have to disable the + /// rendering of images within the [ItemDescription] widget. + List _buildDescription() { + final youtubeUrl = + item.description != null ? _getYoutubeUrl(item.description!) : null; + + if (youtubeUrl != null) { + return [ + ItemYoutubeVideo( + item.media, + youtubeUrl, + ), + ItemDescription( + itemDescription: item.description, + sourceFormat: DescriptionFormat.html, + tagetFormat: DescriptionFormat.markdown, + disableImages: true, + ), + ]; + } + + return [ + ItemDescription( + itemDescription: item.description, + sourceFormat: DescriptionFormat.html, + tagetFormat: DescriptionFormat.markdown, + ), + ]; + } + @override Widget build(BuildContext context) { return Column( @@ -29,11 +84,7 @@ class ItemDetailsReddit extends StatelessWidget { item: item, source: source, ), - ItemDescription( - itemDescription: item.description, - sourceFormat: DescriptionFormat.html, - tagetFormat: DescriptionFormat.markdown, - ), + ..._buildDescription(), ], ); }