Skip to content
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

Improve Markdown support for Confluence #57

Merged
merged 3 commits into from
Feb 19, 2024
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
101 changes: 87 additions & 14 deletions src/Markdown/TypeBuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,49 @@

public $file = '';

public $confluence = false;

public function __construct()
{
$this->processed = new \SplObjectStorage();
}

public function anchorLink($destinationHeader, $anchor = null)
{
if ($this->confluence) {
$l = str_replace('`', '', $destinationHeader);
$l = str_replace(' ', '-', $l);
if (!is_string($l)) {
return '#';

Check warning on line 46 in src/Markdown/TypeBuilder.php

View check run for this annotation

Codecov / codecov/patch

src/Markdown/TypeBuilder.php#L43-L46

Added lines #L43 - L46 were not covered by tests
}

$l = urlencode($l);

Check warning on line 49 in src/Markdown/TypeBuilder.php

View check run for this annotation

Codecov / codecov/patch

src/Markdown/TypeBuilder.php#L49

Added line #L49 was not covered by tests

return '#' . $l;

Check warning on line 51 in src/Markdown/TypeBuilder.php

View check run for this annotation

Codecov / codecov/patch

src/Markdown/TypeBuilder.php#L51

Added line #L51 was not covered by tests
}

if (!empty($anchor)) {
$l = strtolower($anchor);

Check warning on line 55 in src/Markdown/TypeBuilder.php

View check run for this annotation

Codecov / codecov/patch

src/Markdown/TypeBuilder.php#L55

Added line #L55 was not covered by tests
} else {
$l = strtolower($destinationHeader);
}

return '#' . $l;
}

public function header($text, $anchor = null)
{
if ($this->confluence) {
return $text;

Check warning on line 66 in src/Markdown/TypeBuilder.php

View check run for this annotation

Codecov / codecov/patch

src/Markdown/TypeBuilder.php#L66

Added line #L66 was not covered by tests
}

if (!empty($anchor)) {
$l = strtolower($anchor);

Check warning on line 70 in src/Markdown/TypeBuilder.php

View check run for this annotation

Codecov / codecov/patch

src/Markdown/TypeBuilder.php#L70

Added line #L70 was not covered by tests
} else {
$l = strtolower($text);
}
return '<a id="' . $l . '">' . '</a> ' . $text;
}

/**
* @param Schema|boolean|null $schema
Expand Down Expand Up @@ -62,10 +100,17 @@

if (!empty($schema->enum)) {
$res = '';
foreach ($schema->enum as $value) {
$res .= '<br>`' . var_export($value, true) . '`, ';
if ($this->confluence) {
foreach ($schema->enum as $value) {
$res .= '`' . var_export($value, true) . '`, ';

Check warning on line 105 in src/Markdown/TypeBuilder.php

View check run for this annotation

Codecov / codecov/patch

src/Markdown/TypeBuilder.php#L104-L105

Added lines #L104 - L105 were not covered by tests
}
return substr($res, 0, -2);

Check warning on line 107 in src/Markdown/TypeBuilder.php

View check run for this annotation

Codecov / codecov/patch

src/Markdown/TypeBuilder.php#L107

Added line #L107 was not covered by tests
} else {
foreach ($schema->enum as $value) {
$res .= '<br>`' . var_export($value, true) . '`, ';
}
return substr($res, 4, -2);
}
return substr($res, 4, -2);
}

if (!empty($schema->getFromRefs())) {
Expand Down Expand Up @@ -246,6 +291,10 @@
$res = '`*`';
}

if (empty($res)) {
$res = '';

Check warning on line 295 in src/Markdown/TypeBuilder.php

View check run for this annotation

Codecov / codecov/patch

src/Markdown/TypeBuilder.php#L295

Added line #L295 was not covered by tests
}

$res = str_replace('``', '', $res);

return $res;
Expand Down Expand Up @@ -273,7 +322,11 @@
return $name;
}

return '[`' . $name . '`](#' . strtolower($name) . ')';
if ($this->confluence) {
return '[' . $name . '](' . $this->anchorLink($name) . ')';

Check warning on line 326 in src/Markdown/TypeBuilder.php

View check run for this annotation

Codecov / codecov/patch

src/Markdown/TypeBuilder.php#L326

Added line #L326 was not covered by tests
}

return '[`' . $name . '`](' . $this->anchorLink($name) . ')';
}

private static function constraints()
Expand Down Expand Up @@ -350,12 +403,10 @@
}
}

$tnl = strtolower($typeName);

$res = <<<MD


### <a id="$tnl"></a>$typeName
### {$this->header($typeName)}
$head

MD;
Expand All @@ -376,14 +427,19 @@
];
}
}
$res .= TableRenderer::create(new \ArrayIterator($rows))
$tr = TableRenderer::create(new \ArrayIterator($rows))
->stripEmptyColumns()
->setColDelimiter('|')
->setHeadRowDelimiter('-')
->setOutlineVertical(true)
->multilineCellDelimiter('<br>')
->setShowHeader();

if (!$this->confluence) {
$tr->multilineCellDelimiter('<br>');
}

$res .= $tr;

$res .= "\n\n";

$rows = [];
Expand Down Expand Up @@ -412,14 +468,18 @@
}
}

$res .= TableRenderer::create(new \ArrayIterator($rows))
$tr = TableRenderer::create(new \ArrayIterator($rows))
->stripEmptyColumns()
->setColDelimiter('|')
->setHeadRowDelimiter('-')
->setOutlineVertical(true)
->multilineCellDelimiter('<br>')
->setShowHeader();

if (!$this->confluence) {
$tr->multilineCellDelimiter('<br>');
}

$res .= $tr;
}

$res .= <<<MD
Expand Down Expand Up @@ -470,15 +530,28 @@
return $res;
}

private function trim($s)
{
if (empty($s)) {
return '';
}

return trim($s);
}

private function description(Schema $schema)
{
$res = str_replace("\n", " ", trim($schema->title));
if (trim($schema->description)) {
$res = str_replace("\n", " ", $this->trim($schema->title));
if (!is_string($res)) {
return '';

Check warning on line 546 in src/Markdown/TypeBuilder.php

View check run for this annotation

Codecov / codecov/patch

src/Markdown/TypeBuilder.php#L546

Added line #L546 was not covered by tests
}

if ($this->trim($schema->description)) {
if ($res) {
$res .= ". ";
}

$res .= str_replace("\n", " ", trim($schema->description));
$res .= str_replace("\n", " ", $this->trim($schema->description));
}
if ($res) {
return rtrim($res, '.') . '.';
Expand Down
8 changes: 4 additions & 4 deletions tests/src/PHPUnit/Markdown/MarkdownTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public function testJsonSchema()
$this->assertSame(<<<'MD'


### <a id="person"></a>Person
### <a id="person"></a> Person



Expand All @@ -57,7 +57,7 @@ public function testJsonSchema()
|`children`|`Array<`[`Person`](#person)`>`| |


### <a id="unit"></a>Unit
### <a id="unit"></a> Unit
This is a unit of something.


Expand Down Expand Up @@ -123,7 +123,7 @@ function testIssue39() {
$this->assertSame(<<<'MD'


### <a id="anyof0"></a>AnyOf0
### <a id="anyof0"></a> AnyOf0



Expand All @@ -132,7 +132,7 @@ function testIssue39() {
|`test1` |`String`|


### <a id="anyof1"></a>AnyOf1
### <a id="anyof1"></a> AnyOf1



Expand Down
Loading