Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to change image urls in a markdown string

I am working on a nodejs CMS where users write blog posts in Markdown locally, after uploading we process the post in an HTML file. Sometimes users will add a picture like my dog.jpg to the post by copying the image and writing:

![a picture of my dog](my dog.jpg)

I use uslug to convert all filenames so that my dog.jpg becomes my-dog.jpg. However I also need to update the link in the blogpost using uslug, because a) otherwise the link would break because we just changed the filename and b) because most markdown parsers for node will skip the above image syntax because of the whitespace (while the image does get previewed in a lot of local Markdown editors, like Mou).

Does anybody know how I can achieve this using regex?

like image 992
askmike Avatar asked Dec 27 '22 00:12

askmike


1 Answers

You'll need a lot of slashes:

string.replace(/(!\[.*?\]\()(.+?)(\))/g, function(whole, a, b, c) {
    return a + addDashesOrWhatever(b) + c;
});
like image 71
Hubert OG Avatar answered Jan 13 '23 16:01

Hubert OG