diff --git a/packages/guides-restructured-text/src/RestructuredText/Directives/CodeBlockDirective.php b/packages/guides-restructured-text/src/RestructuredText/Directives/CodeBlockDirective.php index f2b58a7c6..37d8e2bdc 100644 --- a/packages/guides-restructured-text/src/RestructuredText/Directives/CodeBlockDirective.php +++ b/packages/guides-restructured-text/src/RestructuredText/Directives/CodeBlockDirective.php @@ -62,6 +62,12 @@ public function process( BlockContext $blockContext, Directive $directive, ): Node|null { + if ($blockContext->getDocumentIterator()->isEmpty()) { + $this->logger->warning('The code-block has no content. Did you properly indent the code? ', $blockContext->getLoggerInformation()); + + return null; + } + $node = new CodeNode( $blockContext->getDocumentIterator()->toArray(), ); diff --git a/packages/guides-restructured-text/src/RestructuredText/Parser/LinesIterator.php b/packages/guides-restructured-text/src/RestructuredText/Parser/LinesIterator.php index a8e390095..f6c6388a2 100644 --- a/packages/guides-restructured-text/src/RestructuredText/Parser/LinesIterator.php +++ b/packages/guides-restructured-text/src/RestructuredText/Parser/LinesIterator.php @@ -17,6 +17,7 @@ use OutOfBoundsException; use function chr; +use function count; use function explode; use function max; use function mb_strpos; @@ -133,6 +134,11 @@ public function toArray(): array return $this->lines; } + public function isEmpty(): bool + { + return count($this->lines) === 0 || (count($this->lines) === 1 && trim($this->lines[0]) === ''); + } + /** @psalm-assert-if-false non-empty-string $line */ public static function isEmptyLine(string|null $line): bool { diff --git a/tests/Integration/tests/code/code-block-empty/expected/index.html b/tests/Integration/tests/code/code-block-empty/expected/index.html new file mode 100644 index 000000000..7113026f5 --- /dev/null +++ b/tests/Integration/tests/code/code-block-empty/expected/index.html @@ -0,0 +1,8 @@ + +
Some paragraph.
+# app/config/services.yml
- # app/config/services.yml
+ // config/routes.php