Skip to content

Commit 3c4b08e

Browse files
authored
Add table of contents to md gen (#49)
1 parent 6239d4f commit 3c4b08e

File tree

3 files changed

+48
-1
lines changed

3 files changed

+48
-1
lines changed

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
55
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
66

7+
## [0.2.36] - 2022-09-16
8+
9+
### Added
10+
- Table of contents in Markdown generator.
11+
712
## [0.2.35] - 2022-01-02
813

914
### Fixes
@@ -103,6 +108,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
103108
### Fixed
104109
- Description trimming bug.
105110

111+
[0.2.36]: https://github.com/swaggest/php-code-builder/compare/v0.2.35...v0.2.36
106112
[0.2.35]: https://github.com/swaggest/php-code-builder/compare/v0.2.34...v0.2.35
107113
[0.2.34]: https://github.com/swaggest/php-code-builder/compare/v0.2.33...v0.2.34
108114
[0.2.33]: https://github.com/swaggest/php-code-builder/compare/v0.2.32...v0.2.33

src/Markdown/TypeBuilder.php

Lines changed: 30 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,12 @@ class TypeBuilder
2121

2222
public $addNamePrefix = '';
2323

24+
/**
25+
* Map of type name to type doc.
26+
* @var array<string,string>
27+
*/
28+
public $types = [];
29+
2430
public $file = '';
2531

2632
public function __construct()
@@ -223,7 +229,7 @@ public function getTypeString($schema, $path = '')
223229
}
224230

225231
if ($schema->format !== null) {
226-
$or []= 'Format: `' . $schema->format . '`';
232+
$or [] = 'Format: `' . $schema->format . '`';
227233
}
228234

229235
$res = '';
@@ -421,11 +427,34 @@ private function makeTypeDef(Schema $schema, $path)
421427
422428
MD;
423429

430+
$this->types[$typeName] = $res;
424431
$this->file .= $res;
425432

426433
return $typeName;
427434
}
428435

436+
public function sortTypes()
437+
{
438+
ksort($this->types);
439+
}
440+
441+
public function tableOfContents()
442+
{
443+
if (count($this->types) === 0) {
444+
return '';
445+
}
446+
447+
$res = '# Types' . "\n\n";
448+
449+
foreach ($this->types as $name => $doc) {
450+
$res .= ' * ' . $name . "\n";
451+
}
452+
453+
$res .= "\n\n";
454+
455+
return $res;
456+
}
457+
429458
private function description(Schema $schema)
430459
{
431460
$res = str_replace("\n", " ", $schema->title . $schema->description);

tests/src/PHPUnit/Markdown/MarkdownTest.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,5 +69,17 @@ public function testJsonSchema()
6969

7070
MD
7171
, $tb->file);
72+
73+
$tb->sortTypes();
74+
$this->assertSame(<<<'MD'
75+
# Types
76+
77+
* [`Person`](#person)
78+
* [`Unit`](#unit)
79+
80+
81+
82+
MD
83+
, $tb->tableOfContents());
7284
}
7385
}

0 commit comments

Comments
 (0)