Skip to content

Commit 7c22820

Browse files
authored
Merge pull request #270 from MotivoZwolle/feature/custom-markdown-parser-support
Retrieve Markdown parser from Application
2 parents fac03b7 + b9e0691 commit 7c22820

7 files changed

+108
-2
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ All notable changes to `larecipe` will be documented in this file
1212
- Add support for Laravel 8 - ([#235](https://github.com/saleem-hadad/larecipe/pull/235))
1313
- Permit show blade directives - ([#234](https://github.com/saleem-hadad/larecipe/pull/234))
1414
- Exclude code blocks from Blade compilation - ([#206](https://github.com/saleem-hadad/larecipe/pull/206))
15+
- Markdown is now parsed against a contract, allowing you to change it - ([#252](https://github.com/saleem-hadad/larecipe/issues/252))
1516

1617
<a name="2.3.0"></a>
1718
# [2.3.0](https://github.com/saleem-hadad/larecipe/releases/tag/v2.3.0) (2020-03-09)

src/Contracts/MarkdownParser.php

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
<?php
2+
3+
namespace BinaryTorch\LaRecipe\Contracts;
4+
5+
interface MarkdownParser
6+
{
7+
/**
8+
* Parse the given source to Markdown, using your Markdown parser of choice.
9+
*
10+
* @param string $source
11+
* @return null|string|string[]
12+
*/
13+
public function parse($source);
14+
}

src/LaRecipeServiceProvider.php

+4
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
use BinaryTorch\LaRecipe\Commands\AssetCommand;
88
use BinaryTorch\LaRecipe\Commands\ThemeCommand;
99
use BinaryTorch\LaRecipe\Commands\InstallCommand;
10+
use BinaryTorch\LaRecipe\Contracts\MarkdownParser;
11+
use BinaryTorch\LaRecipe\Services\ParseDownMarkdownParser;
1012
use BinaryTorch\LaRecipe\Facades\LaRecipe as LaRecipeFacade;
1113
use BinaryTorch\LaRecipe\Commands\GenerateDocumentationCommand;
1214

@@ -54,6 +56,8 @@ public function register()
5456
$this->registerConsoleCommands();
5557
}
5658

59+
$this->app->bind(MarkdownParser::class, ParseDownMarkdownParser::class);
60+
5761
$this->app->alias('LaRecipe', LaRecipeFacade::class);
5862

5963
$this->app->singleton('LaRecipe', function () {
+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?php
2+
3+
namespace BinaryTorch\LaRecipe\Services;
4+
5+
use ParsedownExtra;
6+
use BinaryTorch\LaRecipe\Contracts\MarkdownParser;
7+
8+
class ParseDownMarkdownParser implements MarkdownParser
9+
{
10+
/**
11+
* Parse the given source to Markdown, using your Markdown parser of choice.
12+
*
13+
* @param string $source Markdown source contents
14+
* @return null|string|string[] HTML output
15+
*/
16+
public function parse($source)
17+
{
18+
return (new ParsedownExtra)->text($source);
19+
}
20+
}

src/Traits/HasMarkdownParser.php

+3-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
namespace BinaryTorch\LaRecipe\Traits;
44

5-
use ParsedownExtra;
5+
use Illuminate\Support\Facades\App;
6+
use BinaryTorch\LaRecipe\Contracts\MarkdownParser;
67

78
trait HasMarkdownParser
89
{
@@ -13,6 +14,6 @@ trait HasMarkdownParser
1314
*/
1415
public function parse($text)
1516
{
16-
return (new ParsedownExtra)->text($text);
17+
return App::make(MarkdownParser::class)->parse($text);
1718
}
1819
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
<?php
2+
3+
namespace BinaryTorch\LaRecipe\Tests\Feature;
4+
5+
use Illuminate\Support\Str;
6+
use Illuminate\Support\Facades\App;
7+
use Illuminate\Support\Facades\Config;
8+
use BinaryTorch\LaRecipe\Tests\TestCase;
9+
use BinaryTorch\LaRecipe\Contracts\MarkdownParser;
10+
use BinaryTorch\LaRecipe\Tests\Fixtures\HelloWorldMarkdownParser;
11+
12+
class InterchangeableMarkdownParserTest extends TestCase
13+
{
14+
/** @test */
15+
public function a_parser_is_accepted_as_long_as_the_contract_matches()
16+
{
17+
// set the docs path and landing
18+
Config::set('larecipe.docs.path', 'tests/views/docs');
19+
Config::set('larecipe.docs.landing', 'foo');
20+
21+
// set auth to false
22+
Config::set('larecipe.settings.auth', false);
23+
24+
// Provide a dummy parser
25+
$randomId = (string) Str::uuid();
26+
App::instance(MarkdownParser::class, new HelloWorldMarkdownParser($randomId));
27+
28+
// guest can view foo page
29+
$this->get('/docs/1.0')
30+
->assertViewHasAll([
31+
'title',
32+
'index',
33+
'content',
34+
'currentVersion',
35+
'versions',
36+
'currentSection',
37+
'canonical'
38+
])
39+
->assertSee("<h1>{$randomId}</h1>", false)
40+
->assertDontSee('<h1>Foo</h1>', false)
41+
->assertStatus(200);
42+
}
43+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
<?php
2+
namespace BinaryTorch\LaRecipe\Tests\Fixtures;
3+
4+
use BinaryTorch\LaRecipe\Contracts\MarkdownParser;
5+
6+
class HelloWorldMarkdownParser implements MarkdownParser
7+
{
8+
private $suffix;
9+
10+
public function __construct(string $suffix)
11+
{
12+
$this->suffix = $suffix;
13+
}
14+
15+
public function parse($source)
16+
{
17+
return <<<HTML
18+
<h1>{$this->suffix}</h1>
19+
20+
This is a test client, don't use in any other application!
21+
HTML;
22+
}
23+
}

0 commit comments

Comments
 (0)