From 4610a00c69643b7d2c60e5b19d3387bf9f411af4 Mon Sep 17 00:00:00 2001 From: James Knight Date: Mon, 3 Mar 2025 21:27:58 -0500 Subject: [PATCH 1/2] rest2html: treat referenced wrapped images in base document as inlined 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 --- lib/github/commands/rest2html | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/lib/github/commands/rest2html b/lib/github/commands/rest2html index c6fc663e..8cc1f161 100755 --- a/lib/github/commands/rest2html +++ b/lib/github/commands/rest2html @@ -258,6 +258,12 @@ class GitHubHTMLTranslator(HTMLTranslator): self.body.append(self.starttag(node, 'img', **atts)) HTMLTranslator.depart_image(self, node) + # 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')) def kbd(name, rawtext, text, lineno, inliner, options={}, content=[]): return [nodes.raw('', '%s' % text, format='html')], [] From 2d7048a664d525b38d3c2966b6cc8aa12684e715 Mon Sep 17 00:00:00 2001 From: James Knight Date: Sat, 13 Jun 2026 10:28:37 -0400 Subject: [PATCH 2/2] test: validate inlined rst references do not add whitespaces Verifying that inlined references generated by reStructuredText/docutil do no generate whitespaces between the img tag and closing a tag. Signed-off-by: James Knight --- test/markup_test.rb | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/test/markup_test.rb b/test/markup_test.rb index baa1dec2..6ab95662 100644 --- a/test/markup_test.rb +++ b/test/markup_test.rb @@ -137,4 +137,16 @@ def test_commonmarker_options assert_equal "<style>.red{color: red;}</style>\n", GitHub::Markup.render_s(GitHub::Markups::MARKUP_MARKDOWN, "", options: {commonmarker_opts: [:UNSAFE]}) assert_equal "\n", GitHub::Markup.render_s(GitHub::Markups::MARKUP_MARKDOWN, "", options: {commonmarker_opts: [:UNSAFE], commonmarker_exts: [:autolink, :table, :strikethrough]}) end + + def test_blanks_rst_inlined_reference + expected = '' + + actual = GitHub::Markup.render_s(GitHub::Markups::MARKUP_RST, <<~RST + .. image:: https://example.com/img.svg + :target: https://example.com/ + RST + ) + + assert_equal expected, actual.strip.lines.last + end end