Skip to content

Commit

Permalink
Duplicate headlines fix (fixes #7)
Browse files Browse the repository at this point in the history
  • Loading branch information
caseyamcl committed Jun 26, 2019
1 parent 94c7fb5 commit 6def028
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 8 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
@@ -1,6 +1,11 @@
# ChangeLog - PHP TableOfContents (TOC)
All notable changes to this project are documented in this file.

## [2.0.1] - 2019-06-26
### Fixed
- Duplicate headings are identified correctly ([#7](https://github.com/caseyamcl/toc/issues/7))
- `composer:check-style` and `composer:fix-style` commands now work (fixed missing codesniffer library)

## [2.0] - 2018-11-09
### Changed
- BREAKING: `TocGenerator::getMenu()` returns an empty menu item instead of an empty array when no
Expand Down
9 changes: 7 additions & 2 deletions src/TocGenerator.php
Expand Up @@ -20,6 +20,7 @@
use Knp\Menu\ItemInterface;
use Knp\Menu\Matcher\Matcher;
use Knp\Menu\MenuFactory;
use Knp\Menu\MenuItem;
use Knp\Menu\Renderer\ListRenderer;
use Knp\Menu\Renderer\RendererInterface;
use Masterminds\HTML5;
Expand Down Expand Up @@ -94,6 +95,7 @@ public function getMenu($markup, $topLevel = 1, $depth = 6)
$level = array_search(strtolower($tagName), $tagsToMatch) + 1;

// Determine parent item which to add child
/** @var MenuItem $parent */
if ($level == 1) {
$parent = $menu;
} elseif ($level == $lastElem->getLevel()) {
Expand All @@ -111,8 +113,11 @@ public function getMenu($markup, $topLevel = 1, $depth = 6)
}

$lastElem = $parent->addChild(
$node->getAttribute('title') ?: $node->textContent,
['uri' => '#' . $node->getAttribute('id')]
$node->getAttribute('id'),
[
'label' => $node->getAttribute('title') ?: $node->textContent,
'uri' => '#' . $node->getAttribute('id')
]
);
}

Expand Down
4 changes: 2 additions & 2 deletions src/UniqueSluggifier.php
Expand Up @@ -28,7 +28,7 @@
class UniqueSluggifier
{
/**
* @var \Cocur\Slugify\Slugify
* @var Slugify
*/
private $slugify;

Expand All @@ -40,7 +40,7 @@ class UniqueSluggifier
/**
* Constructor
*
* @param \Cocur\Slugify\Slugify $slugify
* @param Slugify $slugify
*/
public function __construct(Slugify $slugify = null)
{
Expand Down
6 changes: 3 additions & 3 deletions tests/TocGeneratorTest.php
Expand Up @@ -37,8 +37,8 @@ public function testDuplicateHeadingsAreEnumerated()
{
$obj = new TocGenerator();

$html = "<h1 id='x'>A-Header</h1><h1 id='x'>A-Header</h1>";
var_dump($obj->getHtmlMenu($html));
$html = "<h1 id='x'>A-Header</h1><h1 id='y'>A-Header</h1>";
$this->assertSame(2, count($obj->getMenu($html)));
}

public function testGetMenuTraversesLevelsCorrectly()
Expand All @@ -62,7 +62,7 @@ public function testGetMenuTraversesLevelsCorrectly()
$this->assertEquals($fixture, $actual);
}

public function testGetMenuMatchesOnlyElementsWithIDs()
public function testGetMenuGeneratesIdsForElementsWithoutIDs()
{
$html = "
<h1 id='a'>A-Header</h1><p>Foobar</p>
Expand Down
1 change: 0 additions & 1 deletion tests/TocTwigExtensionTest.php
Expand Up @@ -33,7 +33,6 @@ class TocTwigExtensionTest extends PHPUnit_Framework_TestCase
public function testInstantiateSucceeds()
{
$obj = new TocTwigExtension();

$this->assertInstanceOf('\TOC\TocTwigExtension', $obj);
}

Expand Down

0 comments on commit 6def028

Please sign in to comment.