Skip to content

Add table of contents to md gen #49

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Sep 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [0.2.36] - 2022-09-16

### Added
- Table of contents in Markdown generator.

## [0.2.35] - 2022-01-02

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

[0.2.36]: https://github.com/swaggest/php-code-builder/compare/v0.2.35...v0.2.36
[0.2.35]: https://github.com/swaggest/php-code-builder/compare/v0.2.34...v0.2.35
[0.2.34]: https://github.com/swaggest/php-code-builder/compare/v0.2.33...v0.2.34
[0.2.33]: https://github.com/swaggest/php-code-builder/compare/v0.2.32...v0.2.33
Expand Down
31 changes: 30 additions & 1 deletion src/Markdown/TypeBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ class TypeBuilder

public $addNamePrefix = '';

/**
* Map of type name to type doc.
* @var array<string,string>
*/
public $types = [];

public $file = '';

public function __construct()
Expand Down Expand Up @@ -223,7 +229,7 @@ public function getTypeString($schema, $path = '')
}

if ($schema->format !== null) {
$or []= 'Format: `' . $schema->format . '`';
$or [] = 'Format: `' . $schema->format . '`';
}

$res = '';
Expand Down Expand Up @@ -421,11 +427,34 @@ private function makeTypeDef(Schema $schema, $path)

MD;

$this->types[$typeName] = $res;
$this->file .= $res;

return $typeName;
}

public function sortTypes()
{
ksort($this->types);
}

public function tableOfContents()
{
if (count($this->types) === 0) {
return '';
}

$res = '# Types' . "\n\n";

foreach ($this->types as $name => $doc) {
$res .= ' * ' . $name . "\n";
}

$res .= "\n\n";

return $res;
}

private function description(Schema $schema)
{
$res = str_replace("\n", " ", $schema->title . $schema->description);
Expand Down
12 changes: 12 additions & 0 deletions tests/src/PHPUnit/Markdown/MarkdownTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -69,5 +69,17 @@ public function testJsonSchema()

MD
, $tb->file);

$tb->sortTypes();
$this->assertSame(<<<'MD'
# Types

* [`Person`](#person)
* [`Unit`](#unit)



MD
, $tb->tableOfContents());
}
}