improved generation of hashed ID for inline footnotes by adding a nonce

This commit is contained in:
Amy G. Bowersox 2024-08-02 22:48:27 -06:00
parent 81201c5a21
commit abbebab00f

View File

@ -331,6 +331,10 @@ class ObsidianStyleFootnotes(FootnoteExtension):
"""A block processor extended to handle Obsidian inline footnotes.""" """A block processor extended to handle Obsidian inline footnotes."""
RE_INLINE = re.compile(r'\^\[(.+)]') 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: def run(self, parent: etree.Element, blocks: list[str]) -> bool:
""" Find, set, and remove footnote definitions. """ """ Find, set, and remove footnote definitions. """
block = blocks.pop(0) block = blocks.pop(0)
@ -373,7 +377,8 @@ class ObsidianStyleFootnotes(FootnoteExtension):
if m_inline: if m_inline:
footnote = m_inline.group(1).strip() 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) self.footnotes.setFootnote(id, footnote)
blocks.insert(0, block[:m_inline.start(0)].rstrip() blocks.insert(0, block[:m_inline.start(0)].rstrip()
+ INLINE_FOOTNOTE_REF_PATTERN.format(id) + block[m_inline.end(0):]) + INLINE_FOOTNOTE_REF_PATTERN.format(id) + block[m_inline.end(0):])