Skip to content
Merged
1 change: 1 addition & 0 deletions RELEASE_NOTES.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ This serves two purposes:
- Moved the sidebar documentation to the documentation pages section for better organization.
- The build command now groups together all `InMemoryPage` instances under one progress bar group in https://github.com/hydephp/develop/pull/1897
- The `Markdown::render()` method will now always render Markdown using the custom HydePHP Markdown service (thus getting smart features like our Markdown processors) in https://github.com/hydephp/develop/pull/1900
- Improved how the `MarkdownService` class is accessed, by binding it into the service container, in https://github.com/hydephp/develop/pull/1922

### Deprecated
- for soon-to-be removed features.
Expand Down
2 changes: 2 additions & 0 deletions packages/framework/src/Framework/HydeServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Hyde\Foundation\HydeKernel;
use Hyde\Pages\DocumentationPage;
use Hyde\Framework\Services\AssetService;
use Hyde\Framework\Services\MarkdownService;
use Hyde\Framework\Services\BuildTaskService;
use Hyde\Framework\Concerns\RegistersFileLocations;
use Illuminate\Support\ServiceProvider;
Expand All @@ -31,6 +32,7 @@ public function register(): void

$this->app->singleton(AssetService::class, AssetService::class);
$this->app->singleton(BuildTaskService::class, BuildTaskService::class);
$this->app->bind(MarkdownService::class, MarkdownService::class);

$this->kernel->setSourceRoot(Config::getString('hyde.source_root', ''));

Expand Down
5 changes: 4 additions & 1 deletion packages/framework/src/Markdown/Models/Markdown.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,9 @@ public static function fromFile(string $path): static
*/
public static function render(string $markdown, ?string $pageClass = null): string
{
return (new MarkdownService($markdown, $pageClass))->parse();
return app(MarkdownService::class, [
'markdown' => $markdown,
'pageClass' => $pageClass,
])->parse();
}
}
18 changes: 18 additions & 0 deletions packages/framework/tests/Feature/HydeServiceProviderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Hyde\Framework\Testing\Feature;

use Hyde\Framework\Services\MarkdownService;
use Hyde\Framework\Features\Navigation\MainNavigationMenu;
use Hyde\Framework\Features\Navigation\DocumentationSidebar;
use Illuminate\Contracts\Container\BindingResolutionException;
Expand Down Expand Up @@ -68,6 +69,23 @@ public function testProviderRegistersBuildTaskServiceAsSingleton()
$this->assertSame($this->app->make(BuildTaskService::class), $this->app->make(BuildTaskService::class));
}

public function testProviderRegistersMarkdownServiceAsBasicBinding()
{
$args = ['markdown' => 'foo'];

$this->assertTrue($this->app->bound(MarkdownService::class));
$this->assertInstanceOf(MarkdownService::class, $this->app->make(MarkdownService::class, $args));
$this->assertNotSame($this->app->make(MarkdownService::class, $args), $this->app->make(MarkdownService::class, $args));
}

public function testCanSwapMarkdownServiceBinding()
{
$this->app->bind(MarkdownService::class, fn () => 'foo');

$this->assertTrue($this->app->bound(MarkdownService::class));
$this->assertSame('foo', $this->app->make(MarkdownService::class));
}

public function testProviderRegistersSourceDirectories()
{
BladePage::setSourceDirectory('');
Expand Down
11 changes: 8 additions & 3 deletions packages/framework/tests/Unit/MarkdownFacadeTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,22 +4,27 @@

namespace Hyde\Framework\Testing\Unit;

use Mockery;
use Hyde\Testing\UnitTestCase;
use Hyde\Markdown\Models\Markdown;
use Hyde\Framework\Services\MarkdownService;

/**
* @covers \Hyde\Markdown\Models\Markdown
*/
class MarkdownFacadeTest extends UnitTestCase
{
protected static bool $needsKernel = true;
protected static bool $needsConfig = true;

public function testRender(): void
{
$mock = Mockery::mock(MarkdownService::class);
$mock->shouldReceive('parse')->once()->andReturn("<h1>Hello World!</h1>\n");
app()->bind(MarkdownService::class, fn () => $mock);

$html = Markdown::render('# Hello World!');

$this->assertIsString($html);
$this->assertSame("<h1>Hello World!</h1>\n", $html);

Mockery::close();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
* @covers \Hyde\Markdown\Models\MarkdownDocument
* @covers \Hyde\Markdown\Models\Markdown
*/
class MarkdownDocumentTest extends TestCase
class MarkdownHelpersTest extends TestCase
{
public function testConstructorCreatesNewMarkdownDocument()
{
Expand Down Expand Up @@ -108,4 +108,30 @@ public function testCarriageReturnsAreNormalized()
$markdown = new Markdown("foo\nbar");
$this->assertSame("foo\nbar", $markdown->body());
}

public function testRender(): void
{
$html = Markdown::render('# Hello World!');

$this->assertIsString($html);
$this->assertSame("<h1>Hello World!</h1>\n", $html);
}

public function testRenderWithCustomHydeMarkdownFeatures()
{
$html = Markdown::render(<<<'MARKDOWN'
# Hello World

>info Colored blockquote

[Home](/_pages/index.blade.php)
MARKDOWN);

$this->assertSame(<<<'HTML'
<h1>Hello World</h1>
<blockquote class="info"><p>Colored blockquote</p></blockquote>
<p><a href="index.html">Home</a></p>

HTML, $html);
}
}