Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds support for parsedown extra #10

Merged
merged 1 commit into from
May 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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);
}
}