Skip to content

Fix spurious whitespace after inline \Sexpr[results=rd] (e.g. \doi{})#2996

Open
ms609 wants to merge 1 commit into
r-lib:mainfrom
ms609:fix-2809-doi-whitespace
Open

Fix spurious whitespace after inline \Sexpr[results=rd] (e.g. \doi{})#2996
ms609 wants to merge 1 commit into
r-lib:mainfrom
ms609:fix-2809-doi-whitespace

Conversation

@ms609

@ms609 ms609 commented Jun 15, 2026

Copy link
Copy Markdown

Fixes #2809.

\doi{10.2307/2413326}. renders as doi:10.2307/2413326 ., with a space between the link and the trailing full stop.

This PR was constructed with AI assistance; I have manually reviewed the modifications.

Root cause

\doi{} is a system Rd macro that expands (via \Sexpr[results=rd]) to an \ifelse/\href fragment. pkgdown's as_html.tag_Sexpr() evaluates that \Sexpr and re-parses the result as an Rd fragment with rd_text():

rd = flatten_text(rd_text(as.character(res))),

tools::parse_Rd(..., fragment = TRUE) appends a trailing newline TEXT node to every fragment. So the re-parsed doi fragment flattens to <a ...>doi:...</a>\n, and the following . ends up on the next line:

<a href='https://doi.org/10.2307/2413326'>doi:10.2307/2413326</a>\n.\n

The interior \n renders as whitespace in HTML. R's own Rd2HTML doesn't hit this because it expands the macro in place during a single parse rather than re-parsing the macro output as a separate fragment.

Any inline \Sexpr[results=rd]{} followed by punctuation is affected (not just DOIs).

Fix

The fix strips the trailing newline that fragment re-parsing adds, in the rd branch of as_html.tag_Sexpr():

rd = sub("\n$", "", flatten_text(rd_text(as.character(res)))),

This removes the node added by parse_Rd() without touching leading whitespace or valid \Sexpr content.

Tests

  • Added a regression test in test-rd-html.R covering both \doi{...}. and a bare \Sexpr[results=rd]{...}..
  • Updated the "Sexprs in file share environment" test: two adjacent inline \Sexpr now correctly render flush ("11") instead of being split across two lines by the newline artefact.

NEWS

tools::parse_Rd() appends a trailing newline TEXT node when parsing a
fragment. as_html.tag_Sexpr() re-parses the result of a results=rd
\Sexpr as a fragment, so that newline rendered as whitespace between the
macro output and any following text. For inline macros such as \doi{},
this produced 'doi:... .' with a space before trailing punctuation.

Strip the single trailing newline that fragment re-parsing adds.
@ms609 ms609 marked this pull request as draft June 15, 2026 13:20
@ms609 ms609 marked this pull request as ready for review June 15, 2026 13:51
@ms609

ms609 commented Jun 15, 2026

Copy link
Copy Markdown
Author

Correct behaviour verified against real-world test cases.

CI failures seem to reflect unrelated stochastic tests rather than new errors caused by the fix?

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.

pkgdown inserts white space after \doi{..}

1 participant