diff --git a/wagtail_footnotes/blocks.py b/wagtail_footnotes/blocks.py
index 71b17da..cf3a394 100644
--- a/wagtail_footnotes/blocks.py
+++ b/wagtail_footnotes/blocks.py
@@ -1,5 +1,7 @@
import re
+from collections import defaultdict
+
from django.core.exceptions import ValidationError
from django.utils.safestring import mark_safe
from wagtail.blocks import RichTextBlock
@@ -24,6 +26,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:
@@ -37,17 +40,22 @@ 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''
+ link_id = f"footnote-source-{index}-{len(page.footnotes_references[footnote_uuid])}"
+ page.footnotes_references[footnote_uuid].append(link_id)
+ return f'[{index}]'
# note: we return safe html
return mark_safe(FIND_FOOTNOTE_TAG.sub(replace_tag, html)) # noqa: S308
@@ -61,7 +69,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):