Skip to content

Double escaped & in HTML link query params with Sphinx and MyST #1028

Open
@palotasb

Description

@palotasb

What version of myst-parser are you using?

4.0.1

What version dependencies are you using?

The latest as of today.

Package                       Version
----------------------------- ---------
alabaster                     1.0.0
babel                         2.17.0
certifi                       2025.1.31
charset-normalizer            3.4.1
docutils                      0.21.2
idna                          3.10
imagesize                     1.4.1
jinja2                        3.1.5
markdown-it-py                3.0.0
markupsafe                    3.0.2
mdit-py-plugins               0.4.2
mdurl                         0.1.2
myst-parser                   4.0.1
packaging                     24.2
pygments                      2.19.1
pyyaml                        6.0.2
requests                      2.32.3
roman-numerals-py             3.1.0
snowballstemmer               2.2.0
sphinx                        8.2.1
sphinxcontrib-applehelp       2.0.0
sphinxcontrib-devhelp         2.0.0
sphinxcontrib-htmlhelp        2.1.0
sphinxcontrib-jsmath          1.0.1
sphinxcontrib-qthelp          2.0.0
sphinxcontrib-serializinghtml 2.0.0
urllib3                       2.3.0

What operating system are you using?

Mac

Describe the Bug

Creating a link such as the following:

[Test link.](https://example.org/?a=1&b=2)

Produces the following incorrect HTML.

<p><a class="reference external" href="https://example.org/?a=1&amp;amp;b=2">Test link.</a></p>

Note how the & separating the parameters is double-escaped into &amp;amp;.

Expected Behavior

The same output should produce the following HTML.

<p><a class="reference external" href="https://example.org/?a=1&amp;b=2">Test link.</a></p>

To Reproduce

This is a possible duplicate of #760 which is labeled as more-info-required. A possible fix (unverified by me) is at #929.


To reproduce the issue:

Create the following files in an empty directory.

pyproject.toml:

[project]
name = "test-sphinx-amp-mcve"
version = "1"

dependencies = [
    "sphinx",
    "myst-parser",
]

docs/conf.py:

extensions = ["myst_parser"]

docs/index.md:

# Test page

[Test link.](https://example.org/?a=1&b=2)

Install dependencies via uv or any other package manager:

uv venv
. ./venv/bin/activate
uv sync --upgrade --exclude-newer=2025-02-25T14:50:00+01:00

(The last parameter ensures you get the exact same packages as me right now. Omit it to reproduce with newer dependency version in the future.)

Build the docs using Sphinx:

sphinx-build "docs" "docs/build"

Observe the output.

$ grep '1&amp;' --color=always docs/build/index.html
<p><a class="reference external" href="https://example.org/?a=1&amp;amp;b=2">Test link.</a></p>

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions