From b959d2d72f0f0512a3feb774e126f3359cf2646a Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Thu, 5 Dec 2024 19:58:08 +0100 Subject: [PATCH] Cleanup and refactor code --- .../Markdown/Processing/HeadingRenderer.php | 22 +++++++++++-------- 1 file changed, 13 insertions(+), 9 deletions(-) diff --git a/packages/framework/src/Markdown/Processing/HeadingRenderer.php b/packages/framework/src/Markdown/Processing/HeadingRenderer.php index 38f379991b1..35a978a7a09 100644 --- a/packages/framework/src/Markdown/Processing/HeadingRenderer.php +++ b/packages/framework/src/Markdown/Processing/HeadingRenderer.php @@ -70,18 +70,22 @@ public function postProcess(string $html): string protected function makeHeadingId(string $contents): string { - $identifier = Str::slug($contents); + $identifier = $this->ensureIdentifierIsUnique(Str::slug($contents)); - // Check for duplicates in the tracker - $id = $identifier; + $this->headingRegistry[] = $identifier; + + return $identifier; + } + + protected function ensureIdentifierIsUnique(string $slug): string + { + $identifier = $slug; $suffix = 2; - while (in_array($id, $this->headingRegistry)) { - $id = $identifier.'-'.$suffix++; - } - // Record the ID in the tracker and return it - $this->headingRegistry[] = $id; + while (in_array($identifier, $this->headingRegistry)) { + $identifier = $slug.'-'.$suffix++; + } - return $id; + return $identifier; } }