Skip to content

Node Package

Kain edited this page Mar 1, 2024 · 4 revisions

NodeJS

npm install tidy-url

Require

import { TidyURL } from 'tidy-url';
// or
const { TidyURL } = require('tidy-url');

Usage

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

Validating

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!)

AMP & Redirects

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.

Note

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)

Supported Sites

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

Response

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
    }
}
Clone this wiki locally