Skip to content

Improve Markdown support for Confluence #57

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 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 @@ class TypeBuilder

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 '#';
}

$l = urlencode($l);

return '#' . $l;
}

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

return '#' . $l;
}

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

if (!empty($anchor)) {
$l = strtolower($anchor);
} else {
$l = strtolower($text);
}
return '<a id="' . $l . '">' . '</a> ' . $text;
}

/**
* @param Schema|boolean|null $schema
Expand Down Expand Up @@ -62,10 +100,17 @@ public function getTypeString($schema, $path = '')

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) . '`, ';
}
return substr($res, 0, -2);
} 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 @@ public function getTypeString($schema, $path = '')
$res = '`*`';
}

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

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

return $res;
Expand Down Expand Up @@ -273,7 +322,11 @@ private function typeName(Schema $schema, $path, $raw = false)
return $name;
}

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

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

private static function constraints()
Expand Down Expand Up @@ -350,12 +403,10 @@ public function renderTypeDef(Schema $schema, $typeName, $path)
}
}

$tnl = strtolower($typeName);

$res = <<<MD


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

MD;
Expand All @@ -376,14 +427,19 @@ public function renderTypeDef(Schema $schema, $typeName, $path)
];
}
}
$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 @@ public function renderTypeDef(Schema $schema, $typeName, $path)
}
}

$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 @@ public function tableOfContents()
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 '';
}

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