Markdown image alt text is HTML-escaped twice (`'` renders as `'`)
Have you read the Contributing Guidelines on issues?
- I have read the Contributing Guidelines on issues.
Prerequisites
- I'm using the latest version of Docusaurus.
- I have tried the
npm run clearoryarn clearcommand. - I have tried
rm -rf node_modules yarn.lock package-lock.jsonand re-installing packages. - I have tried creating a repro with https://new.docusaurus.io.
- I have read the console error message carefully (if applicable).
Description
The alt text of a local Markdown image is HTML-escaped twice, so apostrophes, quotes and ampersands reach the rendered page as literal entity text. Screen readers then read them out, for example "organization ampersand hash 39 s".
 renders as:
<img alt="An organization&#39;s repository list" ...>so the DOM alt value is An organization's repository list.
The cause is packages/docusaurus-mdx-loader/src/remark/transformImage/index.ts, which still runs escapeHtml() on alt and title before storing them as mdxJsxAttribute values:
if (node.alt) {
attributes.push({
type: 'mdxJsxAttribute',
name: 'alt',
value: escapeHtml(node.alt),
});
}That escape was needed in v2, when this plugin emitted a JSX string (see #3751: the build failed on " in alt text). Since the MDX v2 upgrade (#8288) it builds a JSX AST node instead, and attribute values are escaped again when rendered. The first escape is now redundant, so the text is escaped twice. The compiled page module contains alt:"An organization's repository list".
transformLinks does the same with the title of links to local assets (escapeHtml(node.title)).
A possible fix is to drop the escapeHtml() calls for alt and title in transformImage, and for title in transformLinks, and to add a test with ', " and & in alt text.
Reproducible demo
No response
Steps to reproduce
- Create a site:
npx create-docusaurus@latest my-site classic. - In
docs/intro.md, add: - Run
npm run build(ornpm start) and inspect the image.
Expected behavior
The image's alt attribute is The team's "logo" & mascot.
Actual behavior
The alt attribute is The team's "logo" & mascot. The built HTML contains alt="The team&#39;s &quot;logo&quot; &amp; mascot".
Your environment
- Docusaurus version used: 3.10.2 (
@docusaurus/mdx-loader3.10.2), with@docusaurus/faster - Environment name and version: Node.js 26.8.2
- Operating system and version: Linux (GitHub Codespaces)
Self-service
- I'd be willing to fix this bug myself.
Source: facebook/docusaurus