Skip to content

rest2html: treat referenced wrapped images in base document as inlined#1935

Open
jdknight wants to merge 1 commit intogithub:masterfrom
jdknight:strip-suffix-on-referenced-imgs-at-doc-root
Open

rest2html: treat referenced wrapped images in base document as inlined#1935
jdknight wants to merge 1 commit intogithub:masterfrom
jdknight:strip-suffix-on-referenced-imgs-at-doc-root

Conversation

@jdknight
Copy link
Copy Markdown

@jdknight jdknight commented Mar 4, 2025

docutils will only add newlines around images it believes are inlined. For images held in references, it checks the parent of the reference if its a TextElement to consider it inlined. Since a document is not a TextElement type, it will wrap an image with newlines. For GitHub output, this is not desired and will result in the extra whitespace being rendered with a reference's decorative line. To avoid this, always strip any appended suffixes for images in this scenario.


There are a various GitHub projects which reveal the issue. For example, Sphinx's README.rst shows this issue:

With the changes made in this merge request, the following shows a rendering of HTML before and after the change:

@github-actions
Copy link
Copy Markdown

github-actions Bot commented Jul 2, 2025

This pull request has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@github-actions github-actions Bot added the Stale label Jul 2, 2025
@jdknight
Copy link
Copy Markdown
Author

jdknight commented Jul 3, 2025

!unstale

@github-actions github-actions Bot removed the Stale label Jul 3, 2025
@github-actions
Copy link
Copy Markdown

github-actions Bot commented Sep 1, 2025

This pull request has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@github-actions github-actions Bot added the Stale label Sep 1, 2025
@jdknight
Copy link
Copy Markdown
Author

jdknight commented Sep 1, 2025

!unstale

@github-actions github-actions Bot removed the Stale label Sep 2, 2025
@github-actions
Copy link
Copy Markdown

github-actions Bot commented Nov 2, 2025

This pull request has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@github-actions github-actions Bot added the Stale label Nov 2, 2025
@jdknight
Copy link
Copy Markdown
Author

jdknight commented Nov 2, 2025

!unstale

@github-actions github-actions Bot removed the Stale label Nov 3, 2025
@github-actions
Copy link
Copy Markdown

github-actions Bot commented Jan 2, 2026

This pull request has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@github-actions github-actions Bot added the Stale label Jan 2, 2026
@jdknight
Copy link
Copy Markdown
Author

jdknight commented Jan 2, 2026

!unstale

@github-actions github-actions Bot removed the Stale label Jan 3, 2026
@github-actions
Copy link
Copy Markdown

github-actions Bot commented Mar 4, 2026

This pull request has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.

@github-actions github-actions Bot added the Stale label Mar 4, 2026
@jdknight
Copy link
Copy Markdown
Author

jdknight commented Mar 5, 2026

!unstale

Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

This PR updates the ReST-to-HTML translation to avoid emitting trailing whitespace after an image when the image is wrapped in a reference that is a direct child of the document, preventing GitHub’s link decoration from rendering across the whitespace after the image.

Changes:

  • Strip the trailing newline emitted for a referenced top-level image so it behaves like an inline image in GitHub HTML output.
Show a summary per file
File Description
lib/github/commands/rest2html Adjusts depart_image output to remove trailing newline for document-level referenced images to avoid decorative underline/whitespace artifacts.

Copilot's findings

Tip

Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

  • Files reviewed: 1/1 changed files
  • Comments generated: 1

Comment on lines +261 to +266
# treat images held in a reference on the base document as inlined
# images; this is to help avoid rendering a reference's decorative
# line for the spacing after an image
if (isinstance(node.parent, nodes.reference)
and isinstance(node.parent.parent, nodes.document)):
self.body.append(self.body.pop().rstrip('\n'))
Copy link

Copilot AI Apr 22, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This change adjusts HTML whitespace semantics in a subtle way, but the existing markup tests normalize/strip blank text nodes via Nokogiri (noblanks), so they likely won’t fail if the newline/whitespace regresses. Consider adding a targeted regression test that asserts the raw HTML output for a top-level .. image:: with :target: does not contain whitespace between the <img> and the closing </a> (string-level assertion, not DOM-equality).

Copilot uses AI. Check for mistakes.
@zkoppert zkoppert self-assigned this Apr 22, 2026
docutils will only add newlines around images it believes are inlined.
For images held in references, it checks the parent of the reference if
its a `TextElement` to consider it inlined. Since a document is not a
`TextElement` type, it will wrap an image with newlines. For GitHub
output, this is not desired and will result in the extra whitespace
being rendered with a reference's decorative line. To avoid this,
always strip any appended suffixes for images in this scenario.

Signed-off-by: James Knight <git@jdknight.me>
@zkoppert zkoppert force-pushed the strip-suffix-on-referenced-imgs-at-doc-root branch from 600f67a to ee00347 Compare April 22, 2026 05:06
@zkoppert
Copy link
Copy Markdown
Member

Hey @jdknight — thanks for sticking with this PR, and sorry it took so long to get eyes on it.

I rebased your branch onto master to get CI running (it was 46 commits behind), and all checks pass across Ruby 3.2/3.3/3.4. The fix itself looks good — we reviewed it thoroughly and the logic is sound.

One thing we'd like to see before merging: a regression test. The existing test suite uses Nokogiri with noblanks, which normalizes whitespace-only text nodes — so the current tests wouldn't catch it if this behavior regressed. A string-level assertion (not DOM-equality) that verifies there's no trailing whitespace between the <img> and closing </a> for a top-level referenced image would do the trick.

Something like an RST fixture with a .. image:: directive using :target:, and an assertion on the raw HTML output before Nokogiri parsing. Happy to help brainstorm the approach if that would be useful.

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.

4 participants