Skip to content

fix(code): skip Pygments linenos inline line-number spans (#526) - #537

Open
samiarif02 wants to merge 1 commit into
kepano:mainfrom
samiarif02:fix/pygments-linenos-code-block
Open

samiarif02 wants to merge 1 commit into
kepano:mainfrom
samiarif02:fix/pygments-linenos-code-block

Conversation

@samiarif02

Copy link
Copy Markdown

Problem

Sphinx / Read the Docs render Pygments :linenos: code blocks with the line numbers as inline spans directly in the code:

<span class="linenos">1</span><span class="k">def</span> ...
<span class="linenos">2</span>    ...

The code extractor skips span.lineno (singular — labml.ai's class, added in #223), but not linenos. So the number falls through to the generic line rule, span[class*="line"] matches linenos and appends a newline, and each line number ends up on its own line in the extracted code:

1
def some_function():
2
    interesting = False

Reproducible on the sphinx_rtd_theme demo page ("Emphasized lines with line numbers"). Reported in #526.

Fix

Skip span.linenos alongside span.lineno:

if (element.matches('span.lineno, span.linenos')) {
  return '';
}

Tests

Added tests/pygments-linenos.test.ts with a Sphinx :linenos: block; it fails on main (the digits 1/2/3 survive as their own lines) and passes with the fix. vitest run is green except the pre-existing website/* suites that need the @lezer/* packages and fail the same way on main.

Sphinx / Read the Docs render Pygments `:linenos:` as inline
<span class="linenos">N</span> spans. The code extractor only skipped
`span.lineno` (labml.ai's class), so `linenos` fell through to the
generic `span[class*="line"]` rule and each line number landed on its
own line in the extracted code.

Match `span.linenos` alongside `span.lineno`.

Fixes kepano#526

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant