-
Notifications
You must be signed in to change notification settings - Fork 18
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
Template Filter for regular RichTextField? #20
Comments
Pretty sure that I'm also running into this issue. |
No, after a while, I figured out that it's the raw contents of my RichText field that didn't contain correct |
You can use this filter, it aligns with what the rendering of the StreamField block does, so it's compatible with the existing @register.simple_tag(takes_context=True)
def richtext_footnotes(context, html):
"""
example: {% richtext_footnotes page.body|richtext %}
html: already processed richtext field html
Assumes "page" in context.
"""
FIND_FOOTNOTE_TAG = re.compile(r'<footnote id="(.*?)">.*?</footnote>')
if not isinstance(context.get("page"), Page):
return html
page = context["page"]
if not hasattr(page, "footnotes_list"):
page.footnotes_list = []
footnotes = {str(footnote.uuid): footnote for footnote in page.footnotes.all()}
def replace_tag(match):
try:
index = process_footnote(match.group(1), page)
except (KeyError, ValidationError):
return ""
else:
return f'<a href="#footnote-{index}" id="footnote-source-{index}"><sup>[{index}]</sup></a>'
def process_footnote(footnote_id, page):
footnote = footnotes[footnote_id]
if footnote not in page.footnotes_list:
page.footnotes_list.append(footnote)
# Add 1 to the index as footnotes are indexed starting at 1 not 0.
return page.footnotes_list.index(footnote) + 1
return mark_safe(FIND_FOOTNOTE_TAG.sub(replace_tag, html)) LMK if it's something that belongs in the application itself, I can add a PR @zerolab |
@benjaoming I somehow missed this, but as things stands using Ideally we'd just provide a |
I love this package!
Is there a way to add these footnotes to a regular
RichTextField
(notRichTextBlock
) as well?Adding the "footnotes" feature to a
RichTextField
works on the backend side.In frontend template I cannot get it to work – neither by the regular
richttext
filter, nor by passing the value on withinclude_block
. I reckon that a custom template filter could work. This could just append the list of footnotes to the richtext.The text was updated successfully, but these errors were encountered: