### Initialization Timing Example Source: https://github.com/mquintal/react-router-mdx/blob/master/_autodocs/configuration.md Example illustrating the correct timing for calling init() and routes() functions. ```typescript // react-router.config.ts import { init, routes } from 'react-router-mdx/server' const mdx = init({ path: 'posts' }) export default { ssr: true, async prerender() { return ["/", ...(await mdx.paths())] } } // app/routes.tsx import { routes } from 'react-router-mdx/server' export default [ index("routes/home.tsx"), ...routes("./routes/post.tsx") ] ``` -------------------------------- ### Multiple Paths Configuration Example Source: https://github.com/mquintal/react-router-mdx/blob/master/_autodocs/configuration.md Example demonstrating initialization with multiple paths and aliases, and their file mapping. ```typescript // react-router.config.ts const mdx = init({ paths: ['posts', 'articles'], aliases: ['blog', 'docs'] }) export default { ssr: true, async prerender() { return ["/", ...(await mdx.paths())] } } ``` -------------------------------- ### MDX file example Source: https://github.com/mquintal/react-router-mdx/blob/master/README.md Example of a basic MDX file with frontmatter and content. ```mdx --- title: hello world title --- # hello world This is a hello World mdx file ``` -------------------------------- ### Single Path Configuration Example Source: https://github.com/mquintal/react-router-mdx/blob/master/_autodocs/configuration.md Example demonstrating how to initialize react-router-mdx with a single path and use it for prerendering. ```typescript // react-router.config.ts import { init } from 'react-router-mdx/server' const mdx = init({ path: 'posts' }) export default { ssr: true, async prerender() { return ["/", ...(await mdx.paths())] } } ``` -------------------------------- ### init() example - Single path Source: https://github.com/mquintal/react-router-mdx/blob/master/_autodocs/api-reference/server.md Example demonstrating how to initialize react-router-mdx with a single path and retrieve discovered paths. ```typescript import { init } from 'react-router-mdx/server' const mdx = init({ path: 'posts' }) const paths = await mdx.paths() // Returns: ['posts/hello-world', 'posts/my-first-post'] ``` -------------------------------- ### init() example - Multiple paths with aliases Source: https://github.com/mquintal/react-router-mdx/blob/master/_autodocs/api-reference/server.md Example demonstrating initialization with multiple paths and aliases, and retrieving discovered paths. ```typescript import { init } from 'react-router-mdx/server' const mdx = init({ paths: ['posts', 'articles'], aliases: ['blog', 'docs'] }) const paths = await mdx.paths() // Returns: ['blog/hello-world', 'docs/getting-started'] ``` -------------------------------- ### Single Path Configuration with Alias Example Source: https://github.com/mquintal/react-router-mdx/blob/master/_autodocs/configuration.md Example showing how to configure a custom URL path prefix (alias) for MDX files. ```typescript const mdx = init({ path: 'content/blog-posts', alias: 'blog' }) ``` -------------------------------- ### Install with yarn Source: https://github.com/mquintal/react-router-mdx/blob/master/README.md Installs the react-router-mdx package using yarn. ```sh yarn add react-router-mdx ``` -------------------------------- ### Install with npm Source: https://github.com/mquintal/react-router-mdx/blob/master/README.md Installs the react-router-mdx package using npm. ```sh npm install react-router-mdx ``` -------------------------------- ### routes() example Source: https://github.com/mquintal/react-router-mdx/blob/master/_autodocs/api-reference/server.md Example demonstrating the usage of the routes function in a React Router configuration file. ```typescript // react-router.config.ts import { routes } from 'react-router-mdx/server' export default [ index("routes/home.tsx"), ...routes("./routes/post.tsx") ] ``` -------------------------------- ### Multiple Plugins Source: https://github.com/mquintal/react-router-mdx/blob/master/_autodocs/configuration.md Example demonstrating how to use multiple remark and rehype plugins simultaneously. ```typescript import { loadMdx } from 'react-router-mdx/server' import remarkGfm from 'remark-gfm' import remarkMath from 'remark-math' import rehypeHighlight from 'rehype-highlight' import rehypeSlug from 'rehype-slug' import rehypeToc from 'rehype-toc' export async function loader({ request }) { return await loadMdx(request, { remarkPlugins: [remarkGfm, remarkMath], rehypePlugins: [rehypeHighlight, rehypeSlug, rehypeToc] }) } ``` -------------------------------- ### Syntax Highlighting Source: https://github.com/mquintal/react-router-mdx/blob/master/_autodocs/configuration.md Example of enabling syntax highlighting using rehypeHighlight. ```typescript import { loadMdx } from 'react-router-mdx/server' import rehypeHighlight from 'rehype-highlight' import 'highlight.js/styles/atom-one-dark.css' export async function loader({ request }) { return await loadMdx(request, { rehypePlugins: [rehypeHighlight] }) } ``` -------------------------------- ### MdxAttributes Example Source: https://github.com/mquintal/react-router-mdx/blob/master/_autodocs/types.md Example demonstrating the structure of MdxAttributes based on YAML frontmatter. ```typescript // File: posts/hello-world.mdx // --- // title: Hello World // date: 2024-01-01 // --- const data = await loadAllMdx() // data[0] = { // path: 'posts/hello-world.mdx', // slug: 'hello-world', // title: 'Hello World', // date: '2024-01-01' // } ``` -------------------------------- ### MDX File Usage Example Source: https://github.com/mquintal/react-router-mdx/blob/master/_autodocs/patterns-and-recipes.md An example of how to use custom MDX components within an MDX file, including rendering alerts, code blocks, YouTube embeds, and callouts. ```mdx --- title: Using Custom Components --- # Using Custom Components This is important information! const x = 42 This is a helpful tip for advanced users. ``` -------------------------------- ### getAttributes Example Usage Source: https://github.com/mquintal/react-router-mdx/blob/master/_autodocs/api-reference/utilities.md Example of extracting YAML frontmatter from MDX content. ```typescript import { getAttributes } from 'react-router-mdx/server/mdx' const content = `--- title: Hello World date: 2024-01-01 tags: - javascript - react published: true --- # Hello World ...` const attrs = getAttributes(content) // Returns: // { // title: 'Hello World', // date: '2024-01-01', // tags: ['javascript', 'react'], // published: true // } ``` -------------------------------- ### compile Example Usage with Plugins Source: https://github.com/mquintal/react-router-mdx/blob/master/_autodocs/api-reference/utilities.md Example of using the compile function with remark plugins. ```typescript import { compile } from 'react-router-mdx/server/mdx' import remarkGfm from 'remark-gfm' const compiled = await compile(mdxContent, { remarkPlugins: [remarkGfm] }) ``` -------------------------------- ### getFilePathBasedOnUrl Example Usage Source: https://github.com/mquintal/react-router-mdx/blob/master/_autodocs/api-reference/utilities.md Examples demonstrating how to use getFilePathBasedOnUrl with and without aliases, and with multiple paths. ```typescript import { getFilePathBasedOnUrl } from 'react-router-mdx/server/utils' // Without alias const filePath1 = getFilePathBasedOnUrl( 'https://example.com/posts/hello', ['posts'] ) // Returns: '/home/user/project/posts/hello.mdx' // With alias const filePath2 = getFilePathBasedOnUrl( 'https://example.com/blog/hello', ['posts'], ['blog'] ) // Returns: '/home/user/project/posts/hello.mdx' // Multiple paths (matches first) const filePath3 = getFilePathBasedOnUrl( 'https://example.com/docs/getting-started', ['posts', 'docs'], ['blog', 'docs'] ) // Returns: '/home/user/project/docs/getting-started.mdx' ``` -------------------------------- ### compile Example Usage Source: https://github.com/mquintal/react-router-mdx/blob/master/_autodocs/api-reference/utilities.md Example of compiling MDX content to JavaScript function code. ```typescript import { compile } from 'react-router-mdx/server/mdx' const mdxContent = `--- title: Hello --- # Hello ` const compiled = await compile(mdxContent) // Returns: // `export const metadata = {...} // export default function MDXContent(props) { // return // }` ``` -------------------------------- ### Example - Posts archive Source: https://github.com/mquintal/react-router-mdx/blob/master/_autodocs/api-reference/client.md Demonstrates how to use useMdxFiles to display a list of posts. ```typescript import { useMdxFiles } from 'react-router-mdx/client' import { loadAllMdx } from 'react-router-mdx/server' export async function loader() { return await loadAllMdx() } export default function Archive() { const posts = useMdxFiles() return ( ) } ``` -------------------------------- ### MDX with custom component example Source: https://github.com/mquintal/react-router-mdx/blob/master/README.md Example of an MDX file that uses a custom YouTube component. ```mdx --- title: hello world title --- # hello world This is a hello World mdx file ``` -------------------------------- ### Basic loadAllMdx Usage Example Source: https://github.com/mquintal/react-router-mdx/blob/master/_autodocs/api-reference/server.md Example of using loadAllMdx to fetch metadata for all MDX files. ```typescript import { loadAllMdx } from 'react-router-mdx/server' export async function loader() { const allPosts = await loadAllMdx() return { posts: allPosts } // Returns: // [ // { path: 'posts/hello-world.mdx', slug: 'hello-world', title: 'Hello World', date: '2024-01-01' }, // { path: 'posts/my-post.mdx', slug: 'my-post', title: 'My Post', date: '2024-01-02' } // ] } ``` -------------------------------- ### Advanced Configuration with Plugins Source: https://github.com/mquintal/react-router-mdx/blob/master/_autodocs/configuration.md Example of advanced configuration using plugins for remark and rehype, and setting up SSR and prerendering. ```typescript // react-router.config.ts import { init } from 'react-router-mdx/server' const mdx = init({ path: 'content/posts', alias: 'blog' }) export default { ssr: true, async prerender() { return ["/", "/blog", ...(await mdx.paths())] } } ``` ```typescript // app/routes/post.tsx import { loadMdx } from 'react-router-mdx/server' import { useMdxComponent, useMdxAttributes } from 'react-router-mdx/client' import remarkGfm from 'remark-gfm' import rehypeHighlight from 'rehype-highlight' import type { Route } from './+types/post' export async function loader({ request }: Route.LoaderArgs) { return await loadMdx(request, { remarkPlugins: [remarkGfm], rehypePlugins: [rehypeHighlight] }) } export default function Post() { const Component = useMdxComponent() const { title, date } = useMdxAttributes() return (

