Skip to content

fix(kit): escape curly braces in markdown link & image URLs - #812

Merged
coyotte508 merged 1 commit into
huggingface:mainfrom
coyotte508:fix/escape-braces-in-link-urls
Aug 4, 2026
Merged

coyotte508 merged 1 commit into
huggingface:mainfrom
coyotte508:fix/escape-braces-in-link-urls

Conversation

@coyotte508

Copy link
Copy Markdown
Member

Problem

A markdown link whose URL contains curly braces — e.g. OpenAPI-style path params:

[overview endpoint](https://huggingface-openapi.hf.space/#tag/users/GET/api/users/{username}/overview)

fails the whole HTML doc build (doc-builder build ... --html) at SvelteKit prerender time with:

ReferenceError: username is not defined

Hit in practice by a hub-docs PR linking to an OpenAPI endpoint with a {username} path parameter.

Root cause

In kit/preprocessors/mdsvex/index.js, the treeVisitor remark plugin escapes { → { in text nodes (onText) and in raw-HTML text children (onHtml) so Svelte doesn't interpret braces as expression tags. But a link's URL is stored on the mdast link/image node's url property, not in a child text node — so it is never escaped. mdsvex emits the raw braces into the generated href/src attribute, Svelte 5 compiles {username} into ${$.stringify(username)} in the SSR output, and prerendering (adapter-static) throws.

Fix

Escape { as { in link/image url and title inside the doc body, mirroring onText. The rendered output is byte-identical from the browser's perspective ({ resolves back to { in the attribute value), and the compiled component becomes a static string again.

Note on the doc-body check: isWithinDocBody()'s module-level state machine only works for visitor passes that run before the HF_DOC_BODY_END text node has been visited — the text pass flips the state off at the end of the document, so any later pass (like a new link pass) would see "outside body" for every node. Instead, the marker offsets are located up front and node positions compared against them.

Verified

Ran the actual mdsvexPreprocess + svelte.compile(generate: "server") pipeline on a doc page containing the failing link:

  • before: compiled JS contains ${$.stringify(username)} → ReferenceError at prerender
  • after: compiled JS is a static string; final markup renders href="...api/users/{username}/overview" verbatim
  • links outside the doc body and pages without markers are untouched
  • prettier --check passes

A link's URL lives on the mdast node's `url` property, not in a child
text node, so the existing `onText` visitor never escapes curly braces
inside it. mdsvex then emits the raw braces into the `href`/`src`
attribute of the generated Svelte component, where Svelte 5 interprets
`{...}` as an expression. During prerendering this throws
`ReferenceError: <name> is not defined` and fails the whole doc build —
e.g. for URLs containing OpenAPI path params like:

    [overview endpoint](https://huggingface-openapi.hf.space/#tag/users/GET/api/users/{username}/overview)

Escape `{` as `&huggingface#123;` in link/image `url` and `title` inside the doc
body, mirroring what `onText` does for text nodes. The doc-body check
uses marker offsets rather than the stateful `isWithinDocBody()`,
because that state machine only works for visitors that run before the
END marker's text node is visited (link/image passes run after the text
pass has already flipped the state off).
coyotte508 added a commit to huggingface/hub-docs that referenced this pull request Aug 4, 2026
doc-builder's mdsvex preprocessor doesn't escape curly braces inside
link URLs (only in text nodes), so Svelte 5 treats {username} in the
href as an expression and prerendering fails with
'ReferenceError: username is not defined'.

Upstream fix: huggingface/doc-builder#812
@coyotte508

Copy link
Copy Markdown
Member Author

Ref: #812

@coyotte508
coyotte508 merged commit 9a49534 into huggingface:main Aug 4, 2026
5 checks passed
coyotte508 added a commit to huggingface/hub-docs that referenced this pull request Aug 4, 2026
* Document API access to user hardware items

The user overview endpoint now returns a hardwareItems array, and
/oauth/userinfo includes a hardwareItems claim with the profile scope,
for users whose hardware is public.

* Apply suggestion from @coyotte508

* Percent-encode {username} in OpenAPI link to unbreak doc build

doc-builder's mdsvex preprocessor doesn't escape curly braces inside
link URLs (only in text nodes), so Svelte 5 treats {username} in the
href as an expression and prerendering fails with
'ReferenceError: username is not defined'.

Upstream fix: huggingface/doc-builder#812

* Apply suggestion from @coyotte508

---------

Co-authored-by: huggingface-doc-bot <doc-bot@huggingface.co>
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.

1 participant