-
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add the table of contents generation
- Loading branch information
1 parent
627e570
commit 2c4c1b9
Showing
3 changed files
with
82 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
<?php | ||
|
||
namespace Hyde\Framework\Actions; | ||
|
||
use League\CommonMark\Environment\Environment; | ||
use League\CommonMark\Extension\CommonMark\CommonMarkCoreExtension; | ||
use League\CommonMark\Extension\HeadingPermalink\HeadingPermalinkExtension; | ||
use League\CommonMark\Extension\TableOfContents\TableOfContentsExtension; | ||
use League\CommonMark\MarkdownConverter; | ||
|
||
/** | ||
* Generates a table of contents for the Markdown document. | ||
* @see \Tests\Feature\Actions\GeneratesTableOfContentsTest | ||
*/ | ||
class GeneratesTableOfContents implements ActionContract | ||
{ | ||
protected string $markdown; | ||
|
||
public function __construct(string $markdown) | ||
{ | ||
$this->markdown = $markdown; | ||
} | ||
|
||
public static int $minHeadingLevel = 2; | ||
public static int $maxHeadingLevel = 4; | ||
|
||
public function execute(): string | ||
{ | ||
$config = [ | ||
'table_of_contents' => [ | ||
'html_class' => 'table-of-contents', | ||
'position' => 'top', | ||
'style' => 'bullet', | ||
'min_heading_level' => static::$minHeadingLevel, | ||
'max_heading_level' => static::$maxHeadingLevel, | ||
'normalize' => 'relative', | ||
], | ||
'heading_permalink' => [ | ||
'fragment_prefix' => '', | ||
], | ||
]; | ||
|
||
$environment = new Environment($config); | ||
$environment->addExtension(new CommonMarkCoreExtension()); | ||
$environment->addExtension(new HeadingPermalinkExtension()); | ||
$environment->addExtension(new TableOfContentsExtension()); | ||
|
||
$converter = new MarkdownConverter($environment); | ||
$html = $converter->convert("[[END_TOC]]\n" . $this->markdown)->getContent(); | ||
|
||
return substr($html, 0, strpos($html, '[[END_TOC]]') - 9); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
<?php | ||
|
||
namespace Hyde\Framework\Models; | ||
|
||
use Hyde\Framework\Actions\GeneratesTableOfContents; | ||
|
||
/** | ||
* Trait HasTableOfContents. | ||
* | ||
* @see \Tests\Unit\HasTableOfContentsTest | ||
*/ | ||
trait HasTableOfContents | ||
{ | ||
public string $tableOfContents; | ||
|
||
public function constructTableOfContents(): void | ||
{ | ||
$this->tableOfContents = (new GeneratesTableOfContents($this->body))->execute(); | ||
} | ||
} |