Skip to content

Commit 0c62abb

Browse files
committed
-
1 parent 9c78873 commit 0c62abb

File tree

3 files changed

+73
-7
lines changed

3 files changed

+73
-7
lines changed

src/Generator/JsonGenerator.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -111,9 +111,9 @@ private function generateToc(MetaEntry $metaEntry, Crawler $crawler): array
111111

112112
// this tocTree stores items sequentially (h2, h2, h3, h3, h2, h3, etc.)
113113
$flatTocTree[] = [
114-
'level' => 'h2' === $heading->tagName ? 2 : 3,
114+
'level' => 'h2' === $heading->tagName ? 1 : 2,
115115
'url' => sprintf('%s#%s', $metaEntry->getUrl(), $headerId),
116-
'page' => u($metaEntry->getUrl())->beforeLast('.html'),
116+
'page' => u($metaEntry->getUrl())->beforeLast('.html')->toString(),
117117
'fragment' => $headerId,
118118
'title' => $heading->textContent,
119119
'children' => [],
@@ -123,7 +123,7 @@ private function generateToc(MetaEntry $metaEntry, Crawler $crawler): array
123123
// this tocTree stores items nested by level (h2, h2[h3, h3], h2[h3], etc.)
124124
$nestedTocTree = [];
125125
foreach ($flatTocTree as $tocItem) {
126-
if (2 === $tocItem['level']) {
126+
if (1 === $tocItem['level']) {
127127
$nestedTocTree[] = $tocItem;
128128
} else {
129129
$nestedTocTree[\count($nestedTocTree) - 1]['children'][] = $tocItem;

tests/JsonIntegrationTest.php

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,19 @@
1010
namespace SymfonyDocsBuilder\Tests;
1111

1212
use SymfonyDocsBuilder\DocBuilder;
13+
use SymfonyDocsBuilder\Renderers\TitleNodeRenderer;
1314

1415
class JsonIntegrationTest extends AbstractIntegrationTest
1516
{
17+
public function setUp(): void
18+
{
19+
$reflection = new \ReflectionClass(TitleNodeRenderer::class);
20+
$property = $reflection->getProperty('idUsagesCountByFilename');
21+
$property->setAccessible(true);
22+
23+
$property->setValue([]);
24+
}
25+
1626
/**
1727
* @dataProvider getJsonTests
1828
*/
@@ -26,7 +36,7 @@ public function testJsonGeneration(string $filename, array $expectedData)
2636
$actualFileData = $fJsons[$filename];
2737
foreach ($expectedData as $key => $expectedKeyData) {
2838
$this->assertArrayHasKey($key, $actualFileData, sprintf('Missing key "%s" in file "%s"', $key, $filename));
29-
$this->assertSame($expectedData[$key], $actualFileData[$key], sprintf('Invalid data for key "%s" in file "%s"', $key, $filename));
39+
$this->assertSame($expectedKeyData, $actualFileData[$key], sprintf('Invalid data for key "%s" in file "%s"', $key, $filename));
3040
}
3141
}
3242

@@ -76,9 +86,53 @@ public function getJsonTests()
7686
'title' => 'Design',
7787
'toc_options' => [
7888
'maxDepth' => 2,
79-
'numVisibleItems' => 3,
89+
'numVisibleItems' => 5,
8090
'size' => 'md'
8191
],
92+
'toc' => [
93+
[
94+
'level' => 1,
95+
'url' => 'design.html#section-1',
96+
'page' => 'design',
97+
'fragment' => 'section-1',
98+
'title' => 'Section 1',
99+
'children' => [
100+
[
101+
'level' => 2,
102+
'url' => 'design.html#some-subsection',
103+
'page' => 'design',
104+
'fragment' => 'some-subsection',
105+
'title' => 'Some subsection',
106+
'children' => [],
107+
],
108+
[
109+
'level' => 2,
110+
'url' => 'design.html#some-subsection-1',
111+
'page' => 'design',
112+
'fragment' => 'some-subsection-1',
113+
'title' => 'Some subsection',
114+
'children' => [],
115+
],
116+
],
117+
],
118+
[
119+
'level' => 1,
120+
'url' => 'design.html#section-2',
121+
'page' => 'design',
122+
'fragment' => 'section-2',
123+
'title' => 'Section 2',
124+
'children' => [
125+
[
126+
'level' => 2,
127+
'url' => 'design.html#some-subsection-2',
128+
'page' => 'design',
129+
'fragment' => 'some-subsection-2',
130+
'title' => 'Some subsection',
131+
'children' => [],
132+
],
133+
],
134+
],
135+
],
82136
],
83137
];
84138

tests/fixtures/source/json/design.rst

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,18 +11,30 @@ The toctree below should affects the next/prev. The
1111
first entry is effectively ignored, as it was already
1212
included by the toctree in index.rst (which is parsed first).
1313

14-
Subsection 1
15-
~~~~~~~~~~~~
14+
Some subsection
15+
~~~~~~~~~~~~~~~
1616

1717
This is a subsection of the first section. That's all.
1818

19+
Some subsection
20+
~~~~~~~~~~~~~~~
21+
22+
This sub-section uses the same title as before to test that the tool
23+
never generated two or more headings with the same ID.
24+
1925
Section 2
2026
---------
2127

2228
However, crud (which is ALSO included in the toctree in index.rst),
2329
WILL be read here, as the "crud" in index.rst has not been read
2430
yet (design comes first). Also, design/sub-page WILL be considered.
2531

32+
Some subsection
33+
~~~~~~~~~~~~~~~
34+
35+
This sub-section also uses the same title as in the previous section
36+
to test that the tool never generated two or more headings with the same ID.
37+
2638
.. toctree::
2739
:maxdepth: 1
2840

0 commit comments

Comments
 (0)