-
Notifications
You must be signed in to change notification settings - Fork 18
Node Package
npm install tidy-url
import { TidyURL } from 'tidy-url';
// or
const { TidyURL } = require('tidy-url');
Then pass it a URL and let the magic happen:
const link = TidyURL.clean('https://open.spotify.com/track/1hhZQVLXpg10ySFQFxGbih?si=-k8RwDQwTCK923jxZuy07w&utm_source=copy-link');
console.log(link.url); // https://open.spotify.com/track/1hhZQVLXpg10ySFQFxGbih
You can validate a URL using the validate
function.
TidyURL.validate('https://example.com'); // true
TidyURL.validate('cat'); // false
TidyURL.validate('google.com'); // false (protocol is required!)
By default, tidy-url will remove redirect parameters and AMP links if the rule supports it.
You can disable this feature with allowRedirects
and allowAMP
.
Examples:
// These are the defaults.
TidyURL.config.setMany({
allowAMP: false,
allowRedirects: true
});
TidyURL.clean('https://www.google.com/amp/s/github.com');
TidyURL.clean('https://steamcommunity.com/linkfilter/?url=https://github.com');
// Result for both: https://github.com
More info about AMP on the wiki.
You will always receive a valid response, even if nothing was modified. For example:
const link = TidyURL.clean('https://duckduckgo.com/this-is-fine');
link.url; // https://duckduckgo.com/this-is-fine
link.info.reduction; // 0 (percent)
You can view all custom supported sites here.
However, the global rules will be enough to work with thousands of sites around the internet. You should be able to pass any URL for cleaning.
Request direct support for a website here
The response will always be an object with details of what was cleaned or modified in the URL.
This can be used for debugging, testing or a simple way of letting users know they could have sent a shorter link.
{
"url": "https://open.spotify.com/track/1hhZQVLXpg10ySFQFxGbih",
"info": {
"original": "https://open.spotify.com/track/1hhZQVLXpg10ySFQFxGbih?si=-k8RwDQwTCK923jxZuy07w&utm_source=copy-link",
"reduction": 47,
"difference": 47,
"replace": [],
"removed": [
{
"key": "utm_source",
"value": "copy-link"
},
{
"key": "si",
"value": "-k8RwDQwTCK923jxZuy07w"
}
],
"match": [
{
"rules": ["si", "utm_source", "context"],
"replace": [],
"redirect": "",
"name": "spotify.com",
"match": "/open.spotify.com/i"
}
],
"decoded": null,
"isNewHost": false,
"fullClean": true
}
}
Questions? Comments? Need help?
Feel free to contact me on Discord: drkain