Skip to content

Commit f221ec5

Browse files
authored
Improve Markdown support for Confluence (#57)
1 parent 2f6ad50 commit f221ec5

File tree

2 files changed

+91
-18
lines changed

2 files changed

+91
-18
lines changed

src/Markdown/TypeBuilder.php

+87-14
Original file line numberDiff line numberDiff line change
@@ -30,11 +30,49 @@ class TypeBuilder
3030

3131
public $file = '';
3232

33+
public $confluence = false;
34+
3335
public function __construct()
3436
{
3537
$this->processed = new \SplObjectStorage();
3638
}
3739

40+
public function anchorLink($destinationHeader, $anchor = null)
41+
{
42+
if ($this->confluence) {
43+
$l = str_replace('`', '', $destinationHeader);
44+
$l = str_replace(' ', '-', $l);
45+
if (!is_string($l)) {
46+
return '#';
47+
}
48+
49+
$l = urlencode($l);
50+
51+
return '#' . $l;
52+
}
53+
54+
if (!empty($anchor)) {
55+
$l = strtolower($anchor);
56+
} else {
57+
$l = strtolower($destinationHeader);
58+
}
59+
60+
return '#' . $l;
61+
}
62+
63+
public function header($text, $anchor = null)
64+
{
65+
if ($this->confluence) {
66+
return $text;
67+
}
68+
69+
if (!empty($anchor)) {
70+
$l = strtolower($anchor);
71+
} else {
72+
$l = strtolower($text);
73+
}
74+
return '<a id="' . $l . '">' . '</a> ' . $text;
75+
}
3876

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

63101
if (!empty($schema->enum)) {
64102
$res = '';
65-
foreach ($schema->enum as $value) {
66-
$res .= '<br>`' . var_export($value, true) . '`, ';
103+
if ($this->confluence) {
104+
foreach ($schema->enum as $value) {
105+
$res .= '`' . var_export($value, true) . '`, ';
106+
}
107+
return substr($res, 0, -2);
108+
} else {
109+
foreach ($schema->enum as $value) {
110+
$res .= '<br>`' . var_export($value, true) . '`, ';
111+
}
112+
return substr($res, 4, -2);
67113
}
68-
return substr($res, 4, -2);
69114
}
70115

71116
if (!empty($schema->getFromRefs())) {
@@ -246,6 +291,10 @@ public function getTypeString($schema, $path = '')
246291
$res = '`*`';
247292
}
248293

294+
if (empty($res)) {
295+
$res = '';
296+
}
297+
249298
$res = str_replace('``', '', $res);
250299

251300
return $res;
@@ -273,7 +322,11 @@ private function typeName(Schema $schema, $path, $raw = false)
273322
return $name;
274323
}
275324

276-
return '[`' . $name . '`](#' . strtolower($name) . ')';
325+
if ($this->confluence) {
326+
return '[' . $name . '](' . $this->anchorLink($name) . ')';
327+
}
328+
329+
return '[`' . $name . '`](' . $this->anchorLink($name) . ')';
277330
}
278331

279332
private static function constraints()
@@ -350,12 +403,10 @@ public function renderTypeDef(Schema $schema, $typeName, $path)
350403
}
351404
}
352405

353-
$tnl = strtolower($typeName);
354-
355406
$res = <<<MD
356407
357408
358-
### <a id="$tnl"></a>$typeName
409+
### {$this->header($typeName)}
359410
$head
360411
361412
MD;
@@ -376,14 +427,19 @@ public function renderTypeDef(Schema $schema, $typeName, $path)
376427
];
377428
}
378429
}
379-
$res .= TableRenderer::create(new \ArrayIterator($rows))
430+
$tr = TableRenderer::create(new \ArrayIterator($rows))
380431
->stripEmptyColumns()
381432
->setColDelimiter('|')
382433
->setHeadRowDelimiter('-')
383434
->setOutlineVertical(true)
384-
->multilineCellDelimiter('<br>')
385435
->setShowHeader();
386436

437+
if (!$this->confluence) {
438+
$tr->multilineCellDelimiter('<br>');
439+
}
440+
441+
$res .= $tr;
442+
387443
$res .= "\n\n";
388444

389445
$rows = [];
@@ -412,14 +468,18 @@ public function renderTypeDef(Schema $schema, $typeName, $path)
412468
}
413469
}
414470

415-
$res .= TableRenderer::create(new \ArrayIterator($rows))
471+
$tr = TableRenderer::create(new \ArrayIterator($rows))
416472
->stripEmptyColumns()
417473
->setColDelimiter('|')
418474
->setHeadRowDelimiter('-')
419475
->setOutlineVertical(true)
420-
->multilineCellDelimiter('<br>')
421476
->setShowHeader();
422477

478+
if (!$this->confluence) {
479+
$tr->multilineCellDelimiter('<br>');
480+
}
481+
482+
$res .= $tr;
423483
}
424484

425485
$res .= <<<MD
@@ -470,15 +530,28 @@ public function tableOfContents()
470530
return $res;
471531
}
472532

533+
private function trim($s)
534+
{
535+
if (empty($s)) {
536+
return '';
537+
}
538+
539+
return trim($s);
540+
}
541+
473542
private function description(Schema $schema)
474543
{
475-
$res = str_replace("\n", " ", trim($schema->title));
476-
if (trim($schema->description)) {
544+
$res = str_replace("\n", " ", $this->trim($schema->title));
545+
if (!is_string($res)) {
546+
return '';
547+
}
548+
549+
if ($this->trim($schema->description)) {
477550
if ($res) {
478551
$res .= ". ";
479552
}
480553

481-
$res .= str_replace("\n", " ", trim($schema->description));
554+
$res .= str_replace("\n", " ", $this->trim($schema->description));
482555
}
483556
if ($res) {
484557
return rtrim($res, '.') . '.';

tests/src/PHPUnit/Markdown/MarkdownTest.php

+4-4
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ public function testJsonSchema()
4444
$this->assertSame(<<<'MD'
4545
4646
47-
### <a id="person"></a>Person
47+
### <a id="person"></a> Person
4848
4949
5050
@@ -57,7 +57,7 @@ public function testJsonSchema()
5757
|`children`|`Array<`[`Person`](#person)`>`| |
5858
5959
60-
### <a id="unit"></a>Unit
60+
### <a id="unit"></a> Unit
6161
This is a unit of something.
6262
6363
@@ -123,7 +123,7 @@ function testIssue39() {
123123
$this->assertSame(<<<'MD'
124124
125125
126-
### <a id="anyof0"></a>AnyOf0
126+
### <a id="anyof0"></a> AnyOf0
127127
128128
129129
@@ -132,7 +132,7 @@ function testIssue39() {
132132
|`test1` |`String`|
133133
134134
135-
### <a id="anyof1"></a>AnyOf1
135+
### <a id="anyof1"></a> AnyOf1
136136
137137
138138

0 commit comments

Comments
 (0)