From abbebab00f1405d9dfe833fa5ee440ea48f9ecf1 Mon Sep 17 00:00:00 2001 From: Amy Gale Ruth Bowersox Date: Fri, 2 Aug 2024 22:48:27 -0600 Subject: [PATCH] improved generation of hashed ID for inline footnotes by adding a nonce --- src/dragonglass/mparse.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/dragonglass/mparse.py b/src/dragonglass/mparse.py index fb5c82b..55e2a0d 100644 --- a/src/dragonglass/mparse.py +++ b/src/dragonglass/mparse.py @@ -331,6 +331,10 @@ class ObsidianStyleFootnotes(FootnoteExtension): """A block processor extended to handle Obsidian inline footnotes.""" RE_INLINE = re.compile(r'\^\[(.+)]') + def __init__(self, footnotes: FootnoteExtension) -> None: + super(ObsidianStyleFootnotes.BlockProcessor, self).__init__(footnotes) + self._inlines_nonce = 0 + def run(self, parent: etree.Element, blocks: list[str]) -> bool: """ Find, set, and remove footnote definitions. """ block = blocks.pop(0) @@ -373,7 +377,8 @@ class ObsidianStyleFootnotes(FootnoteExtension): if m_inline: footnote = m_inline.group(1).strip() - id = hashlib.sha1(bytes(footnote, 'utf-8')).hexdigest() + id = hashlib.sha1(bytes(f"{footnote}{self._inlines_nonce}", 'utf-8')).hexdigest() + self._inlines_nonce += 1 self.footnotes.setFootnote(id, footnote) blocks.insert(0, block[:m_inline.start(0)].rstrip() + INLINE_FOOTNOTE_REF_PATTERN.format(id) + block[m_inline.end(0):])