neutronX / django-markdownx

Comprehensive Markdown plugin built for Django
https://neutronx.github.io/django-markdownx/
Other
854 stars 153 forks source link

Using absolute paths for images? #222

Closed digitalWestie closed 2 years ago

digitalWestie commented 2 years ago

In the docs I can see I can alter the path to the image using MARKDOWNX_URLS_PATH https://neutronx.github.io/django-markdownx/customization/#markdownx_urls_path

However, the docs say this is a "Relative URL to which the Markdown text is sent to be encoded as HTML", can I not use an absolute path here?

xenatisch commented 2 years ago

I haven't tried this myself, but it should be possible via the MARKDOWNX_MEDIA_PATH setting variable (see the documentation).

If that doesn't work, we can always look into adding a new feature.

digitalWestie commented 2 years ago

Ah, I think I may have confused my own question as I misunderstood the purpose of those settings.

To explain a bit further, what I'm looking to do is to drag in the image, and instead of generating markdown such as:

[](/media/markdownx/myimage.png)

Instead generate an absolute URL that includes the domain:

[](https://mydomain.com/media/markdownx/myimage.png)

I'll take a look and see if I can find a way to prepend the domain etc to the relative '/media/markdownx/' path.

digitalWestie commented 2 years ago

I was able to achieve this in the end by overriding ImageUploadView.

In views:

class AbsoluteImageUploadView(ImageUploadView):
  def form_valid(self, form):
    response = super(ImageUploadView, self).form_valid(form)
    if self.request.is_ajax():
      image_path = form.save(commit=True)
      absolute_image_path = self.request.build_absolute_uri(image_path)
      image_code = '![]({})'.format(absolute_image_path)
      return JsonResponse({'image_code': image_code})
    return response

In urls:

re_path(r'^markdownx/absolute-upload/', AbsoluteImageUploadView.as_view()),

In settings:

MARKDOWNX_UPLOAD_URLS_PATH = '/markdownx/absolute-upload/'