Skip to content

Commit

Permalink
Fixed commonmark error when no language specified
Browse files Browse the repository at this point in the history
  • Loading branch information
alexgarrettsmith committed Apr 5, 2024
1 parent 05050a1 commit bfe36db
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/CommonMark/CodeBlockRenderer.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,16 @@ public function render(Node $node, ChildNodeRendererInterface $childRenderer)
$this->highlighter->withGutter((int)$startAt);
}

$parsed = $this->highlighter->parse($node->getLiteral(), $matches['language']);
$language = $matches['language'] ?? 'txt';

$parsed = $this->highlighter->parse($node->getLiteral(), $language);

$theme = $this->highlighter->getTheme();

if ($theme instanceof WithPre) {
return $theme->preBefore() . $parsed . $theme->preAfter();
} else {
return '<pre data-lang="' . $matches['language'] . '">' . $parsed . '</pre>';
return '<pre data-lang="' . $language . '">' . $parsed . '</pre>';
}
}
}
26 changes: 26 additions & 0 deletions tests/CommonMark/CodeBlockRendererTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,32 @@ public function test_commonmark_with_pre(): void
<pre style="color: #212121; background-color: #ffffff;"><span style="color: #D32F2F;">echo</span>;
</pre>

TXT;

$this->assertSame($expected, $markdown->convert($input)->getContent());
}

public function test_commonmark_with_no_language(): void
{
$environment = new Environment();

$environment
->addExtension(new CommonMarkCoreExtension())
->addExtension(new FrontMatterExtension())
->addRenderer(FencedCode::class, new CodeBlockRenderer());

$markdown = new MarkdownConverter($environment);

$input = <<<'TXT'
```
echo;
```
TXT;

$expected = <<<'TXT'
<pre data-lang="txt">echo;
</pre>

TXT;

$this->assertSame($expected, $markdown->convert($input)->getContent());
Expand Down

0 comments on commit bfe36db

Please sign in to comment.