forked from sixlive/parsedown-highlight
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds support for parsedown extra (sixlive#10)
- Loading branch information
Showing
4 changed files
with
83 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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\"><?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); | ||
} | ||
} |