Skip to content

[TASK] Warn when code-block has no content #823

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jan 21, 2024
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
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use OutOfBoundsException;

use function chr;
use function count;
use function explode;
use function max;
use function mb_strpos;
Expand Down Expand Up @@ -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
{
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<!-- content start -->
<div class="section" id="title">
<h1>Title</h1>

<p>Some paragraph.</p>
</div>

<!-- content end -->
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
app.WARNING: The code-block has no content. Did you properly indent the code?
6 changes: 6 additions & 0 deletions tests/Integration/tests/code/code-block-empty/input/index.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Title
=====

.. code-block:: xml

Some paragraph.
Original file line number Diff line number Diff line change
@@ -1,30 +1,29 @@
<!-- content start -->
<div class="section" id="directive-tests">
<h1>Directive tests</h1>
<h1>Directive tests</h1>

<div class="configuration-block">
<div role="tablist" aria-label="Configuration formats" class="configuration-tabs configuration-tabs-length-2">
<button role="tab" type="button" data-language="yaml"
aria-controls="configuration-block-tabpanel-523eafc9524d67db5f21ecae2362d532" aria-selected="true"
data-active="true" >
<span>Yaml</span>
</button>
<button role="tab" type="button" data-language="custom"
aria-controls="configuration-block-tabpanel-a51ba27b3c76153f629592baf23834dc" aria-selected="false"
tabindex="-1">
<span>CUSTOM</span>
</button>
<div class="configuration-block">
<div role="tablist" aria-label="Configuration formats" class="configuration-tabs configuration-tabs-length-2">
<button role="tab" type="button" data-language="yaml"
aria-controls="configuration-block-tabpanel-523eafc9524d67db5f21ecae2362d532" aria-selected="true"
data-active="true" >
<span>Yaml</span>
</button>
<button role="tab" type="button" data-language="custom"
aria-controls="configuration-block-tabpanel-a51ba27b3c76153f629592baf23834dc" aria-selected="false"
tabindex="-1">
<span>CUSTOM</span>
</button>
</div>

<div role="tabpanel" id="configuration-block-tabpanel-523eafc9524d67db5f21ecae2362d532" aria-label="Yaml" class="configuration-codeblock" data-language="yaml" style="">
<pre><code class="language-yaml"># app/config/services.yml</code></pre>
</div>
<pre><code class="language-yaml"># app/config/services.yml</code></pre>
</div>
<div role="tabpanel" id="configuration-block-tabpanel-a51ba27b3c76153f629592baf23834dc" aria-label="CUSTOM" class="configuration-codeblock" data-language="custom" style="display: none">
<pre><code class="language-php">// config/routes.php</code></pre>
</div>
<pre><code class="language-php">// config/routes.php</code></pre>
</div>

</div>

</div>

<!-- content end -->