From 1b4022955540bc9cd8a769e3c3819a13286a6eb2 Mon Sep 17 00:00:00 2001 From: Caen De Silva Date: Thu, 17 Nov 2022 20:41:06 +0100 Subject: [PATCH] Add a Hyde::markdown() helper --- .../src/Foundation/Concerns/ImplementsStringHelpers.php | 7 +++++++ packages/framework/src/Hyde.php | 2 ++ packages/framework/tests/Feature/HydeKernelTest.php | 6 ++++++ 3 files changed, 15 insertions(+) diff --git a/packages/framework/src/Foundation/Concerns/ImplementsStringHelpers.php b/packages/framework/src/Foundation/Concerns/ImplementsStringHelpers.php index 7e810ad2d3a..afc34a1b8bf 100644 --- a/packages/framework/src/Foundation/Concerns/ImplementsStringHelpers.php +++ b/packages/framework/src/Foundation/Concerns/ImplementsStringHelpers.php @@ -4,6 +4,8 @@ namespace Hyde\Foundation\Concerns; +use Hyde\Markdown\Models\Markdown; +use Illuminate\Support\HtmlString; use Illuminate\Support\Str; /** @@ -23,4 +25,9 @@ public function makeTitle(string $slug): string Str::headline($slug) )); } + + public function markdown(string $text): HtmlString + { + return new HtmlString(Markdown::render($text)); + } } diff --git a/packages/framework/src/Hyde.php b/packages/framework/src/Hyde.php index c8b7821567e..1b479238ad9 100644 --- a/packages/framework/src/Hyde.php +++ b/packages/framework/src/Hyde.php @@ -12,6 +12,7 @@ use Hyde\Pages\Concerns\HydePage; use Hyde\Support\Models\Route; use Illuminate\Support\Facades\Facade; +use Illuminate\Support\HtmlString; /** * General facade for Hyde services. @@ -36,6 +37,7 @@ * @method static string image(string $name, bool $preferQualifiedUrl = false) * @method static string url(string $path = '') * @method static string makeTitle(string $slug) + * @method static HtmlString markdown(string $text) * @method static string currentPage() * @method static string getBasePath() * @method static string getSourceRoot() diff --git a/packages/framework/tests/Feature/HydeKernelTest.php b/packages/framework/tests/Feature/HydeKernelTest.php index c0f1d709165..e769a5f49b4 100644 --- a/packages/framework/tests/Feature/HydeKernelTest.php +++ b/packages/framework/tests/Feature/HydeKernelTest.php @@ -16,6 +16,7 @@ use Hyde\Testing\TestCase; use Illuminate\Support\Facades\Config; use Illuminate\Support\Facades\View; +use Illuminate\Support\HtmlString; /** * This test class runs high-level tests on the HydeKernel class, @@ -90,6 +91,11 @@ public function test_make_title_helper_returns_title_from_page_slug() $this->assertEquals('Foo Bar', Hyde::makeTitle('foo-bar')); } + public function test_markdown_helper_converts_markdown_to_html() + { + $this->assertEquals(new HtmlString("

foo

\n"), Hyde::markdown('foo')); + } + public function test_format_html_path_helper_formats_path_according_to_config_rules() { Config::set('site.pretty_urls', false);