From 18b43eaf9deabb1060ddf8b4bcec252a10beba97 Mon Sep 17 00:00:00 2001 From: Ryan Chandler Date: Thu, 28 Aug 2025 17:46:33 +0100 Subject: [PATCH 1/2] Fallback to txt when grammar does not exist in CommonMark extension --- src/Adapters/CommonMark/CodeBlockRenderer.php | 4 ++++ ...ack_to_txt_if_the_grammar_doesnt_exist.snap | 2 ++ .../Adapters/CommonMark/PhikiExtensionTest.php | 18 ++++++++++++++++++ 3 files changed, 24 insertions(+) create mode 100644 tests/.pest/snapshots/Adapters/CommonMark/PhikiExtensionTest/it_falls_back_to_txt_if_the_grammar_doesnt_exist.snap diff --git a/src/Adapters/CommonMark/CodeBlockRenderer.php b/src/Adapters/CommonMark/CodeBlockRenderer.php index 4ba1917..1f56fe3 100644 --- a/src/Adapters/CommonMark/CodeBlockRenderer.php +++ b/src/Adapters/CommonMark/CodeBlockRenderer.php @@ -46,6 +46,10 @@ protected function detectGrammar(FencedCode $node): Grammar|string preg_match('/[a-zA-Z]+/', $node->getInfoWords()[0], $matches); + if (! $this->phiki->environment->grammars->has($matches[0])) { + return 'txt'; + } + return $matches[0]; } } diff --git a/tests/.pest/snapshots/Adapters/CommonMark/PhikiExtensionTest/it_falls_back_to_txt_if_the_grammar_doesnt_exist.snap b/tests/.pest/snapshots/Adapters/CommonMark/PhikiExtensionTest/it_falls_back_to_txt_if_the_grammar_doesnt_exist.snap new file mode 100644 index 0000000..0dd5421 --- /dev/null +++ b/tests/.pest/snapshots/Adapters/CommonMark/PhikiExtensionTest/it_falls_back_to_txt_if_the_grammar_doesnt_exist.snap @@ -0,0 +1,2 @@ +
class A {}
+
diff --git a/tests/Adapters/CommonMark/PhikiExtensionTest.php b/tests/Adapters/CommonMark/PhikiExtensionTest.php index 1e6e8f3..b4aed43 100644 --- a/tests/Adapters/CommonMark/PhikiExtensionTest.php +++ b/tests/Adapters/CommonMark/PhikiExtensionTest.php @@ -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(); +}); From 9600b05b34468f3b3beeb86e2b57f05073b2d38c Mon Sep 17 00:00:00 2001 From: Ryan Chandler Date: Thu, 28 Aug 2025 17:46:50 +0100 Subject: [PATCH 2/2] Use the Grammar enum instead of magic string --- src/Adapters/CommonMark/CodeBlockRenderer.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Adapters/CommonMark/CodeBlockRenderer.php b/src/Adapters/CommonMark/CodeBlockRenderer.php index 1f56fe3..a177bde 100644 --- a/src/Adapters/CommonMark/CodeBlockRenderer.php +++ b/src/Adapters/CommonMark/CodeBlockRenderer.php @@ -41,13 +41,13 @@ 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 'txt'; + return Grammar::Txt; } return $matches[0];