Skip to content

Commit

Permalink
fix #29 remove duplicated slashes
Browse files Browse the repository at this point in the history
  • Loading branch information
LeetaoGoooo committed Jun 9, 2024
1 parent 062a7c9 commit 439037f
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions lib/common/link_helper.dart
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,40 @@ class LinkHelper {
return links.first.text;
}

static String removeDuplicateSlashes(String url) {
// Split the URL into components
Uri uri = Uri.parse(url);

// Get the scheme (e.g., https://)
String scheme = uri.scheme;

// Get the authority (e.g., www.baidu.com)
String authority = uri.authority;

// Get the path (e.g., /path/to/resource)
String path = uri.path.replaceAll(RegExp(r'/{2,}'), '/');

// Get the query (e.g., ?hello)
String query = uri.query;

// Get the fragment (e.g., #anchor)
String fragment = uri.fragment;

// Construct the new URL with no duplicated slashes
String newUrl = '$scheme://$authority$path';

// Append the query if it exists
if (query.isNotEmpty) {
newUrl += '?$query';
}

// Append the fragment if it exists
if (fragment.isNotEmpty) {
newUrl += '#$fragment';
}

return newUrl;
}

static getSubscriptionUrl(Radar radar) async {
Future<SharedPreferences> _prefs = SharedPreferences.getInstance();
Expand All @@ -27,6 +61,7 @@ class LinkHelper {
host = prefs.getString("RSSHUB")!;
}
var url = "$host${radar.path}";
url = removeDuplicateSlashes(url);
if (!radar.isRssHub) {
url = radar.path!;
}
Expand Down

0 comments on commit 439037f

Please sign in to comment.