Files
hyperframes/docs/snippets/install-command.jsx
T
Michael WangandClaude Opus 5.5 24508ce2da fix: harden docs snippets, SVG sanitizer, lint regexes, GSAP writers and registry pins (#7512)
* fix(docs): escape catalog preview values and the install command's --vars argument

The catalog preview embeds variable values as JSON that cannot close a
script element or attribute, accepts value messages only from the page
that embeds it, and reads only declared ids with primitive values from
the link. The install command shell-quotes its --vars JSON and keeps
only variable-id keys with primitive values.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(core): fold CSS line endings before decoding escapes in the SVG sanitizer

A browser folds CR LF, CR and form feed into one newline before it reads
CSS escapes, so the whitespace after a hex escape can be a CR LF pair.
The sanitizer now does the same, so an escaped function name split by a
line ending is checked under its real name. The media-use bundle is
regenerated from the source.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(lint): match template ids and CSS rules in linear time

A getElementById template literal is matched against document ids with
an in-order indexOf scan instead of a regex of joined wildcards, so the
time no longer grows with the number of placeholders. The CSS rule
scanners only start a match at the beginning of a brace-free run, which
finds the same rules without rescanning long brace-less text. Lint
results on every HTML file in the repository are unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(parsers): keep GSAP writer output inside its script element

String literals and quoted keys the GSAP writers emit now encode a
script closer or comment opener as \u003c, so a value can no longer end
the inline script that holds the timeline. Strings without those
sequences are written exactly as before. The writers also refuse a tween
method outside set, to, from and fromTo, and a motion path whose
coordinates or curviness are not numbers, since both are emitted as code.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(scripts): confine registry asset hosting to files inside the item

The asset hosting script now refuses a manifest path that leaves the
item directory or reaches its file through a symlink, before anything is
read, staged, uploaded or removed. A repository test fails when the
registry tracks a symlink or a manifest path that leaves its item.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(registry): pin the map blocks' d3 and atlas files to exact versions

The five map blocks load d3 7.9.2, the build the d3@7 range serves today,
with a subresource integrity hash, and fetch us-atlas 3.0.1 and
world-atlas 2.0.2, the files their ranges serve today. The bytes are
unchanged, so the blocks render the same. A repository test fails when a
registry item names an npm CDN package by range, tag or no version.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(registry): pin map blocks to d3 7.9.0

7.9.0 has been the stable release since 2024; 7.9.1 and 7.9.2 were published days ago. Pin the
long-standing version, with its subresource integrity hash.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(registry): keep the map block PR source-only and formatted

Restore the generated catalog pages and payloads, which the catalog
publish job regenerates from the item sources. Name the us-atlas URL
in us-map-flow so the call fits on one line and formatting is unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* refactor(scripts): check registry asset containment with one prefix test

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* fix(lint): scan simple CSS rules in linear time

The two CSS-rule scans in the gsap rules took a body that could cross
'{', so a closed style of many '{' with no '}' rescanned to its end from
every brace. A scanner now returns the same selector and body pairs and
remembers the next '}', so each scan is linear. Findings are identical
on every tracked HTML file.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

* test(core): label the form-feed sanitizer case as a guard

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

---------

Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-10 20:01:29 +00:00

166 lines
6.5 KiB
React

/**
* The install command, with a copy button that is always visible.
*
* A plain code fence renders a copy control only on hover, so it is invisible
* to anyone who has not already guessed it is there, and absent from the
* accessibility tree entirely. This is the one line on a catalog page that
* every reader is here to take, so the affordance is spelled out.
*
* The button is icon-only, and both icons are Mintlify's own: the same
* clipboard and check paths, at the same 16px, that the sitewide code-block
* copy button draws. Two copy affordances sit on a catalog page, and they
* should read as one control used twice. Sizing, hover plate, focus ring and
* the brand-coloured check live in `custom.css` next to the rules that style
* that sitewide button, so the pair cannot drift apart.
*
* The "Copied" announcement is a sibling `role="status"`, not a swapped
* `aria-label`: it is the pattern Mintlify already uses on the code-block
* button, so a screen reader hears the same word from both controls, and it
* does not rename a control while it holds focus.
*
* navigator.clipboard is unavailable on insecure origins, which is exactly the
* local `mint dev` preview these pages are written against. The textarea path
* below is the fallback, not decoration.
*/
export const InstallCommand = ({ command, item }) => {
const [copied, setCopied] = React.useState(false);
/**
* The values a reader tuned, read from the same query string the variables
* panel writes.
*
* The URL is the shared state rather than a prop, because this line and the
* panel are separate components mounted by MDX with no parent between them.
* It also means a shared link and this command agree without either knowing
* the other exists.
*
* Read after mount: the first render happens on the server, where there is
* no query string to read.
*/
const [tuned, setTuned] = React.useState("");
React.useEffect(() => {
if (!item) return;
// >>> vars argument
/**
* The ` --vars '<json>'` suffix for a query value, or "" when nothing usable is in it.
*
* Keeps variable-id keys with string, finite number or boolean values, and
* escapes `'` so the JSON stays one shell argument.
*/
function varsArgument(raw) {
if (!raw) return "";
const parsed = JSON.parse(raw);
if (!parsed || typeof parsed !== "object" || Array.isArray(parsed)) return "";
const kept = Object.entries(parsed).filter(([id, value]) => {
if (!/^[A-Za-z_][\w-]*$/.test(id)) return false;
if (typeof value === "number") return Number.isFinite(value);
return typeof value === "string" || typeof value === "boolean";
});
if (kept.length === 0) return "";
return ` --vars '${JSON.stringify(Object.fromEntries(kept)).replace(/'/g, "'\\''")}'`;
}
// <<< vars argument
const read = () => {
try {
setTuned(varsArgument(new URLSearchParams(window.location.search).get(`vars-${item}`)));
} catch {
// A hand-edited link should leave the plain command, not break it.
setTuned("");
}
};
read();
// `replaceState` fires nothing, so the panel says so itself; `popstate`
// covers the back button.
window.addEventListener("hf-vars-changed", read);
window.addEventListener("popstate", read);
return () => {
window.removeEventListener("hf-vars-changed", read);
window.removeEventListener("popstate", read);
};
}, [item]);
const fullCommand = `${command}${tuned}`;
const copy = async () => {
try {
if (navigator.clipboard && window.isSecureContext) {
await navigator.clipboard.writeText(fullCommand);
} else {
// The scratch textarea has to take focus to be selected, and removing
// it drops focus on <body> — so a reader who copied with the keyboard
// would Tab from the top of the page again. Hand focus back.
const previous = document.activeElement;
const scratch = document.createElement("textarea");
scratch.value = fullCommand;
scratch.setAttribute("readonly", "");
scratch.style.position = "fixed";
scratch.style.opacity = "0";
document.body.appendChild(scratch);
scratch.select();
document.execCommand("copy");
document.body.removeChild(scratch);
previous?.focus?.();
}
setCopied(true);
setTimeout(() => setCopied(false), 2000);
} catch {
// Leave the command on screen and selectable. Reporting a failure the
// reader cannot act on is worse than letting them select it by hand.
}
};
return (
<div className="hf-install-command not-prose my-4 flex items-stretch overflow-hidden rounded-xl border border-zinc-200 bg-zinc-50 dark:border-zinc-800 dark:bg-zinc-900">
<code className="flex-1 overflow-x-auto whitespace-nowrap border-r border-zinc-200 px-4 py-3 font-mono text-sm text-zinc-800 dark:border-zinc-800 dark:text-zinc-100">
{fullCommand}
</code>
<button
type="button"
onClick={copy}
data-copied={copied ? "true" : "false"}
aria-label={`Copy ${command} to the clipboard`}
className="hf-install-copy"
>
<svg
className="hf-install-copy-clipboard"
xmlns="http://www.w3.org/2000/svg"
width="16"
height="16"
viewBox="0 0 18 18"
fill="none"
stroke="currentColor"
strokeWidth="1.5"
strokeLinecap="round"
strokeLinejoin="round"
aria-hidden="true"
>
<path d="M14.25 5.25H7.25C6.14543 5.25 5.25 6.14543 5.25 7.25V14.25C5.25 15.3546 6.14543 16.25 7.25 16.25H14.25C15.3546 16.25 16.25 15.3546 16.25 14.25V7.25C16.25 6.14543 15.3546 5.25 14.25 5.25Z" />
<path d="M2.80103 11.998L1.77203 5.07397C1.61003 3.98097 2.36403 2.96397 3.45603 2.80197L10.38 1.77297C11.313 1.63397 12.19 2.16297 12.528 3.00097" />
</svg>
<svg
className="hf-install-copy-check"
xmlns="http://www.w3.org/2000/svg"
width="16"
height="16"
viewBox="0 0 18 18"
fill="none"
stroke="currentColor"
strokeWidth="2"
strokeLinecap="round"
strokeLinejoin="round"
aria-hidden="true"
>
<path d="M2.75 9.5L6.5 13.25L15.25 4.5" />
</svg>
</button>
<span className="hf-install-copy-status" role="status" aria-live="polite">
{copied ? "Copied" : ""}
</span>
</div>
);
};