Skip to content

added 3 more options #133

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,12 @@ WAGTAILMARKDOWN = {
"extensions": [], # optional. a list of python-markdown supported extensions
"extension_configs": {}, # optional. a dictionary with the extension name as key, and its configuration as value
"extensions_settings_mode": "extend", # optional. Possible values: "extend" or "override". Defaults to "extend".
"unsafe_html": True, # optional. skip bleach.
"image_opts": { # optional. specify default image rendition options.
"spec": "width-700",
"classname": "right",
},
"wrap_images_in_anchors": False, # optional. do not wrap images in anchors tags.
}
```

Expand Down
8 changes: 8 additions & 0 deletions src/wagtailmarkdown/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -96,5 +96,13 @@
"styles": DEFAULT_ALLOWED_STYLES,
}

DEFAULT_IMAGE_OPTS = {
"spec": "width-500",
"classname": "left",
}

UNSAFE_HTML = False

WRAP_IMAGES_IN_ANCHORS = True

SETTINGS_MODE_OVERRIDE = "override"
27 changes: 20 additions & 7 deletions src/wagtailmarkdown/mdx/linkers/image.py
Original file line number Diff line number Diff line change
@@ -1,21 +1,27 @@
import contextlib
import xml.etree.ElementTree as etree

from django.conf import settings

from django.core.exceptions import MultipleObjectsReturned, ObjectDoesNotExist
from wagtail.images import get_image_model

from wagtailmarkdown.constants import (
DEFAULT_IMAGE_OPTS,
SETTINGS_MODE_OVERRIDE,
WRAP_IMAGES_IN_ANCHORS,
)

OPTS = getattr(settings, "WAGTAILMARKDOWN", {}).get("image_opts", DEFAULT_IMAGE_OPTS)

# TODO: Default spec and class should be configurable, because they're
# dependent on how the project is set up. Hard-coding of 'left',
# 'right' and 'full-width' should be removed.
wrap_images_in_anchors = getattr(settings, "WAGTAILMARKDOWN", {}).get(
"wrap_images_in_anchors", WRAP_IMAGES_IN_ANCHORS
)


class Linker:
def run(self, fname, options):
opts = {
"spec": "width-500",
"classname": "left",
}
opts = OPTS

for opt in options:
bits = opt.split("=", 1)
Expand Down Expand Up @@ -45,6 +51,13 @@ def run(self, fname, options):
image_url = image.file.url
rendition = image.get_rendition(opts["spec"])

if not wrap_images_in_anchors:
img = etree.Element("img")
img.set("src", rendition.url)
img.set("class", opts["classname"])
img.set("width", str(rendition.width))
img.set("height", str(rendition.height))
return img
a = etree.Element("a")
a.set("data-toggle", "lightbox")
a.set("data-type", "image")
Expand Down
11 changes: 10 additions & 1 deletion src/wagtailmarkdown/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,25 @@
from django.utils.encoding import smart_str
from django.utils.safestring import mark_safe

from wagtailmarkdown.constants import DEFAULT_BLEACH_KWARGS, SETTINGS_MODE_OVERRIDE
from wagtailmarkdown.constants import (
DEFAULT_BLEACH_KWARGS,
SETTINGS_MODE_OVERRIDE,
UNSAFE_HTML,
)
from wagtailmarkdown.mdx.inlinepatterns import ImageExtension, LinkExtension
from wagtailmarkdown.mdx.linker import LinkerExtension


unsafe_html = getattr(settings, "WAGTAILMARKDOWN", {}).get("unsafe_html", UNSAFE_HTML)


def render_markdown(text, context=None):
"""
Turn markdown into HTML.
"""
markdown_html = _transform_markdown_into_html(text)
if unsafe_html:
return mark_safe(markdown_html)
sanitised_markdown_html = _sanitise_markdown_html(markdown_html)
# note: we use mark_safe here because bleach is already sanitising the HTML
return mark_safe(sanitised_markdown_html) # noqa: S308
Expand Down