Skip to content

Commit

Permalink
Cleanup and refactor code
Browse files Browse the repository at this point in the history
  • Loading branch information
caendesilva committed Dec 5, 2024
1 parent 155c456 commit b959d2d
Showing 1 changed file with 13 additions and 9 deletions.
22 changes: 13 additions & 9 deletions packages/framework/src/Markdown/Processing/HeadingRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
}

0 comments on commit b959d2d

Please sign in to comment.