yabwe / medium-editor

Medium.com WYSIWYG editor clone. Uses contenteditable API to implement a rich text solution.
https://yabwe.github.io/medium-editor/
Other
16.05k stars 1.85k forks source link

Link validation #130

Open daviferreira opened 10 years ago

daviferreira commented 10 years ago

As @johnlim5847 said on #124, we need a way to validate links. I'm thinking about a callback executed when the user adds a new link, so validation would be optional and customized. The function would be passed via options.

ScotterC commented 10 years ago

I've found that it's a real pain to truly validate links with a request to the url. However, validating the format of the link is fairly straight forward. This could be configurable with a 'allowPaths' or force full url option.

samthomson commented 10 years ago

An asynchronous get request which allows me to hook an event on its returned status code would be great for me, then I could for example display a custom message for linking to 404s or even 418s, but importantly I could handle displaying it myself.

phiggins42 commented 9 years ago

@samthomson this is possible now ... You would need to replace the default 'anchor' with a "custom" extension building on 'anchor'. If you make the checkLinkFormat function return a promise/deferred or some such, you get one final chance to invalidate the value without exiting out of the button form control.

so, using jquery or such:

AnchorExtension.prototype.checkLinkFormat = function(url){
   return $.ajax("/my/validation/endpoint", { url: url });
};

AnchorExtension.prototype.doFormSave = function(){
   var opts = this.getFormOpts(), t = this;
   $.when(opts.url)
        .done(function(url){
               opts.url = url;
               t.completeFormSave(opts); // creates the link, opts.url deemed valid
        })
        .fail(function(){ ... show validation/etc? whatever you want. backend said url unreachable/etc })
}
nmielnik commented 9 years ago

Another way to support this functionality is to build event canceling into MediumEditor.

The anchor extension could trigger something like a 'createAnchor' event, with some additional information about the link being created. Anyone listening for this event could inspect what link is about to be created, and then call preventDefault() on the event object if the validation fails.

Again, this would require some new functionality built into the custom events, but this seems like useful functionality to add even beyond just link validation.