{title}

) } ``` -------------------------------- ### Simple Blog - app/routes.tsx Source: https://github.com/mquintal/react-router-mdx/blob/master/_autodocs/configuration.md Route definitions for a simple blog setup. ```typescript // app/routes.tsx import { index, route } from "@react-router/dev/routes" import { routes } from 'react-router-mdx/server' export default [ index("routes/home.tsx"), route("/blog", "routes/blog-index.tsx"), ...routes("./routes/post.tsx") ] ``` -------------------------------- ### URL to File Path Transformation Example Source: https://github.com/mquintal/react-router-mdx/blob/master/_autodocs/architecture.md Illustrates how a URL is converted back into a file path. ```text Input URL: https://example.com/blog/hello-world Paths: ['posts'] Aliases: ['blog'] Transformation: 1. Extract URL path: /blog/hello-world 2. Find matching alias: blog matches index 0 3. Get corresponding path: posts (index 0) 4. Extract slug: hello-world 5. Construct file path: {cwd}/posts/hello-world.mdx ``` -------------------------------- ### Nested MDX Files Source: https://github.com/mquintal/react-router-mdx/blob/master/_autodocs/patterns-and-recipes.md Example of handling nested MDX files and deriving information from the slug. ```typescript import { useMdxAttributes } from 'react-router-mdx/client' export default function Post() { const attributes = useMdxAttributes() // For /posts/2024/january // You can derive the year and month from the slug if needed const slug = useParams().slug // "2024/january" const [year, month] = slug.split('/') return
{/* ... */}
} ``` -------------------------------- ### MDX File Location - Blog Posts Source: https://github.com/mquintal/react-router-mdx/blob/master/_autodocs/configuration.md Example of organizing MDX files for a blog post collection. ```plaintext posts/ ├── 2024-01-01-hello-world.mdx ├── 2024-01-15-react-tips.mdx └── 2024-02-01-typescript-guide.mdx ``` -------------------------------- ### LoadData Example Source: https://github.com/mquintal/react-router-mdx/blob/master/_autodocs/types.md Example usage of LoadData in a route loader. ```typescript // In route loader const data = await loadMdx(request) // data: LoadData = { // __raw: "export const metadata = {...}; export default function MDXContent(props) {...}", // attributes: { title: 'My Post', date: '2024-01-01', author: 'Jane' } // } ``` -------------------------------- ### Create MDX files Source: https://github.com/mquintal/react-router-mdx/blob/master/_autodocs/README.md Example of a basic MDX file with frontmatter. ```mdx --- title: Hello World date: 2024-01-01 author: Jane Doe --- # Hello World This is my first post. ``` -------------------------------- ### getAttributes Example Usage (No Frontmatter) Source: https://github.com/mquintal/react-router-mdx/blob/master/_autodocs/api-reference/utilities.md Example of using getAttributes when MDX content has no frontmatter. ```typescript const content = `# Hello World without frontmatter` const attrs = getAttributes(content) // Returns: {} (empty object) ``` -------------------------------- ### MDX File Location - Documentation Source: https://github.com/mquintal/react-router-mdx/blob/master/_autodocs/configuration.md Example of organizing MDX files for a documentation collection. ```plaintext docs/ ├── getting-started.mdx ├── installation.mdx ├── api/ │ ├── overview.mdx │ └── functions.mdx └── guides/ └── advanced.mdx ``` -------------------------------- ### Example 1: Custom Route Generation Source: https://github.com/mquintal/react-router-mdx/blob/master/_autodocs/api-reference/utilities.md Generates custom routes by listing MDX files and transforming their paths. ```typescript import { listMdxFilesSync, transformFilePathToUrlPath } from 'react-router-mdx/server/utils' import { route } from '@react-router/dev/routes' const customRoutes = () => { const paths = ['posts'] const files = listMdxFilesSync(paths) return files[0].map(filePath => { const urlPath = transformFilePathToUrlPath(filePath, paths[0]) // Add custom metadata return route(urlPath, 'routes/post.tsx', { id: urlPath, handle: { breadcrumb: urlPath.split('/').pop() } }) }) } export default [...customRoutes()] ``` -------------------------------- ### Add metadata example Source: https://github.com/mquintal/react-router-mdx/blob/master/README.md Example of how to use MDX file attributes to generate meta tags for SEO. ```ts export function meta({ data: { attributes } }: Route.MetaArgs) { return [ { title: attributes.title }, { property: "og:title", content: attributes.title, }, ] } ``` -------------------------------- ### MDXComponents Usage Example Source: https://github.com/mquintal/react-router-mdx/blob/master/_autodocs/types.md Example of how to define and use the MDXComponents type. ```typescript const components: MDXComponents = { h1: ({ children }) => (

{children}

), a: ({ href, children }) => ( {children} ), code: ({ children }) => ( {children} ), YouTube: ({ id }: { id: string }) => (