Skip to content

Commit

Permalink
Adds support for parsedown extra (sixlive#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
sixlive authored May 20, 2020
1 parent e05632e commit 9b64af1
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 1 deletion.
10 changes: 10 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,16 @@ $parsedown->text(file_get_contents(__DIR__.'/README.md'));
<pre><code class="language-asldfh">put 'WHOOP!'</code></pre>
```

### Using Parsedown Extra
**Note: This requires version [0.8.0-beta-1](https://github.com/erusev/parsedown-extra/releases/tag/0.8.0-beta-1)**

```php
$parsedown = new \sixlive\ParsedownHighlightExtra;

$parsedown->text(file_get_contents(__DIR__.'/README.md'));
```


## Testing

``` bash
Expand Down
3 changes: 2 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
"require": {
"php": "^7.1|7.2",
"erusev/parsedown": "1.8.0-beta-5",
"scrivo/highlight.php": "^9.14"
"scrivo/highlight.php": "^9.14",
"erusev/parsedown-extra": "0.8.0-beta-1"
},
"require-dev": {
"friendsofphp/php-cs-fixer": "^2.10",
Expand Down
42 changes: 42 additions & 0 deletions src/ParsedownHighlightExtra.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
<?php

namespace sixlive;

use ParsedownExtra;
use DomainException;
use Highlight\Highlighter;

class ParsedownHighlightExtra extends ParsedownExtra
{
protected $highlighter;

public function __construct()
{
$this->highlighter = new Highlighter;
}

protected function blockFencedCodeComplete($block)
{
if (! isset($block['element']['element']['attributes'])) {
return $block;
}

$code = $block['element']['element']['text'];
$languageClass = $block['element']['element']['attributes']['class'];
$language = explode('-', $languageClass);

try {
$highlighted = $this->highlighter->highlight($language[1], $code);
$block['element']['element']['attributes']['class'] = vsprintf('%s hljs %s', [
$languageClass,
$highlighted->language,
]);
$block['element']['element']['rawHtml'] = $highlighted->value;
unset($block['element']['element']['text']);
} catch (DomainException $e) {
//
}

return $block;
}
}
29 changes: 29 additions & 0 deletions tests/ParsedownExtraHighlightTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php

namespace Tests;

use PHPUnit\Framework\TestCase;
use sixlive\ParsedownHighlightExtra;

class ParsedownExtraHighlightTest extends TestCase
{
/** @test */
public function code_is_rendered()
{
$parsedown = new ParsedownHighlightExtra;

$renderedMarkdown = $parsedown->text(file_get_contents(__DIR__.'/Support/test.md'));

$this->assertContains("<pre><code class=\"language-php hljs php\"><span class=\"hljs-meta\">&lt;?php</span>\n\n<span class=\"hljs-keyword\">echo</span> <span class=\"hljs-string\">'foo'</span>;</code></pre>", $renderedMarkdown);
}

/** @test */
public function language_errors_are_swallowd()
{
$parsedown = new ParsedownHighlightExtra;

$renderedMarkdown = $parsedown->text(file_get_contents(__DIR__.'/Support/test.md'));

$this->assertContains("<pre><code class=\"language-asldfh\">put 'WHOOP!'</code></pre>", $renderedMarkdown);
}
}

0 comments on commit 9b64af1

Please sign in to comment.