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

Template Filter for regular RichTextField? #20

Open
andre-fuchs opened this issue Sep 8, 2021 · 4 comments
Open

Template Filter for regular RichTextField? #20

andre-fuchs opened this issue Sep 8, 2021 · 4 comments

Comments

@andre-fuchs
Copy link

I love this package!
Is there a way to add these footnotes to a regular RichTextField (not RichTextBlock) 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 with include_block. I reckon that a custom template filter could work. This could just append the list of footnotes to the richtext.

@benjaoming
Copy link
Contributor

Pretty sure that I'm also running into this issue.

@benjaoming
Copy link
Contributor

No, after a while, I figured out that it's the raw contents of my RichText field that didn't contain correct <footnote id="[uuid]">[uuid]</footnote>.

@benjaoming
Copy link
Contributor

You can use this filter, it aligns with what the rendering of the StreamField block does, so it's compatible with the existing footnotes.html include.

@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

@zerolab
Copy link
Member

zerolab commented Aug 22, 2023

@benjaoming I somehow missed this, but as things stands using RichTextField with the footnotes feature only outputs UUIDs, so this is definitely something to fix.

Ideally we'd just provide a RichTextFieldWithFootnotes that adds footnotes to the features list, if not there already. And then does the necessary conversions without developers needing to use a separate template tag

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

No branches or pull requests

3 participants