From d7f90156feb198ab691a075bd7ec6211a28bf8ec Mon Sep 17 00:00:00 2001
From: John-Scott Atlakson <24574+jsma@users.noreply.github.com>
Date: Sat, 29 Jul 2023 11:17:32 +0900
Subject: [PATCH] Initial attempt at allowing multiple references to the same
footnote
Previously this generated the same ``id`` attribute for every link in rich text for the same footnote, which breaks the "Back to content" link.
This generates a unique ``id`` for each footnote reference.
This commit does not expose these links on the front-end (yet), but at least the "back" link will point to a single id.
---
wagtail_footnotes/blocks.py | 13 ++++++++++---
1 file changed, 10 insertions(+), 3 deletions(-)
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):