Skip to content

Commit

Permalink
Replace Markdown parser with an interface
Browse files Browse the repository at this point in the history
Does NOT change public API, but you can override the contract in your
application's service provider.
  • Loading branch information
Roelof Roos committed May 7, 2021
1 parent fac03b7 commit b9e0691
Show file tree
Hide file tree
Showing 7 changed files with 108 additions and 2 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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))

<a name="2.3.0"></a>
# [2.3.0](https://github.com/saleem-hadad/larecipe/releases/tag/v2.3.0) (2020-03-09)
Expand Down
14 changes: 14 additions & 0 deletions src/Contracts/MarkdownParser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php

namespace BinaryTorch\LaRecipe\Contracts;

interface MarkdownParser
{
/**
* Parse the given source to Markdown, using your Markdown parser of choice.
*
* @param string $source
* @return null|string|string[]
*/
public function parse($source);
}
4 changes: 4 additions & 0 deletions src/LaRecipeServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
use BinaryTorch\LaRecipe\Commands\AssetCommand;
use BinaryTorch\LaRecipe\Commands\ThemeCommand;
use BinaryTorch\LaRecipe\Commands\InstallCommand;
use BinaryTorch\LaRecipe\Contracts\MarkdownParser;
use BinaryTorch\LaRecipe\Services\ParseDownMarkdownParser;
use BinaryTorch\LaRecipe\Facades\LaRecipe as LaRecipeFacade;
use BinaryTorch\LaRecipe\Commands\GenerateDocumentationCommand;

Expand Down Expand Up @@ -54,6 +56,8 @@ public function register()
$this->registerConsoleCommands();
}

$this->app->bind(MarkdownParser::class, ParseDownMarkdownParser::class);

$this->app->alias('LaRecipe', LaRecipeFacade::class);

$this->app->singleton('LaRecipe', function () {
Expand Down
20 changes: 20 additions & 0 deletions src/Services/ParseDownMarkdownParser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

namespace BinaryTorch\LaRecipe\Services;

use ParsedownExtra;
use BinaryTorch\LaRecipe\Contracts\MarkdownParser;

class ParseDownMarkdownParser implements MarkdownParser
{
/**
* Parse the given source to Markdown, using your Markdown parser of choice.
*
* @param string $source Markdown source contents
* @return null|string|string[] HTML output
*/
public function parse($source)
{
return (new ParsedownExtra)->text($source);
}
}
5 changes: 3 additions & 2 deletions src/Traits/HasMarkdownParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@

namespace BinaryTorch\LaRecipe\Traits;

use ParsedownExtra;
use Illuminate\Support\Facades\App;
use BinaryTorch\LaRecipe\Contracts\MarkdownParser;

trait HasMarkdownParser
{
Expand All @@ -13,6 +14,6 @@ trait HasMarkdownParser
*/
public function parse($text)
{
return (new ParsedownExtra)->text($text);
return App::make(MarkdownParser::class)->parse($text);
}
}
43 changes: 43 additions & 0 deletions tests/Feature/InterchangeableMarkdownParserTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php

namespace BinaryTorch\LaRecipe\Tests\Feature;

use Illuminate\Support\Str;
use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Config;
use BinaryTorch\LaRecipe\Tests\TestCase;
use BinaryTorch\LaRecipe\Contracts\MarkdownParser;
use BinaryTorch\LaRecipe\Tests\Fixtures\HelloWorldMarkdownParser;

class InterchangeableMarkdownParserTest extends TestCase
{
/** @test */
public function a_parser_is_accepted_as_long_as_the_contract_matches()
{
// set the docs path and landing
Config::set('larecipe.docs.path', 'tests/views/docs');
Config::set('larecipe.docs.landing', 'foo');

// set auth to false
Config::set('larecipe.settings.auth', false);

// Provide a dummy parser
$randomId = (string) Str::uuid();
App::instance(MarkdownParser::class, new HelloWorldMarkdownParser($randomId));

// guest can view foo page
$this->get('/docs/1.0')
->assertViewHasAll([
'title',
'index',
'content',
'currentVersion',
'versions',
'currentSection',
'canonical'
])
->assertSee("<h1>{$randomId}</h1>", false)
->assertDontSee('<h1>Foo</h1>', false)
->assertStatus(200);
}
}
23 changes: 23 additions & 0 deletions tests/Fixtures/HelloWorldMarkdownParser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php
namespace BinaryTorch\LaRecipe\Tests\Fixtures;

use BinaryTorch\LaRecipe\Contracts\MarkdownParser;

class HelloWorldMarkdownParser implements MarkdownParser
{
private $suffix;

public function __construct(string $suffix)
{
$this->suffix = $suffix;
}

public function parse($source)
{
return <<<HTML
<h1>{$this->suffix}</h1>
This is a test client, don't use in any other application!
HTML;
}
}

0 comments on commit b9e0691

Please sign in to comment.