Skip to content
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

[BUGFIX] Fix parsing and rendering of code-block with line numbers #685

Merged
merged 1 commit into from
Nov 18, 2023
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 @@ -70,15 +70,19 @@ public function process(
return $node;
}

/** @param mixed[] $options */
/** @param array<string, DirectiveOption> $options */
private function setStartingLineNumberBasedOnOptions(array $options, CodeNode $node): void
{
$startingLineNumber = null;
if (isset($options['linenos'])) {
if (isset($options['linenos']) || isset($options['number-lines'])) {
$startingLineNumber = 1;
}

$startingLineNumber = $options['number-lines'] ?? $options['lineno-start'] ?? $startingLineNumber;
if (isset($options['number-lines'])) {
$startingLineNumber = $options['number-lines']->getValue() ?? $startingLineNumber;
} elseif (isset($options['lineno-start'])) {
$startingLineNumber = $options['lineno-start']->getValue() ?? $startingLineNumber;
}

if ($startingLineNumber === null) {
return;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@
<span class="caption-text">{{ node.caption }}</span>
</div>
{%- endif -%}
<pre{% if node.classes %} class="{{ node.classesString }}"{% endif %}><code class="language-{{ node.language }}{{ node.startingLineNumber ? ' line-numbers' }}" {{- node.startingLineNumber ? ' data-start=' ~ node.startingLineNumber }}>{{ node.value }}</code></pre>
<pre{% if node.classes %} class="{{ node.classesString }}"{% endif %}><code class="language-{{ node.language }}{{ node.startingLineNumber ? ' line-numbers' }}" {%- if node.startingLineNumber %} data-start="{{ node.startingLineNumber }}"{% endif %}>{{ node.value }}</code></pre>
{%- endif -%}
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<div class="section" id="index">
<h1>index</h1>

<pre><code class="language-php line-numbers" data-start=2>&lt;?php
<pre><code class="language-php line-numbers" data-start="2">&lt;?php

declare(strict_types=1);

Expand Down
15 changes: 15 additions & 0 deletions tests/Integration/tests/code/code-block-lineno/expected/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
<!DOCTYPE html>
<html lang="en">
<head>
<title>Title</title>

</head>
<body>
<div class="section" id="title">
<h1>Title</h1>

<pre><code class="language-ruby line-numbers" data-start="10">Some more Ruby code, with line numbering starting at 10.</code></pre>
</div>

</body>
</html>
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Title
=====

.. code-block:: ruby
:lineno-start: 10

Some more Ruby code, with line numbering starting at 10.