Passed
Push — master ( 9f91a2...9bbf4f )
by Caen
08:15 queued 05:27
created

DocumentationPage::getTableOfContents()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace Hyde\Framework\Models\Pages;
4
5
use Hyde\Framework\Actions\GeneratesSidebarTableOfContents;
6
use Hyde\Framework\Concerns\FrontMatter\Schemas\DocumentationPageSchema;
7
use Hyde\Framework\Contracts\AbstractMarkdownPage;
8
use Hyde\Framework\Contracts\RouteContract;
9
use Hyde\Framework\Models\FrontMatter;
10
use Hyde\Framework\Models\Markdown;
11
use Hyde\Framework\Models\Route;
12
13
class DocumentationPage extends AbstractMarkdownPage
14
{
15
    use DocumentationPageSchema;
16
17
    public static string $sourceDirectory = '_docs';
18
    public static string $outputDirectory = 'docs';
19
    public static string $template = 'hyde::layouts/docs';
20
21
    /** @inheritDoc */
22
    public function __construct(string $identifier = '', ?FrontMatter $matter = null, ?Markdown $markdown = null)
23
    {
24
        parent::__construct($identifier, $matter, $markdown);
25
26
        $this->constructDocumentationPageSchema();
27
    }
28
29
    /** @inheritDoc */
30
    public function getCurrentPagePath(): string
31
    {
32
        return trim(static::getOutputDirectory().'/'.basename($this->identifier), '/');
33
    }
34
35
    /** @internal */
36
    public function getOnlineSourcePath(): string|false
37
    {
38
        if (config('docs.source_file_location_base') === null) {
39
            return false;
40
        }
41
42
        return trim(config('docs.source_file_location_base'), '/').'/'.$this->identifier.'.md';
43
    }
44
45
    public static function home(): ?RouteContract
46
    {
47
        return Route::exists(static::$outputDirectory.'/index') ? Route::get(static::$outputDirectory.'/index') : null;
48
    }
49
50
    public static function hasTableOfContents(): bool
51
    {
52
        return config('docs.table_of_contents.enabled', true);
53
    }
54
55
    /**
56
     * Generate Table of Contents as HTML from a Markdown document body.
57
     */
58
    public function getTableOfContents(): string
59
    {
60
        return (new GeneratesSidebarTableOfContents($this->markdown))->execute();
61
    }
62
}
63