|
| 1 | +import { defineConfig } from "astro/config"; |
| 2 | +import { fileURLToPath } from "node:url"; |
| 3 | +import path from "node:path"; |
| 4 | +import starlight from "@astrojs/starlight"; |
| 5 | +import react from "@astrojs/react"; |
| 6 | +import mdx from "@astrojs/mdx"; |
| 7 | +import { injectMdxImports } from "./src/remark/inject-mdx-imports.mjs"; |
| 8 | +import { rewriteCodeGroup } from "./src/remark/rewrite-code-group.mjs"; |
| 9 | +import { markdownSource } from "./src/integrations/markdown-source.mjs"; |
| 10 | +import { sidebarV1 } from "./src/sidebars/v1.mjs"; |
| 11 | +import { sidebarV2 } from "./src/sidebars/v2.mjs"; |
| 12 | +import { sidebarV3 } from "./src/sidebars/v3.mjs"; |
| 13 | + |
| 14 | +const here = path.dirname(fileURLToPath(import.meta.url)); |
| 15 | +const docsRoot = path.resolve(here, ".."); |
| 16 | +const snippetsDir = path.resolve(docsRoot, "snippets"); |
| 17 | +const mintlifyShim = path.resolve(here, "src/components/mintlify-compat.jsx"); |
| 18 | + |
| 19 | +// Names the shim re-exports (both its own components and re-exported |
| 20 | +// Starlight primitives). Every .mdx file gets these injected globally, |
| 21 | +// except when the file already imports a binding with the same name. |
| 22 | +// CodeGroup is intentionally NOT here — it's rewritten at the AST level |
| 23 | +// by the rewriteCodeGroup remark plugin, so no runtime component is needed. |
| 24 | +const GLOBAL_MDX_NAMES = [ |
| 25 | + "Card", |
| 26 | + "Warning", |
| 27 | + "Note", |
| 28 | + "Info", |
| 29 | + "Tip", |
| 30 | + "Columns", |
| 31 | + "Steps", |
| 32 | + "Step", |
| 33 | + "Frame", |
| 34 | + "ParamField", |
| 35 | + "Badge", |
| 36 | + "ClientSpecific", |
| 37 | + "VueSpecific", |
| 38 | + "Vue2Specific", |
| 39 | + "Vue3Specific", |
| 40 | + "ReactSpecific", |
| 41 | + "SvelteSpecific", |
| 42 | + "Svelte4Specific", |
| 43 | + "Svelte5Specific", |
| 44 | + "CurrentCodeTab", |
| 45 | +]; |
| 46 | + |
| 47 | +// Mirror of docusaurus/src/loaders/snippet-loader.js — snippet .jsx files |
| 48 | +// omit React hook imports and assume a browser `localStorage`. Inject both |
| 49 | +// so the originals work without modification. |
| 50 | +function snippetLoaderPlugin() { |
| 51 | + return { |
| 52 | + name: "inertia-snippet-loader", |
| 53 | + enforce: "pre", |
| 54 | + transform(code, id) { |
| 55 | + if (!id.includes(`${path.sep}snippets${path.sep}`)) return null; |
| 56 | + if (!/\.jsx?$/.test(id)) return null; |
| 57 | + const preamble = |
| 58 | + `import React, { useState, useCallback, useEffect } from "react";\n` + |
| 59 | + `const _ls = typeof window !== "undefined" ? window.localStorage : { getItem: () => null, setItem: () => {} };\n`; |
| 60 | + const safe = code.replace(/\blocalStorage\b/g, "_ls"); |
| 61 | + return { code: preamble + safe, map: null }; |
| 62 | + }, |
| 63 | + }; |
| 64 | +} |
| 65 | + |
| 66 | +export default defineConfig({ |
| 67 | + site: "https://inertiajs.com", |
| 68 | + // The root splash page lives in a separate repo; this build only owns |
| 69 | + // /docs/*. Redirect the bare /docs entry point to the latest version's |
| 70 | + // intro page so wandering links land somewhere useful. |
| 71 | + redirects: { |
| 72 | + "/docs": "/docs/v3/getting-started/", |
| 73 | + }, |
| 74 | + markdown: { |
| 75 | + remarkPlugins: [ |
| 76 | + // Must run BEFORE injectMdxImports: CodeGroup rewriting removes the |
| 77 | + // <CodeGroup> element entirely, so we don't need to inject an import |
| 78 | + // for a component that's already gone. |
| 79 | + rewriteCodeGroup, |
| 80 | + [ |
| 81 | + injectMdxImports, |
| 82 | + { |
| 83 | + imports: [{ source: mintlifyShim, names: GLOBAL_MDX_NAMES }], |
| 84 | + }, |
| 85 | + ], |
| 86 | + ], |
| 87 | + }, |
| 88 | + integrations: [ |
| 89 | + react(), |
| 90 | + starlight({ |
| 91 | + title: "Inertia.js Documentation", |
| 92 | + favicon: "/favicon.svg", |
| 93 | + head: [ |
| 94 | + // Pre-paint: read the saved framework and set it on <html> so |
| 95 | + // CSS attribute selectors (e.g. initial tab highlight) apply |
| 96 | + // before the main sync script runs, avoiding a framework flash. |
| 97 | + { |
| 98 | + tag: "script", |
| 99 | + content: |
| 100 | + "try{var v=localStorage.getItem('code');if(v)document.documentElement.dataset.framework=v.replace(/^\"|\"$/g,'');}catch(e){}", |
| 101 | + }, |
| 102 | + // Deferred sync script: wires up tab clicks + hides non-matching |
| 103 | + // framework wrappers. Module script ensures it's deferred and |
| 104 | + // runs once per navigation. |
| 105 | + { |
| 106 | + tag: "script", |
| 107 | + attrs: { type: "module", src: "/framework-switcher.js" }, |
| 108 | + }, |
| 109 | + ], |
| 110 | + expressiveCode: { |
| 111 | + // Paired dark/light themes — auto-switches with Starlight's theme toggle. |
| 112 | + themes: ["github-dark-default", "github-light-default"], |
| 113 | + styleOverrides: { |
| 114 | + borderRadius: "0.5rem", |
| 115 | + codeFontSize: "0.875rem", |
| 116 | + codePaddingBlock: "0.875rem", |
| 117 | + frames: { |
| 118 | + shadowColor: "transparent", |
| 119 | + }, |
| 120 | + }, |
| 121 | + shiki: { |
| 122 | + // The v2/v3 content uses ```cshtml fences; Shiki bundles razor |
| 123 | + // (which handles .cshtml files) but not the alias. |
| 124 | + langAlias: { cshtml: "razor" }, |
| 125 | + }, |
| 126 | + }, |
| 127 | + logo: { |
| 128 | + light: "./src/assets/logo-light.svg", |
| 129 | + dark: "./src/assets/logo-dark.svg", |
| 130 | + replacesTitle: true, |
| 131 | + }, |
| 132 | + social: [ |
| 133 | + { |
| 134 | + icon: "github", |
| 135 | + label: "GitHub", |
| 136 | + href: "https://github.com/inertiacore", |
| 137 | + }, |
| 138 | + ], |
| 139 | + components: { |
| 140 | + SiteTitle: "./src/components/SiteTitleWithVersion.astro", |
| 141 | + Sidebar: "./src/components/VersionedSidebar.astro", |
| 142 | + PageTitle: "./src/components/PageTitleWithMarkdownActions.astro", |
| 143 | + Search: "./src/components/VersionedSearch.astro", |
| 144 | + }, |
| 145 | + customCss: ["./src/styles/custom.css"], |
| 146 | + sidebar: [ |
| 147 | + { label: "v3.x", collapsed: true, items: sidebarV3 }, |
| 148 | + { label: "v2.x", collapsed: true, items: sidebarV2 }, |
| 149 | + { label: "v1.x", collapsed: true, items: sidebarV1 }, |
| 150 | + ], |
| 151 | + }), |
| 152 | + mdx(), |
| 153 | + markdownSource({ |
| 154 | + docsRoot, |
| 155 | + versions: [ |
| 156 | + { src: "v1", route: "docs/v1" }, |
| 157 | + { src: "v2", route: "docs/v2" }, |
| 158 | + { src: "v3", route: "docs/v3" }, |
| 159 | + ], |
| 160 | + }), |
| 161 | + ], |
| 162 | + vite: { |
| 163 | + plugins: [snippetLoaderPlugin()], |
| 164 | + resolve: { |
| 165 | + // Alias array form: exact-file regexes redirect the framework-specific |
| 166 | + // snippet files to the local shim (which outputs div + data-framework-only |
| 167 | + // attributes), while `/snippets` still maps to the shared dir for |
| 168 | + // everything else (v1-eol-warning.mdx, v2-upgrade-warning.mdx, etc.). |
| 169 | + alias: [ |
| 170 | + { |
| 171 | + find: /^\/snippets\/(vue|vue2|vue3|react|svelte|svelte4|svelte5|client)-specific\.jsx$/, |
| 172 | + replacement: mintlifyShim, |
| 173 | + }, |
| 174 | + { |
| 175 | + find: /^\/snippets\/current-code-tab\.jsx$/, |
| 176 | + replacement: mintlifyShim, |
| 177 | + }, |
| 178 | + { find: "/snippets", replacement: snippetsDir }, |
| 179 | + ], |
| 180 | + }, |
| 181 | + server: { fs: { allow: [docsRoot] } }, |
| 182 | + }, |
| 183 | +}); |
0 commit comments