diff --git a/packages/react/404.html b/packages/react/404.html new file mode 100644 index 0000000..7fd7dd9 --- /dev/null +++ b/packages/react/404.html @@ -0,0 +1,18 @@ + + + + + + + + + \ No newline at end of file diff --git a/packages/react/package.json b/packages/react/package.json index 6f4bcb9..a67ec43 100644 --- a/packages/react/package.json +++ b/packages/react/package.json @@ -5,7 +5,7 @@ "type": "module", "scripts": { "dev": "vite", - "build": "tsc && vite build", + "build": "tsc && vite build && cp ./404.html ./dist", "lint": "eslint . --ext ts,tsx --report-unused-disable-directives --max-warnings 0", "preview": "vite preview" }, diff --git a/packages/react/src/App.tsx b/packages/react/src/App.tsx index 85bcf70..1f2b3a6 100644 --- a/packages/react/src/App.tsx +++ b/packages/react/src/App.tsx @@ -59,7 +59,7 @@ function HashedHeaders(Level: `h${1 | 2 | 3 | 4 | 5 | 6}`) { root: null, rootMargin: "0px", threshold: buildThresholdList(), - } + }, ); if (internalRef.current) { observer.observe(internalRef.current); @@ -87,13 +87,24 @@ function HashedHeaders(Level: `h${1 | 2 | 3 | 4 | 5 | 6}`) { } const overrideComponents = { - pre: forwardRef((props, ref) => { - const { props: { children, className } } = React.cloneElement(React.Children.only(props.children)); + pre: forwardRef( + (props, ref) => { + const { + props: { children, className }, + } = React.cloneElement(React.Children.only(props.children)); - const language = (typeof className !== "string" || !className.includes("language-") ? "typescript" : /language-([a-z]+)/.exec(className)![1]) ?? "typescript" + const language = + (typeof className !== "string" || !className.includes("language-") + ? "typescript" + : /language-([a-z]+)/.exec(className)![1]) ?? "typescript"; - return {children as string}; - }), + return ( + + {children as string} + + ); + }, + ), code: forwardRef((props: any, ref) => ( { ); }); +const REDIRECTED_404 = /^\?(\/([a-zA-Z0-9\-_]+\/?)+)(&.*)*$/; + const router = createBrowserRouter( createRoutesFromElements( } errorElement={}> - } /> + + REDIRECTED_404.test(window.location.search) + ? redirect(REDIRECTED_404.exec(window.location.search)?.[1] ?? "/") + : true + } + element={} + /> }> redirect("/docs/introduction")} /> {routes} - - ) + , + ), ); function App() {