Skip to content

Commit

Permalink
Merge pull request #19 from ARCANEDEV/update-package
Browse files Browse the repository at this point in the history
Adding league/commonmark v2
  • Loading branch information
arcanedev-maroc authored Jul 28, 2021
2 parents e023dbc + 6269cf5 commit fc64689
Show file tree
Hide file tree
Showing 7 changed files with 67 additions and 23 deletions.
6 changes: 4 additions & 2 deletions .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ jobs:
steps:
- name: Checkout code
uses: actions/checkout@v2
with:
fetch-depth: 2

- name: Cache dependencies
uses: actions/cache@v2
Expand All @@ -42,5 +44,5 @@ jobs:
- name: Scrutinizer CI
run: |
wget https://scrutinizer-ci.com/ocular.phar
php ocular.phar code-coverage:upload --format=php-clover coverage.clover
composer require scrutinizer/ocular --dev
vendor/bin/ocular code-coverage:upload --format=php-clover coverage.clover
10 changes: 6 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,11 @@
"require": {
"php": "^7.3|^8.0",
"arcanedev/support": "^8.0",
"league/commonmark": "^1.3"
"league/commonmark": "^1.3|^2.0"
},
"require-dev": {
"orchestra/testbench": "^6.4",
"laravel/framework": "^8.0",
"orchestra/testbench-core": "^6.4",
"phpunit/phpunit": "^9.3.3"
},
"autoload": {
Expand All @@ -34,8 +35,9 @@
}
},
"scripts": {
"test": "phpunit",
"coverage": "phpunit --coverage-html build/coverage/html"
"test": "phpunit --colors=always",
"test:dox": "phpunit --testdox --colors=always",
"test:cov": "phpunit --coverage-html coverage"
},
"extra": {
"branch-alias": {
Expand Down
5 changes: 4 additions & 1 deletion config/markdown.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@

'parsers' => [
'commonmark' => [
'class' => Arcanedev\LaravelMarkdown\Parsers\CommonMarkParser::class,
'class' => Arcanedev\LaravelMarkdown\Parsers\CommonMarkParser::class,
'options' => [
'allow_unsafe_links' => false,
],
],
],

Expand Down
12 changes: 9 additions & 3 deletions src/Markdown.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,15 @@ public function parser($driver = null)
*/
public function buildParsers(): void
{
foreach ($this->config->get('markdown.parsers', []) as $name => $parser) {
$this->extend($name, function (Application $app) use ($parser) {
return $app->make($parser['class']);
$parsers = array_keys($this->config->get('markdown.parsers', []));

foreach ($parsers as $name) {
$this->extend($name, function (Application $app) use ($name) {
$config = $app['config'];

return $app
->make($config->get("markdown.parsers.{$name}.class"))
->setOptions($config->get("markdown.parsers.{$name}.options") ?? []);
});
}
}
Expand Down
36 changes: 36 additions & 0 deletions src/Parsers/AbstractParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,42 @@ abstract class AbstractParser implements Parser
*/
protected $buffering = false;

/**
* Parser's options.
*
* @var array
*/
protected $options = [];

/* -----------------------------------------------------------------
| Getters & Setters
| -----------------------------------------------------------------
*/

/**
* Get the parser's options.
*
* @return array
*/
public function getOptions(): array
{
return $this->options;
}

/**
* Set the parser's options.
*
* @param array $options
*
* @return $this
*/
public function setOptions(array $options): self
{
$this->options = $options;

return $this;
}

/* -----------------------------------------------------------------
| Main Methods
| -----------------------------------------------------------------
Expand Down
15 changes: 5 additions & 10 deletions src/Parsers/CommonMarkParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,7 @@
namespace Arcanedev\LaravelMarkdown\Parsers;

use Illuminate\Support\HtmlString;
use League\CommonMark\CommonMarkConverter;
use League\CommonMark\Environment;
use League\CommonMark\Extension\Table\TableExtension;
use League\CommonMark\GithubFlavoredMarkdownConverter;

/**
* Class CommonMarkParser
Expand All @@ -30,13 +28,10 @@ class CommonMarkParser extends AbstractParser
*/
public function parse(string $text): HtmlString
{
$environment = Environment::createCommonMarkEnvironment()
->addExtension(new TableExtension);
$converter = new GithubFlavoredMarkdownConverter($this->getOptions());

$converter = new CommonMarkConverter([
'allow_unsafe_links' => false,
], $environment);

return new HtmlString($converter->convertToHtml($text));
return new HtmlString(
(string) $converter->convertToHtml($text)
);
}
}
6 changes: 3 additions & 3 deletions tests/MarkdownTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -127,11 +127,11 @@ public function it_can_skip_clean_xss(): void
/** @test */
public function it_can_skip_escape_markups(): void
{
$this->app['config']->set('markdown.parser.options.html_input', false);
$this->app['config']->set('markdown.parsers.commonmark.options.html_input', 'allow');

static::assertStringContainsString(
'<p><b>This is a script</b><script>alert(\'hello\');</script></p>',
$this->markdown()->parse("<b>This is a script</b><script>alert('hello');</script>")->toHtml()
"<p><strong>This is a script</strong>&lt;script>alert('hello');&lt;/script></p>\n",
$this->markdown()->parse("**This is a script**<script>alert('hello');</script>")->toHtml()
);
}

Expand Down

0 comments on commit fc64689

Please sign in to comment.