New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Make it possible to customize image insertion code. #20
Conversation
It can be useful to insert more that just a markdown image tag, for example to provide additional control on the layout of images. Since there's no convenient way to handle this on the JavaScript side, we generate the code to insert in Python. Then it can be overridden with the existing extensibility mechanism, that is, with a custom view.
The previous commit is backwards-incompatible for users who pointed MARKDOWNX_UPLOAD_URLS_PATH to a custom view. This commit restores backwards-compatibility with the previous API.
My use case is to insert I believe a more common use case for customizing the upload function would be the ability to attach other files (e.g. PDF) and put a download link instead of an image embed. |
Another use case for this would be to use the "image reference" syntax:
It's uncommon but legal! |
As this is the right approach, I would add a general mechanism for adding files/links. In my opinion, current ammend is not "pluggable". You just can't use custom |
Fast idea: template file with attachment's type (content_type – string) like:
It could be simple editable for custom altering. |
If I may take a step back for a minute, I think that django-markdownx already provides several levels of configurability, perhaps too many, and adding more may make the configuration more intimidating than practical :-) There are three ways to change the markdown rendering:
If I were designing a similar library from scratch, I'd just use There's only one way to customize image upload and insertion:
I think that's good for flexibility (although I would have relied on URL reversing as shown above rather than on a setting). If you want to make it easier for users to override smaller bits of functionality, you could restructure
However, designing a convention to select a template depending on the file type sounds (to me) more complicated than just inheriting a class-based view and replacing a method with my own logic. A little bit of code can be more convenient than configuration. So, this is a pretty long comment, and I realize such large changes may be impractical... but I wanted to share my ideas about customisation :-) Really, the only change I need is to make the JavaScript insert what the Django view returns as is. Hence this pull request! To give you an idea my use case, here's how I overrode the views in my application: # We replace markdownx's views entirely because they cannot be customized
# easily. Having all the code here makes it easier to see what's going on.
import os.path
import urllib.parse
from django import forms
from django.core import urlresolvers
from django.http import HttpResponse, JsonResponse
from django.utils.text import slugify
from django.views.generic import View
from ..models import Image
from .utils import markdownify
class MarkdownifyView(View):
def post(self, request, *args, **kwargs):
assert self.request.is_ajax()
return HttpResponse(markdownify(request.POST['content']))
class UploadImageForm(forms.Form):
image = forms.ImageField()
class ImageUploadView(View):
def post(self, request, *args, **kwargs):
assert self.request.is_ajax()
# Super sketchy way to get the post id without touching markdownx.
referer = request.META['HTTP_REFERER']
parsed = urllib.parse.urlparse(referer)
resolved = urlresolvers.resolve(parsed.path)
assert resolved.view_name == 'admin:blog_post_change'
post_id = resolved.args[0]
# Minimal sanity check, simalar to what markdownx does.
form = UploadImageForm(request.POST, request.FILES)
if form.is_valid():
picture = form.cleaned_data['image']
name = os.path.splitext(picture.name)[0]
slug = slugify(name)
Image.objects.create(
post_id=post_id,
picture=picture,
legend=name,
alt_text=name,
slug=slug,
)
image_code = '{{% blog_image "{}" "center" %}}'.format(slug)
return JsonResponse({'image_code': image_code})
else:
return JsonResponse(form.errors, status=400) |
Hello @adi-, let me know if I can do anything to help move this forwards (and stop using my fork). Thanks :-) |
I am little bit busy right now, but I am going to attach your amends in few days, I guess :) |
Great, thanks! No hurry, I just wanted to know if this was still on your radar. |
Done. django-markdownx v1.5 uploaded to pypi |
Heh, I don't even have to beg for release, then... Thanks! |
It can be useful to insert more that just a markdown image tag, for
example to provide additional control on the layout of images. Since
there's no convenient way to handle this on the JavaScript side, we
generate the code to insert in Python. Then it can be overridden with
the existing extensibility mechanism, that is, with a custom view.