Update filename within Markdown image link?

This has been frustrating me. I have some content here I want to change some markdown image references to be changed just to the filename.

For example:


![pp.jpeg](https://prod-files-secure.s3.us-west-2.amazonaws.com/1adef11a-f493-4330-805f-7f8441e2dba0/1112a8b1-83e7-4dcf-9c1b-6924f84f1f23/pp.jpeg)

![pp-eyes.jpeg](https://prod-files-secure.s3.us-west-2.amazonaws.com/1adef11a-f493-4330-805f-7f8441e2dba0/2e64af5a-2e52-4b07-8da1-c228613ac82c/pp-eyes.jpeg)

Should be changed to:

pp.jpeg

pp-eyes.jpeg

And all other content unchanged.

This is the macro:

But it is doing nothing to the text, and I just cannot see what I've missed here. I'd really appreciate a fresh pair of eyes. Maybe the regex is OK, but there's something KM specific that I am missing here?

All help greatly appreciated!

This search expression works for me:

\!\[(.+?\.jpeg)\].*\.jpeg\)

with the replacement being $1

image

1 Like

Note that I want it to cater for any file type, and specifically those served from the AWS prod-files-secure domain. In reality, the source file will have a mixture of png, gif, jpg, jpeg.

I appreciate that this can provide a solution for me (which I can adapt a little). But I would like to understand where I have gone wrong with my original attempt if at all possible.

For starters, that looks more like a sed expression than a KM regex -- KM doesn't have the wrapping slashes, doesn't use a trailing g for "global", etc. Check out the Regular Expressions wiki page.

And I think you can go a bit more specific with your pattern by anchoring both ends, since you want to find "stuff before the first . of a line" and "stuff between the last / and last . of a line". So I'd use something like:

Search: ^([^.]+)(.*)/([^.]+)(.*?)$
Replace: $1-eyes$2/$3-eyes$4

Did you try using regex101.com to analyse your search pattern? Well, if you didn't you should give it a go.

This is what it makes of mine: regex101: build, test, and debug regex

Then my expression would become:

\!\[(.+?\.(jpeg|jpg|gif|png))\].*\.(jpeg|jpg|gif|png)\)

The file name in both label and url ?

You can often simplify (or skip) the regular expression by using it in a split rather than a search and replace.

Here, for example, splitting the two halves of the MD link on ](,
and applying the same search and replace,

(perhaps pp.jpegpp-eyes.jpeg) to each half.

In an Execute JavaScript for Automation action, for example, possibly something like:

Updated filename in MD image link.kmmacros (3.6 KB)


Expand disclosure triangle to view JS source
return (() => {
    "use strict";

    const main = () =>
        toMDImageLink(
            both(
                x => x.replace("pp.jpeg", "pp-eyes.jpeg")
            )(
                fromMDImageLink(
                    kmvar.local_MDImageLink
                )
            )
        );


    // ------------ FROM AND TO MD IMAGE LINK ------------

    // fromMDImageLink :: MD String -> (String, URL)
    const fromMDImageLink = imageLink =>
        bimap(
            x => x.slice(2)
        )(
            x => x.slice(0, -1)
        )(
            imageLink.split(/\]\(/u)
            .filter(Boolean)
        );


    // toMDImageLink :: (String, URL) -> MD String
    const toMDImageLink = ([label, url]) =>
        `![${label}](${url})`;


    // --------------------- GENERIC ---------------------

    // bimap :: (a -> b) -> (c -> d) -> (a, c) -> (b, d)
    const bimap = f =>
    // Tuple instance of bimap.
    // A tuple of the application of f and g to the
    // first and second values respectively.
        g => tpl => [
            f(tpl[0]),
            g(tpl[1])
        ];


    // both :: (a -> b) -> (a, a) -> (b, b)
    const both = f =>
    // A tuple obtained by separately
    // applying f to each of the two
    // values in the given tuple.
        ([a, b]) => [
            f(a),
            f(b)
        ];

    return main();
})();