From b9e069144d159a544078973efcab6be63d992d0a Mon Sep 17 00:00:00 2001 From: Roelof Roos Date: Fri, 7 May 2021 11:57:13 +0200 Subject: [PATCH] Replace Markdown parser with an interface Does NOT change public API, but you can override the contract in your application's service provider. --- CHANGELOG.md | 1 + src/Contracts/MarkdownParser.php | 14 ++++++ src/LaRecipeServiceProvider.php | 4 ++ src/Services/ParseDownMarkdownParser.php | 20 +++++++++ src/Traits/HasMarkdownParser.php | 5 ++- .../InterchangeableMarkdownParserTest.php | 43 +++++++++++++++++++ tests/Fixtures/HelloWorldMarkdownParser.php | 23 ++++++++++ 7 files changed, 108 insertions(+), 2 deletions(-) create mode 100644 src/Contracts/MarkdownParser.php create mode 100644 src/Services/ParseDownMarkdownParser.php create mode 100644 tests/Feature/InterchangeableMarkdownParserTest.php create mode 100644 tests/Fixtures/HelloWorldMarkdownParser.php diff --git a/CHANGELOG.md b/CHANGELOG.md index 89a965d6..07b0e191 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -12,6 +12,7 @@ All notable changes to `larecipe` will be documented in this file - Add support for Laravel 8 - ([#235](https://github.com/saleem-hadad/larecipe/pull/235)) - Permit show blade directives - ([#234](https://github.com/saleem-hadad/larecipe/pull/234)) - Exclude code blocks from Blade compilation - ([#206](https://github.com/saleem-hadad/larecipe/pull/206)) +- Markdown is now parsed against a contract, allowing you to change it - ([#252](https://github.com/saleem-hadad/larecipe/issues/252)) # [2.3.0](https://github.com/saleem-hadad/larecipe/releases/tag/v2.3.0) (2020-03-09) diff --git a/src/Contracts/MarkdownParser.php b/src/Contracts/MarkdownParser.php new file mode 100644 index 00000000..7884509c --- /dev/null +++ b/src/Contracts/MarkdownParser.php @@ -0,0 +1,14 @@ +registerConsoleCommands(); } + $this->app->bind(MarkdownParser::class, ParseDownMarkdownParser::class); + $this->app->alias('LaRecipe', LaRecipeFacade::class); $this->app->singleton('LaRecipe', function () { diff --git a/src/Services/ParseDownMarkdownParser.php b/src/Services/ParseDownMarkdownParser.php new file mode 100644 index 00000000..8ef471cc --- /dev/null +++ b/src/Services/ParseDownMarkdownParser.php @@ -0,0 +1,20 @@ +text($source); + } +} diff --git a/src/Traits/HasMarkdownParser.php b/src/Traits/HasMarkdownParser.php index 7a40c1a8..32abf003 100644 --- a/src/Traits/HasMarkdownParser.php +++ b/src/Traits/HasMarkdownParser.php @@ -2,7 +2,8 @@ namespace BinaryTorch\LaRecipe\Traits; -use ParsedownExtra; +use Illuminate\Support\Facades\App; +use BinaryTorch\LaRecipe\Contracts\MarkdownParser; trait HasMarkdownParser { @@ -13,6 +14,6 @@ trait HasMarkdownParser */ public function parse($text) { - return (new ParsedownExtra)->text($text); + return App::make(MarkdownParser::class)->parse($text); } } diff --git a/tests/Feature/InterchangeableMarkdownParserTest.php b/tests/Feature/InterchangeableMarkdownParserTest.php new file mode 100644 index 00000000..5358fbe1 --- /dev/null +++ b/tests/Feature/InterchangeableMarkdownParserTest.php @@ -0,0 +1,43 @@ +get('/docs/1.0') + ->assertViewHasAll([ + 'title', + 'index', + 'content', + 'currentVersion', + 'versions', + 'currentSection', + 'canonical' + ]) + ->assertSee("

{$randomId}

", false) + ->assertDontSee('

Foo

', false) + ->assertStatus(200); + } +} diff --git a/tests/Fixtures/HelloWorldMarkdownParser.php b/tests/Fixtures/HelloWorldMarkdownParser.php new file mode 100644 index 00000000..4343db40 --- /dev/null +++ b/tests/Fixtures/HelloWorldMarkdownParser.php @@ -0,0 +1,23 @@ +suffix = $suffix; + } + + public function parse($source) + { + return <<{$this->suffix} + + This is a test client, don't use in any other application! + HTML; + } +}