neutronX / django-markdownx

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

NEW FEATURE: custom model field image prefix, custom filename and less duplication #192

Open sstacha opened 4 years ago

sstacha commented 4 years ago

I needed some functionality that I think would be valuable to the base project if you are interested in any of it. The 3 feature changes are pretty simple. 1) If a developer creates a markdownx field with a new attribute field_image_prefix="somepathstring" that prefix is appended to the path adding the ability for field level image separation. 2) If a content creator renames the image before dropping they can easily control the filename that is stored This now gives developers and content creators 3 levels of customization without any overloading or extending this projects code. The existing project level setting prefix MARKDOWNX_MEDIA_PATH + model/field level path prefix + imagename 3) Last, to prevent the number of duplicates and for easy url generation for existing images content creators can drop an existing image from the media folder location into a markdown field to create a link to an image that already exists.

I attempted to add the functionality with the least amount of changes or impact to existing or new installations.

I did not intend the comments to be kept but I thought it would be valuable during review to see why I think I needed each change.

An example of what a model field might look like:

content = MarkdownxField(field_image_prefix='markdownpost/content', help_text=mark_safe(
        'Markdown Reference: <a href="https://commonmark.org/help/">https://commonmark.org/help/</a>'))

An example of what a dropped image me.jpg final path might look like with the following settings:

MARKDOWNX_MEDIA_PATH = datetime.datetime.now().strftime('markdownx/%Y')
MARKDOWNX_USE_ORIGINAL_IMAGE_NAME=True

/media/markdownx/2020/markdownpost/content/me.jpg

This is my first attempt at a pull request so if I did something wrong please let me know