Skip to content

Commit

Permalink
Add the table of contents generation
Browse files Browse the repository at this point in the history
  • Loading branch information
caendesilva committed Apr 19, 2022
1 parent 627e570 commit 2c4c1b9
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 0 deletions.
53 changes: 53 additions & 0 deletions src/Actions/GeneratesTableOfContents.php
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);
}
}
9 changes: 9 additions & 0 deletions src/Models/DocumentationPage.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,15 @@

class DocumentationPage extends MarkdownDocument
{
use HasTableOfContents;

public static string $sourceDirectory = '_docs';
public static string $parserClass = DocumentationPageParser::class;

public function __construct(array $matter, string $body, string $title = '', string $slug = '')
{
parent::__construct($matter, $body, $title, $slug);

$this->constructTableOfContents();
}
}
20 changes: 20 additions & 0 deletions src/Models/HasTableOfContents.php
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();
}
}

0 comments on commit 2c4c1b9

Please sign in to comment.