Skip to content
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
6 changes: 5 additions & 1 deletion src/Adapters/CommonMark/CodeBlockRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,15 @@ public function render(Node $node, ChildNodeRendererInterface $childRenderer)
protected function detectGrammar(FencedCode $node): Grammar|string
{
if (! isset($node->getInfoWords()[0]) || $node->getInfoWords()[0] === '') {
return 'txt';
return Grammar::Txt;
}

preg_match('/[a-zA-Z]+/', $node->getInfoWords()[0], $matches);

if (! $this->phiki->environment->grammars->has($matches[0])) {
return Grammar::Txt;
}

return $matches[0];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
<pre class="phiki github-dark" style="background-color: #24292e;color: #e1e4e8;"><code><span class="line"><span class="token">class A {}
</span></span></code></pre>
18 changes: 18 additions & 0 deletions tests/Adapters/CommonMark/PhikiExtensionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -65,3 +65,21 @@ class A {}

expect($generated)->toMatchSnapshot();
});

it('falls back to txt if the grammar doesnt exist', function () {
$environment = new Environment;

$environment
->addExtension(new CommonMarkCoreExtension)
->addExtension(new PhikiExtension('github-dark'));

$markdown = new MarkdownConverter($environment);

$generated = $markdown->convert(<<<'MD'
```nonexistentlang
class A {}
```
MD);

expect($generated)->toMatchSnapshot();
});