Skip to content

Commit 9947b92

Browse files
authored
feat: add "html" and "markdown" arg option to contentsList() (fix #20) (PR #36)
* feat: add "html" option arg for `contentsList()` (issue #20) * feat: add "markdown" option arg for `contentsList()` * docs: update PHPDocs for `html` and `markdown` option args closes #20
1 parent 3184a83 commit 9947b92

10 files changed

Lines changed: 192 additions & 55 deletions

File tree

Extension.php

Lines changed: 26 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -294,34 +294,46 @@ public function body($text, $omit_toc_tag = false)
294294
}
295295

296296
/**
297-
* Returns the parsed ToC.
298-
* If the arg is "string" then it returns the ToC in HTML string.
297+
* Returns the parsed ToC in various formats.
299298
*
300-
* @param string $type_return Type of the return format. "string", "json", "flatarray" and "nestedarray".
299+
* If the arg is empty or "html" then it returns the ToC in HTML string.
301300
*
302-
* @return false|string|array ToC in HTML/JSON format string or array.
301+
* @param string $type_return Type of the return format. Available types: "markdown", "html", "json", "flatarray" and "nestedarray". Default: "html". Aliases: "string" = "html", "md" = "markdown".
302+
*
303+
* @return false|string|array ToC in HTML/JSON format string or PHP array.
303304
*/
304-
public function contentsList($type_return = 'string')
305+
public function contentsList($type_return = 'html')
305306
{
306-
if ('string' === strtolower($type_return)) {
307+
$type_return = strtolower($type_return);
308+
309+
if ('markdown' === $type_return || 'md' === $type_return) {
307310
$result = '';
308311
if (! empty($this->contentsListString)) {
309-
// Parses the ToC list in markdown to HTML
312+
$result = $this->contentsListString;
313+
}
314+
315+
return rtrim($result);
316+
}
317+
318+
if ('html' === $type_return || 'string' === $type_return) {
319+
$result = '';
320+
if (! empty($this->contentsListString)) {
321+
// Parses the markdown ToC list to HTML
310322
$result = $this->body($this->contentsListString);
311323
}
312324

313325
return $result;
314326
}
315327

316-
if ('json' === strtolower($type_return)) {
328+
if ('json' === $type_return) {
317329
return json_encode($this->contentsListArray);
318330
}
319331

320-
if ('flatarray' === strtolower($type_return)) {
332+
if ('flatarray' === $type_return) {
321333
return $this->contentsListArray;
322334
}
323335

324-
if ('nestedarray' === strtolower($type_return)) {
336+
if ('nestedarray' === $type_return) {
325337
return $this->buildNestedToc($this->contentsListArray);
326338
}
327339

@@ -332,7 +344,7 @@ public function contentsList($type_return = 'string')
332344
. ' in Line:' . __LINE__ . ' (Using default type)'
333345
);
334346

335-
return $this->contentsList('string');
347+
return $this->contentsList('html');
336348
}
337349

338350
/**
@@ -451,6 +463,8 @@ protected function getSalt()
451463
/**
452464
* Gets the markdown tag for ToC.
453465
*
466+
* It returns the current tag to search for the ToC tag, '[toc]' for example, in the markdown text.
467+
*
454468
* @return string
455469
*/
456470
protected function getTagToC()
@@ -530,7 +544,7 @@ protected function setContentsListAsString(array $Content)
530544
$level = $level - $cutIndent;
531545
}
532546

533-
$indent = str_repeat(' ', $level);
547+
$indent = str_repeat(' ', $level-1);
534548

535549
// Stores in markdown list format as below:
536550
// - [Header1](#Header1)

README.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,13 @@ $parser = new \ParsedownToc();
3737
// Get the parsed HTML
3838
$html = $parser->text($inputMarkdown);
3939

40-
// Get the Table of Contents
41-
$ToC = $parser->contentsList();
40+
// Get the Table of Contents in HTML
41+
$tocHTML = $parser->contentsList('html');
4242

4343
// Print the parsed HTML and ToC
4444
echo $html . PHP_EOL;
4545
echo "---" . PHP_EOL;
46-
echo $ToC . PHP_EOL;
46+
echo $tocHTML . PHP_EOL;
4747
```
4848

4949
```shellsession
@@ -121,10 +121,10 @@ require_once __DIR__ . '/vendor/autoload.php';
121121
// Sample Markdown with '[toc]' tag included
122122
$text_markdown = file_get_contents('SAMPLE.md');
123123

124-
$Parsedown = new \ParsedownToC();
124+
$parser = new \ParsedownToC();
125125

126126
// Parse Markdown and the '[toc]' tag to HTML
127-
$html = $Parsedown->text($text_markdown);
127+
$html = $parser->text($text_markdown);
128128

129129
echo $html . PHP_EOL;
130130

docs/classes/DynamicParent.html

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ <h2 class="phpdocumentor-content__title">
107107
<aside class="phpdocumentor-element-found-in">
108108
<abbr class="phpdocumentor-element-found-in__file" title="Extension.php"><a href="files/extension.html">Extension.php</a></abbr>
109109
:
110-
<span class="phpdocumentor-element-found-in__line">34</span>
110+
<span class="phpdocumentor-element-found-in__line">32</span>
111111

112112
</aside>
113113

@@ -175,7 +175,7 @@ <h4 class="phpdocumentor-element__name" id="method___construct">
175175
<aside class="phpdocumentor-element-found-in">
176176
<abbr class="phpdocumentor-element-found-in__file" title="Extension.php"><a href="files/extension.html">Extension.php</a></abbr>
177177
:
178-
<span class="phpdocumentor-element-found-in__line">36</span>
178+
<span class="phpdocumentor-element-found-in__line">34</span>
179179

180180
</aside>
181181

0 commit comments

Comments
 (0)