Skip to content

Commit

Permalink
Merge pull request #35 from phannaly/master
Browse files Browse the repository at this point in the history
Disable interpolation
  • Loading branch information
Saleem Hadad authored Sep 22, 2018
2 parents e3e17ac + 1cb8a9f commit f83938a
Show file tree
Hide file tree
Showing 9 changed files with 92 additions and 8 deletions.
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
],
"require": {
"illuminate/support": "~5.4.0|~5.5.0|~5.6.0|~5.7.0",
"illuminate/view": "~5.4.0|~5.5.0|~5.6.0|~5.7.0",
"erusev/parsedown-extra": "^0.7.1",
"symfony/dom-crawler": "^4.1"
},
Expand Down
2 changes: 1 addition & 1 deletion publishable/assets/js/app.js

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions resources/js/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,11 @@ import Argon from "./plugins/argon-kit";

Vue.use(Argon);
Vue.config.productionTip = false;
const noDelimiter = {replace: () => '(?!x)x'};

const app = new Vue({
el: '#app',
delimiters: [noDelimiter, noDelimiter],
data() {
return {
sidebar: false,
Expand Down
4 changes: 2 additions & 2 deletions src/DocumentationRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,12 +39,12 @@ public function __construct(Documentation $documentation)
*
* @return $this
*/
public function get($version, $page = null)
public function get($version, $page = null, $data = [])
{
$this->sectionPage = $page ?: config('larecipe.docs.landing');
$this->index = $this->documentation->getIndex($version);

$this->content = $this->documentation->get($version, $this->sectionPage);
$this->content = $this->documentation->get($version, $this->sectionPage, $data);

if (is_null($this->content)) {
return $this->prepareNotFound();
Expand Down
12 changes: 7 additions & 5 deletions src/Models/Documentation.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@

namespace BinaryTorch\LaRecipe\Models;

use Illuminate\Filesystem\Filesystem;
use BinaryTorch\LaRecipe\Traits\HasBladeParser;
use BinaryTorch\LaRecipe\Traits\HasMarkdownParser;
use Illuminate\Contracts\Cache\Repository as Cache;
use Illuminate\Filesystem\Filesystem;

class Documentation
{
use HasMarkdownParser;
use HasMarkdownParser, HasBladeParser;

/**
* The filesystem implementation.
Expand Down Expand Up @@ -73,13 +74,14 @@ public function getIndex($version)
* @param string $page
* @return string
*/
public function get($version, $page)
public function get($version, $page, $data = [])
{
$closure = function () use ($version, $page) {
$closure = function () use ($version, $page, $data) {
$path = base_path(config('larecipe.docs.path').'/'.$version.'/'.$page.'.md');

if ($this->files->exists($path)) {
$parsedContent = $this->parse($this->files->get($path));
$content = $this->parse($this->files->get($path));
$parsedContent = $this->renderBlade($content, $data);

return $this->replaceLinks($version, $parsedContent);
}
Expand Down
48 changes: 48 additions & 0 deletions src/Traits/HasBladeParser.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

namespace BinaryTorch\LaRecipe\Traits;

use Illuminate\Support\Facades\Blade;

trait HasBladeParser
{
/**
* Render markdown contain blade syntax
*
* @param $content
* @param $data
* @return string
*/
public function renderBlade($content, $data = [])
{
$content = $this->compileBlade($content);
$obLevel = ob_get_level();
ob_start();
extract($data, EXTR_SKIP);

try {
eval('?' . '>' . $content);
} catch (\Exception $e) {
while (ob_get_level() > $obLevel) ob_end_clean();
throw $e;
} catch (\Throwable $e) {
while (ob_get_level() > $obLevel) ob_end_clean();
throw new \Exception($e);
}

$contents = ob_get_clean();

return $contents;
}

/**
* Compile blade content
*
* @param $content
* @return string
*/
public function compileBlade($content)
{
return Blade::compileString($content);
}
}
14 changes: 14 additions & 0 deletions tests/Unit/DocumentationRepositoryTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -102,4 +102,18 @@ public function it_has_all_published_versions()
$this->documentationRepository = $this->app->make(DocumentationRepository::class);
$this->assertEquals(['1.0', '2.0', '2.1'], $this->documentationRepository->publishedVersions);
}

/** @test */
public function it_can_has_blade_syntax_in_markdown()
{
Config::set('larecipe.docs.path', 'tests/views/docs');

// Simple data that will render in blade syntax inside markdown file
$data = [
"issues" => ["one", "two", "three"]
];

$documentation = $this->documentationRepository->get('1.0', 'blade', $data);
$this->assertContains('issues: 3', $documentation->content);
}
}
14 changes: 14 additions & 0 deletions tests/Unit/DocumentationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,18 @@ public function it_caches_the_requested_documentation_and_index_for_a_given_peri
$this->documentation->get('1.0', 'bar');
$this->assertNull(cache('larecipe.docs.1.0.bar'));
}

/** @test */
public function it_can_compile_blade_content()
{
$bladeContent = "{{ csrf_field() }}";
$this->assertEquals('<?php echo e(csrf_field()); ?>', $this->documentation->compileBlade($bladeContent));
}

/** @test */
public function it_can_render_blade_content()
{
$content = "{{ count(['foo', 'bar']) }}";
$this->assertEquals(2, $this->documentation->renderBlade($content));
}
}
3 changes: 3 additions & 0 deletions tests/views/docs/1.0/blade.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# Get started with blade syntax

issues: {{ count($issues) }}

0 comments on commit f83938a

Please sign in to comment.