From 7dd47736fba8fe13628d190a02a3915794a9bcd3 Mon Sep 17 00:00:00 2001 From: bggRGjQaUbCoE Date: Thu, 13 Feb 2025 21:33:52 +0800 Subject: [PATCH] opt: better url pattern Signed-off-by: bggRGjQaUbCoE --- lib/common/constants.dart | 3 +++ lib/pages/video/detail/introduction/view.dart | 6 ++++-- .../video/detail/introduction/widgets/intro_detail.dart | 3 ++- lib/pages/video/detail/reply/widgets/reply_item.dart | 7 ++++--- lib/pages/video/detail/reply/widgets/reply_item_grpc.dart | 8 ++++---- lib/pages/video/detail/widgets/ai_detail.dart | 3 ++- 6 files changed, 19 insertions(+), 11 deletions(-) diff --git a/lib/common/constants.dart b/lib/common/constants.dart index f3fb3d12..53e98833 100644 --- a/lib/common/constants.dart +++ b/lib/common/constants.dart @@ -26,6 +26,9 @@ class Constants { '%7B%22appId%22%3A5%2C%22platform%22%3A3%2C%22version%22%3A%221.46.2%22%2C%22abtest%22%3A%22%22%7D'; //Uri.encodeComponent('{"appId": 5,"platform": 3,"version": "1.46.2","abtest": ""}'); + static const urlPattern = + r'https?://[-A-Za-z0-9+&@#/%?=~_|!:,.;]+[-A-Za-z0-9+&@#/%=~_|]'; + // 超分辨率滤镜 static const List mpvAnime4KShaders = [ 'Anime4K_Clamp_Highlights.glsl', diff --git a/lib/pages/video/detail/introduction/view.dart b/lib/pages/video/detail/introduction/view.dart index 4ec968e4..0bd64d28 100644 --- a/lib/pages/video/detail/introduction/view.dart +++ b/lib/pages/video/detail/introduction/view.dart @@ -1002,8 +1002,10 @@ class _VideoInfoState extends State with TickerProviderStateMixin { switch (currentDesc.type) { case 1: final List spanChildren = []; - final RegExp urlRegExp = - RegExp(r'https?://\S+\b|av\d+|bv\S+\b', caseSensitive: false); + final RegExp urlRegExp = RegExp( + '${Constants.urlPattern}|av\\d+|bv\\S+\\b', + caseSensitive: false, + ); (currentDesc.rawText as String).splitMapJoin( urlRegExp, diff --git a/lib/pages/video/detail/introduction/widgets/intro_detail.dart b/lib/pages/video/detail/introduction/widgets/intro_detail.dart index b6d69eea..88c1ba78 100644 --- a/lib/pages/video/detail/introduction/widgets/intro_detail.dart +++ b/lib/pages/video/detail/introduction/widgets/intro_detail.dart @@ -1,3 +1,4 @@ +import 'package:PiliPlus/common/constants.dart'; import 'package:PiliPlus/pages/search/widgets/search_text.dart'; import 'package:flutter/gestures.dart'; import 'package:flutter/material.dart'; @@ -147,7 +148,7 @@ class IntroDetail extends StatelessWidget { switch (currentDesc.type) { case 1: final List spanChildren = []; - final RegExp urlRegExp = RegExp(r'https?://\S+\b'); + final RegExp urlRegExp = RegExp(Constants.urlPattern); final Iterable matches = urlRegExp.allMatches(currentDesc.rawText); diff --git a/lib/pages/video/detail/reply/widgets/reply_item.dart b/lib/pages/video/detail/reply/widgets/reply_item.dart index a3b088d5..ac48b855 100644 --- a/lib/pages/video/detail/reply/widgets/reply_item.dart +++ b/lib/pages/video/detail/reply/widgets/reply_item.dart @@ -1,5 +1,6 @@ import 'dart:math'; +import 'package:PiliPlus/common/constants.dart'; import 'package:PiliPlus/common/widgets/imageview.dart'; import 'package:PiliPlus/http/video.dart'; import 'package:PiliPlus/models/dynamics/result.dart'; @@ -704,7 +705,7 @@ class ReplyItem extends StatelessWidget { if (jumpUrlKeysList.isNotEmpty) { patternStr += '|${jumpUrlKeysList.map(RegExp.escape).join('|')}'; } - patternStr += r'|https?://\S+\b'; + patternStr += '|${Constants.urlPattern}'; final RegExp pattern = RegExp(patternStr); List matchedStrs = []; void addPlainTextSpan(str) { @@ -896,10 +897,10 @@ class ReplyItem extends StatelessWidget { }, ), ); - } else if (RegExp(r'https?://\S+\b').hasMatch(matchStr)) { + } else if (RegExp(Constants.urlPattern).hasMatch(matchStr)) { spanChildren.add( TextSpan( - text: ' $matchStr ', + text: matchStr, style: TextStyle( color: Theme.of(context).colorScheme.primary, ), diff --git a/lib/pages/video/detail/reply/widgets/reply_item_grpc.dart b/lib/pages/video/detail/reply/widgets/reply_item_grpc.dart index b716f7ca..4e45e230 100644 --- a/lib/pages/video/detail/reply/widgets/reply_item_grpc.dart +++ b/lib/pages/video/detail/reply/widgets/reply_item_grpc.dart @@ -1,5 +1,6 @@ import 'dart:math'; +import 'package:PiliPlus/common/constants.dart'; import 'package:PiliPlus/common/widgets/badge.dart'; import 'package:PiliPlus/common/widgets/imageview.dart'; import 'package:PiliPlus/grpc/app/main/community/reply/v1/reply.pb.dart'; @@ -101,7 +102,6 @@ class ReplyItemGrpc extends StatelessWidget { ); Widget _buildContent(context) { - print(replyItem.member.garbCardImage); return Column( children: [ if (ModuleAuthorModel.showDynDecorate && @@ -745,7 +745,7 @@ class ReplyItemGrpc extends StatelessWidget { if (jumpUrlKeysList.isNotEmpty) { patternStr += '|${jumpUrlKeysList.map(RegExp.escape).join('|')}'; } - patternStr += r'|https?://\S+\b'; + patternStr += '|${Constants.urlPattern}'; final RegExp pattern = RegExp(patternStr); List matchedStrs = []; void addPlainTextSpan(str) { @@ -934,10 +934,10 @@ class ReplyItemGrpc extends StatelessWidget { }, ), ); - } else if (RegExp(r'https?://\S+\b').hasMatch(matchStr)) { + } else if (RegExp(Constants.urlPattern).hasMatch(matchStr)) { spanChildren.add( TextSpan( - text: ' $matchStr ', + text: matchStr, style: TextStyle( color: Theme.of(context).colorScheme.primary, ), diff --git a/lib/pages/video/detail/widgets/ai_detail.dart b/lib/pages/video/detail/widgets/ai_detail.dart index fb13e146..46cfa56b 100644 --- a/lib/pages/video/detail/widgets/ai_detail.dart +++ b/lib/pages/video/detail/widgets/ai_detail.dart @@ -1,3 +1,4 @@ +import 'package:PiliPlus/common/constants.dart'; import 'package:flutter/gestures.dart'; import 'package:flutter/material.dart'; import 'package:flutter_smart_dialog/flutter_smart_dialog.dart'; @@ -171,7 +172,7 @@ class AiDetail extends StatelessWidget { switch (currentDesc.type) { case 1: List spanChildren = []; - RegExp urlRegExp = RegExp(r'https?://\S+\b'); + RegExp urlRegExp = RegExp(Constants.urlPattern); Iterable matches = urlRegExp.allMatches(currentDesc.rawText); int previousEndIndex = 0;