Ionaru / easy-markdown-editor

EasyMDE: A simple, beautiful, and embeddable JavaScript Markdown editor. Delightful editing for beginners and experts alike. Features built-in autosaving and spell checking.
https://stackblitz.com/edit/easymde
MIT License
2.39k stars 316 forks source link

Allow absolute URLs for returned image filePaths #240

Open dalboris opened 4 years ago

dalboris commented 4 years ago

Describe the problem

If I run easyMDE on a webpage hosted at https://www.example.com, and my imageUploadEndpoint returns something like:

{
  "data":
  {
    "filePath": "https://static.example.com/images/abc.jpg"
  }
}

Then easyMDE will add the following markdown:

![](https://www.example.com/https://static.example.com/images/abc.jpg)

which is obviously incorrect. This is a bit frustrating, I think it's quite a typical situation for images to be hosted on a different subdomain than the main application, so support for absolute URLs is very important.

Another concrete example I encountered is that while developing and testing my API, I temporarily returned http://placekitten.com/200/300 (some generic image placeholder showing cute kitten) when I hadn't yet implemented the methods to store the image and return the real URL. This didn't correctly work since easyMDE doesn't support absolute URLs.

Describe the solution you'd like

A solution is to simply detect whether the provided filePath is an absolute URL, in which case the current domain shouldn't be prepended.

Ionaru commented 4 years ago

I agree this would be good to have.

fuzzymannerz commented 3 years ago

I know it's been a few months but if anyone is looking to do this, you can find it in this line: https://github.com/Ionaru/easy-markdown-editor/blob/3b6402b3e86edb1c5bdaf66689bf60ee0981a685/src/js/easymde.js#L2341

Just change it to: onSuccess(response.data.filePath);

If you're using the min version you'll want to search for:
(window.location.origin+"/"+e.data.filePath)

and change it to:

(e.data.filePath)

Hope this is helpful to someone searching for an answer to this.