Passed
Push — master ( 137957...c626e1 )
by Caen
03:59 queued 14s
created

DocumentationPage   A

Complexity

Total Complexity 10

Size/Duplication

Total Lines 61
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
eloc 17
dl 0
loc 61
rs 10
c 0
b 0
f 0
wmc 10

7 Methods

Rating   Name   Duplication   Size   Complexity  
A getOnlineSourcePath() 0 7 2
A home() 0 3 1
A getOutputPath() 0 5 2
A getRouteKey() 0 5 2
A getTableOfContents() 0 3 1
A homeRouteName() 0 3 1
A hasTableOfContents() 0 3 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Hyde\Pages;
6
7
use Hyde\Facades\Config;
8
use Hyde\Framework\Actions\GeneratesSidebarTableOfContents;
9
use Hyde\Pages\Concerns\BaseMarkdownPage;
10
use Hyde\Support\Models\Route;
11
12
/**
13
 * Page class for documentation pages.
14
 *
15
 * Documentation pages are stored in the _docs directory and using the .md extension.
16
 * The Markdown will be compiled to HTML using the documentation page layout to the _site/docs/ directory.
17
 *
18
 * @see https://hydephp.com/docs/master/documentation-pages
19
 */
20
class DocumentationPage extends BaseMarkdownPage
21
{
22
    public static string $sourceDirectory = '_docs';
23
    public static string $outputDirectory = 'docs';
24
    public static string $template = 'hyde::layouts/docs';
25
26
    public static function home(): ?Route
27
    {
28
        return Route::get(static::homeRouteName());
29
    }
30
31
    public static function homeRouteName(): string
32
    {
33
        return static::baseRouteKey().'/index';
34
    }
35
36
    /** @see https://hydephp.com/docs/master/documentation-pages#automatic-edit-page-button */
37
    public function getOnlineSourcePath(): string|false
38
    {
39
        if (config('docs.source_file_location_base') === null) {
40
            return false;
41
        }
42
43
        return trim((string) config('docs.source_file_location_base'), '/').'/'.$this->identifier.'.md';
44
    }
45
46
    public static function hasTableOfContents(): bool
47
    {
48
        return config('docs.table_of_contents.enabled', true);
49
    }
50
51
    /**
52
     * Generate Table of Contents as HTML from a Markdown document body.
53
     */
54
    public function getTableOfContents(): string
55
    {
56
        return (new GeneratesSidebarTableOfContents($this->markdown))->execute();
57
    }
58
59
    /**
60
     * Get the route key for the page.
61
     *
62
     * If flattened outputs are enabled, this will use the identifier basename so nested pages are flattened.
63
     */
64
    public function getRouteKey(): string
65
    {
66
        return Config::getBool('docs.flattened_output_paths', true)
67
            ? unslash(static::outputDirectory().'/'.basename($this->identifier))
68
            : parent::getRouteKey();
69
    }
70
71
    /**
72
     * Get the path where the compiled page will be saved.
73
     *
74
     * If flattened outputs are enabled, this will use the identifier basename so nested pages are flattened.
75
     */
76
    public function getOutputPath(): string
77
    {
78
        return Config::getBool('docs.flattened_output_paths', true)
79
            ? static::outputPath(basename($this->identifier))
80
            : parent::getOutputPath();
81
    }
82
}
83