Skip to content

Commit 8d9e8b2

Browse files
committed
refactor(starlight): use Starlight <Aside> for Mintlify admonitions
The React shims in mintlify-compat.jsx rebuilt the Starlight aside markup by hand and silently dropped the <svg> inside the icon span, so every Warning/Note/Info/Tip rendered with an empty icon slot. Replace the hand-rolled components with an AST rewrite (same pattern as rewriteCodeGroup): rewriteAsides maps <Warning>/<Note>/<Info>/<Tip> to <Aside type="caution|note|note|tip"> before injectMdxImports runs. The shim now re-exports Aside directly from @astrojs/starlight/components, and GLOBAL_MDX_NAMES drops the rewritten names in favor of Aside. Icons, ARIA semantics, and future Starlight aside updates come along for free.
1 parent 7712dbb commit 8d9e8b2

3 files changed

Lines changed: 58 additions & 33 deletions

File tree

starlight/astro.config.mjs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import react from "@astrojs/react";
66
import mdx from "@astrojs/mdx";
77
import { injectMdxImports } from "./src/remark/inject-mdx-imports.mjs";
88
import { rewriteCodeGroup } from "./src/remark/rewrite-code-group.mjs";
9+
import { rewriteAsides } from "./src/remark/rewrite-asides.mjs";
910
import { sidebarV1 } from "./src/sidebars/v1.mjs";
1011
import { sidebarV2 } from "./src/sidebars/v2.mjs";
1112
import { sidebarV3 } from "./src/sidebars/v3.mjs";
@@ -18,14 +19,13 @@ const mintlifyShim = path.resolve(here, "src/components/mintlify-compat.jsx");
1819
// Names the shim re-exports (both its own components and re-exported
1920
// Starlight primitives). Every .mdx file gets these injected globally,
2021
// except when the file already imports a binding with the same name.
21-
// CodeGroup is intentionally NOT here — it's rewritten at the AST level
22-
// by the rewriteCodeGroup remark plugin, so no runtime component is needed.
22+
//
23+
// Intentionally NOT in this list:
24+
// - CodeGroup → rewritten to a div tree by rewriteCodeGroup
25+
// - Warning/Note/Info/Tip → rewritten to <Aside type="..."> by rewriteAsides
2326
const GLOBAL_MDX_NAMES = [
2427
"Card",
25-
"Warning",
26-
"Note",
27-
"Info",
28-
"Tip",
28+
"Aside",
2929
"Columns",
3030
"Steps",
3131
"Step",
@@ -72,10 +72,11 @@ export default defineConfig({
7272
},
7373
markdown: {
7474
remarkPlugins: [
75-
// Must run BEFORE injectMdxImports: CodeGroup rewriting removes the
76-
// <CodeGroup> element entirely, so we don't need to inject an import
77-
// for a component that's already gone.
75+
// Element rewrites must run BEFORE injectMdxImports — they replace
76+
// the original tag names so the import injector knows it doesn't
77+
// need to provide bindings for the originals.
7878
rewriteCodeGroup,
79+
rewriteAsides,
7980
[
8081
injectMdxImports,
8182
{

starlight/src/components/mintlify-compat.jsx

Lines changed: 10 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,15 @@
11
import React from "react";
22

3-
// Re-export Starlight's Card so content files outside the project root
4-
// (symlinked v1/v2/v3 trees) can reach it via this shim's absolute path.
5-
// Auto-import maps <Card> → this re-export.
6-
export { Card } from "@astrojs/starlight/components";
7-
8-
// --- Admonition wrappers -> Starlight aside CSS classes ---
9-
10-
const Aside = ({ type, children }) => (
11-
<aside
12-
aria-label={type}
13-
className={`starlight-aside starlight-aside--${type}`}
14-
>
15-
<p className="starlight-aside__title">
16-
<span className="starlight-aside__icon" aria-hidden="true" />
17-
{type[0].toUpperCase() + type.slice(1)}
18-
</p>
19-
<section className="starlight-aside__content">{children}</section>
20-
</aside>
21-
);
22-
23-
export const Warning = ({ children }) => <Aside type="caution">{children}</Aside>;
24-
export const Note = ({ children }) => <Aside type="note">{children}</Aside>;
25-
export const Info = ({ children }) => <Aside type="note">{children}</Aside>;
26-
export const Tip = ({ children }) => <Aside type="tip">{children}</Aside>;
3+
// Re-export Starlight components from this shim so content files outside
4+
// the project root (symlinked v1/v2/v3 trees) can reach them via the
5+
// shim's absolute path — Vite can't resolve `@astrojs/starlight/...`
6+
// from /docs/v* MDX files because they live outside the project's
7+
// `node_modules` lookup chain.
8+
//
9+
// `Aside` is referenced via the rewriteAsides remark plugin which maps
10+
// <Warning>/<Note>/<Info>/<Tip> → <Aside type="...">. `Card` is
11+
// referenced directly by content files.
12+
export { Aside, Card } from "@astrojs/starlight/components";
2713

2814
// --- Framework filter wrappers ---
2915
// Emit a wrapper <div> with `data-framework-only` listing every framework
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import { visit } from "unist-util-visit";
2+
3+
/**
4+
* Remark plugin that rewrites Mintlify-style admonition tags into
5+
* Starlight's native <Aside> component. The original wrappers were
6+
* React shims that re-built the aside markup by hand and silently
7+
* dropped Starlight's icon SVGs — using <Aside> directly gets the
8+
* icons, ARIA semantics, and any future styling for free.
9+
*
10+
* Mappings:
11+
* <Warning> → <Aside type="caution">
12+
* <Note> → <Aside type="note">
13+
* <Info> → <Aside type="note"> // Mintlify "info" ≈ Starlight "note"
14+
* <Tip> → <Aside type="tip">
15+
*
16+
* Children pass through unchanged. The rewrite runs before
17+
* injectMdxImports so the resulting <Aside> tag picks up its global
18+
* import from the shim re-export.
19+
*/
20+
const ASIDE_TYPES = {
21+
Warning: "caution",
22+
Note: "note",
23+
Info: "note",
24+
Tip: "tip",
25+
};
26+
27+
export function rewriteAsides() {
28+
return function transformer(tree) {
29+
visit(tree, "mdxJsxFlowElement", (node) => {
30+
const type = ASIDE_TYPES[node.name];
31+
if (!type) return;
32+
node.name = "Aside";
33+
node.attributes = [
34+
{ type: "mdxJsxAttribute", name: "type", value: type },
35+
];
36+
});
37+
};
38+
}

0 commit comments

Comments
 (0)