Skip to content
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

Using absolute paths for images? #222

Closed
digitalWestie opened this issue Jan 24, 2022 · 3 comments
Closed

Using absolute paths for images? #222

digitalWestie opened this issue Jan 24, 2022 · 3 comments
Labels

Comments

@digitalWestie
Copy link

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
Copy link
Collaborator

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
Copy link
Author

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
Copy link
Author

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/'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

2 participants