Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extract trait for methods used to flatten documentation pages #653

Merged
merged 10 commits into from
Nov 8, 2022
40 changes: 40 additions & 0 deletions packages/framework/src/Pages/Concerns/UsesFlattenedOutputPaths.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<?php

declare(strict_types=1);

namespace Hyde\Pages\Concerns;

use function basename;
use function unslash;

/**
* @internal This trait is currently experimental and should not be relied upon outside of Hyde.
*
* This trait is used to flatten the output path of a page. This is only used for the documentation pages,
* where all pages are output to the same directory, but where putting the page in a subdirectory will
* create a nested navigation structure in the sidebar.
*
* @see https://hydephp.com/docs/master/documentation-pages#using-subdirectories
*/
trait UsesFlattenedOutputPaths
{
/**
* Get the route key for the page.
*
* Uses the identifier basename so nested pages are flattened.
*/
public function getRouteKey(): string
{
return unslash(static::outputDirectory().'/'.basename($this->identifier));
}

/**
* Get the path where the compiled page will be saved.
*
* Uses the identifier basename so nested pages are flattened.
*/
public function getOutputPath(): string
{
return static::outputPath(basename($this->identifier));
}
}
20 changes: 4 additions & 16 deletions packages/framework/src/Pages/DocumentationPage.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,15 @@
*/
class DocumentationPage extends BaseMarkdownPage implements DocumentationPageSchema
{
use Concerns\UsesFlattenedOutputPaths;

public static string $sourceDirectory = '_docs';
public static string $outputDirectory = 'docs';
public static string $template = 'hyde::layouts/docs';

/** @inheritDoc */
public function getRouteKey(): string
public static function home(): ?Route
{
return trim(static::outputDirectory().'/'.basename($this->identifier), '/');
return Route::get(static::$outputDirectory.'/index');
}

/** @see https://hydephp.com/docs/master/documentation-pages#automatic-edit-page-button */
Expand All @@ -39,11 +40,6 @@ public function getOnlineSourcePath(): string|false
return trim(config('docs.source_file_location_base'), '/').'/'.$this->identifier.'.md';
}

public static function home(): ?Route
{
return Route::get(static::$outputDirectory.'/index');
}

public static function hasTableOfContents(): bool
{
return config('docs.table_of_contents.enabled', true);
Expand All @@ -56,12 +52,4 @@ public function getTableOfContents(): string
{
return (new GeneratesSidebarTableOfContents($this->markdown))->execute();
}

/**
* Return the output path for the identifier basename so nested pages are flattened.
*/
public function getOutputPath(): string
{
return static::outputPath(basename($this->identifier));
}
}