Strip URL Parameters


#1

Hello all,

I’m new to Drafts and trying to use the Share Extension to create blog post drafts from Safari.

In my draft, the [[URL]] parameter sometimes appears as a URL with parameters:

e.g. https://moz.com/blog/smart-seo-goals?utm_source=feedburner&utm_medium=feed&utm_campaign=Feed%3A+MozBlog+(Moz+Blog)

Could someone point me the way towards searching and replacing in links to remove the parameters?

i.e. remove this part from the link above:
“?utm_source=feedburner&utm_medium=feed&utm_campaign=Feed%3A+MozBlog+%28Moz+Blog%29”

Thanks!

Ivan


#2

One approach might be a generic takeWhile function:

(() => {
    'use strict';

    const main = () => {
        const strFull = 'https://moz.com/blog/smart-seo-goals?utm_source=feedburner&utm_medium=feed&utm_campaign=Feed%3A+MozBlog+(Moz+Blog)';

        const strBase = takeWhile(
            c => c !== '?',
            strFull
        );

        return strBase;
        // --> https://moz.com/blog/smart-seo-goals
    };


    // GENERIC FUNCTION --------------------------------------

    // takeWhile :: (a -> Bool) -> [a] -> [a]
    const takeWhile = (p, xs) => {
        let i = 0,
            lng = xs.length;
        while ((i < lng) && p(xs[i])) {
            i = i + 1;
        }
        return xs.slice(0, i);
    };

    // MAIN ---------------------------------------------------
    return main();
})();

#3

Or perhaps, tidying a little:

// takeWhile :: (a -> Bool) -> [a] -> [a]
const takeWhile = (p, xs) => {
    let i = 0;
    const lng = xs.length;
    while ((i < lng) && p(xs[i])) (i = i + 1);
    return xs.slice(0, i);
};

#4

Something simple like this should work:

function removeQuery(uri) {
    return uri.split('?')[0];
}

#5

Thank you dchar and char8!

Currently, the URL is inserted along with other text in drafts.content

I’ll try to figure out how to extract the URLs from the content as from my understanding, I need to pass just the URL string for your solutions to work?

Thanks again.


#6

I’ve ended up with a complicated regex I copied from the web, but it seems to be doing the job for now.

Thanks again everyone for the suggestions.

// Regex to strip URLs of any query parameters
const urlRegex = /((https?\:\/\/)(([^:\/?#])(?:\:([0-9]+))?)([\/]{0,1}[^?#])(\?[^#]|)(#.|))/i;

// define replacement expression...
const replaceWith = "<a href=\"$2$4$6\">$2$4$6</a>";

// do the replacement...
draft.content = draft.content.replace(urlRegex, replaceWith);
draft.update();

#7

Looks like you’ve found a good solution – a footnote on posting code: you can make it more legible by fencing above and below with three backticks ``` to enable syntax highlighting.

```

// Regex to strip URLs of any query parameters
const urlRegex = /((https?://)(([^:/?#])(?::([0-9]+))?)([/]{0,1}[^?#])(?[^#]|)(#.|))/i;

// define replacement expression…
const replaceWith = “<a href=”$2$4$6">$2$4$6";

// do the replacement…
draft.content = draft.content.replace(urlRegex, replaceWith);
draft.update();

```


#8

Thanks for the tip. Updated my code.