diff --git a/wagtail_footnotes/blocks.py b/wagtail_footnotes/blocks.py index a3a6d2b..8dd1aeb 100644 --- a/wagtail_footnotes/blocks.py +++ b/wagtail_footnotes/blocks.py @@ -1,3 +1,4 @@ +from collections import defaultdict import re from django.core.exceptions import ValidationError @@ -33,6 +34,7 @@ def __init__(self, **kwargs): self.features = [] if "footnotes" not in self.features: self.features.append("footnotes") + self.footnotes = {} def replace_footnote_tags(self, value, html, context=None): if context is None: @@ -46,15 +48,20 @@ def replace_footnote_tags(self, value, html, context=None): page = new_context["page"] if not hasattr(page, "footnotes_list"): page.footnotes_list = [] + if not hasattr(page, "footnotes_references"): + page.footnotes_references = defaultdict(list) self.footnotes = {str(footnote.uuid): footnote for footnote in page.footnotes.all()} def replace_tag(match): + footnote_uuid = match.group(1) try: - index = self.process_footnote(match.group(1), page) + index = self.process_footnote(footnote_uuid, page) except (KeyError, ValidationError): return "" else: - return f'[{index}]' + link_id = f'footnote-source-{index}-{len(page.footnotes_references[footnote_uuid])}' + page.footnotes_references[footnote_uuid].append(link_id) + return f'[{index}]' return mark_safe(FIND_FOOTNOTE_TAG.sub(replace_tag, html)) @@ -67,7 +74,6 @@ def render(self, value, context=None): def render_basic(self, value, context=None): html = super().render_basic(value, context) - return self.replace_footnote_tags(value, html, context=context) def process_footnote(self, footnote_id, page):