easy-markdown-editor icon indicating copy to clipboard operation
easy-markdown-editor copied to clipboard

afterImageUploaded should ignore query string distinguishing image files

Open shrry2 opened this issue 4 years ago • 3 comments

Description I use Firebase Cloud Storage as image storage, which makes public image url be https://...fileName.png?alt=media.
When I call onSuccess handler after uploading and getting the public url, it inserts the content as normal link ([title](url)) while expected to insert as image (![title](url)).

This is because easymde calls afterImageUploaded function internally when onSuccess handler is called. It simply uses the last part of the image url (query string ?alt=media included), which means any url which has query string will not be handled as image even if it really has the extension of image.

https://github.com/Ionaru/easy-markdown-editor/blob/072176a86ebbd17fabe47845866610e77e65f779/src/js/easymde.js#L810

var imageName = url.substr(url.lastIndexOf('/') + 1);
var ext = imageName.substring(imageName.lastIndexOf('.') + 1);

// Check if media is an image
if (['png', 'jpg', 'jpeg', 'gif', 'svg'].includes(ext)) {
  _replaceSelection(cm, stat.image, options.insertTexts.uploadedImage, url);
} else {
  var text_link = options.insertTexts.link;
  text_link[0] = '[' + imageName;
  _replaceSelection(cm, stat.link, text_link, url);
}

The part getting file extension from url should be like:

var ext = url.split(/[#?]/)[0].split('.').pop().trim();

I took this example from: https://stackoverflow.com/a/47767860/5359376

Expected behavior afterImageUploaded handler should ignore query string in the uploaded image url. Also, it would be great if these extension or the tag choosing strategy be customizable.

Thank you.

shrry2 avatar Mar 11 '21 09:03 shrry2

Can you test this on the @next version of the editor? https://github.com/Ionaru/easy-markdown-editor/pull/311 was recently merged that resolved a very similar issue.

Ionaru avatar Mar 11 '21 11:03 Ionaru

I'm sorry, but I didn't notice that this was already resolved. I copied some of the modified code and it seems to work fine. Instead of using this package directly, I'm using react-simplemde-editor. Once the next version is published, then I'll make a pull request to update the dependency. Thanks.

shrry2 avatar Mar 11 '21 12:03 shrry2

Realistically the URL shouldn't be used to determine if the uploaded file is an image at all, as it has no relation to the content of the response. To do this properly, the MIME type should be used instead.

girlpunk avatar May 12 '22 16:05 girlpunk