From 39646b890438a2d50ee1bacfc47dc4ed5199b45a Mon Sep 17 00:00:00 2001 From: DistractedMOSFET <59909722+distractedmosfet@users.noreply.github.com> Date: Thu, 2 Jun 2022 14:42:13 +1200 Subject: [PATCH 1/2] FIX: Anchor ids in separate renders should not affect each other. The anchors plugin remembered used anchor ids per MarkdownIt object for entire lifetime of the object. This means if you use the same MarkdownIt object to render multiple documents then the generated anchor ids will be affected by the order of rendered documents. This commit makes it clear the known ids after anchors are generated, so separate renders should not affect each other. --- mdit_py_plugins/anchors/index.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/mdit_py_plugins/anchors/index.py b/mdit_py_plugins/anchors/index.py index dcbf789..fb5eea3 100644 --- a/mdit_py_plugins/anchors/index.py +++ b/mdit_py_plugins/anchors/index.py @@ -112,7 +112,7 @@ def _anchor_func(state: StateCore): ([Token("text", "", 0, content=" ")] if permalinkSpace else []) + link_tokens ) - + slugs.clear() return _anchor_func From 74c43f8bbad7220c533928ef70b3864d4b0866e3 Mon Sep 17 00:00:00 2001 From: DistractedMOSFET <59909722+distractedmosfet@users.noreply.github.com> Date: Thu, 2 Jun 2022 15:00:59 +1200 Subject: [PATCH 2/2] =?UTF-8?q?=20=E2=99=BB=EF=B8=8F=20REFACTOR:=20Create?= =?UTF-8?q?=20new=20set=20rather=20than=20clear=20old?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Actually, if I'm just gonna clear the set each time clearly I should just make a new one. No reason to keep it lingering around in memory. --- mdit_py_plugins/anchors/index.py | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/mdit_py_plugins/anchors/index.py b/mdit_py_plugins/anchors/index.py index fb5eea3..abbd48a 100644 --- a/mdit_py_plugins/anchors/index.py +++ b/mdit_py_plugins/anchors/index.py @@ -65,9 +65,8 @@ def _make_anchors_func( permalinkBefore: bool, permalinkSpace: bool, ): - slugs: Set[str] = set() - def _anchor_func(state: StateCore): + slugs: Set[str] = set() for (idx, token) in enumerate(state.tokens): if token.type != "heading_open": continue @@ -112,7 +111,7 @@ def _anchor_func(state: StateCore): ([Token("text", "", 0, content=" ")] if permalinkSpace else []) + link_tokens ) - slugs.clear() + return _anchor_func