Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(dev): support overriding components for Markdown elements #7502

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from

Conversation

lpsinger
Copy link
Contributor

You can use an MDX provider to override which React components are used for standard Markdown elements. For example, suppose that for all routes under the /products path you want to replace all Markdown links with <Link> components instead of HTML <a> elements. Create the following route module app/routes/products.tsx:

import { MDXProvider } from "@mdx-js/react";
import { Link, Outlet } from "@remix-run/react";

export default function Products() {
  return (
    <MDXProvider
      components={{
        a: ({ children, href, ref, ...props }) =>
          href ? (
            <Link to={href} {...props}>
              {children}
            </Link>
          ) : (
            <a {...props}>{children}</a>
          ),
      }}
    >
      <Outlet />
    </MDXProvider>
  );
}

Then app/routes/products.hammers.mdx might look like this:

# Hammers

If you like our hammers, then you should also check out our
[nails](/products/nails)!
  • Docs
  • Tests

Testing Strategy: Modified an existing integration test

@changeset-bot
Copy link

changeset-bot bot commented Sep 20, 2023

🦋 Changeset detected

Latest commit: 736ef04

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 17 packages
Name Type
integration-tests Minor
@remix-run/dev Minor
@remix-run/server-runtime Minor
@remix-run/cloudflare Minor
@remix-run/deno Minor
@remix-run/node Minor
@remix-run/react Minor
@remix-run/cloudflare-pages Minor
@remix-run/cloudflare-workers Minor
@remix-run/architect Minor
@remix-run/express Minor
@remix-run/serve Minor
@remix-run/testing Minor
create-remix Minor
remix Minor
@remix-run/css-bundle Minor
@remix-run/eslint-config Minor

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@lpsinger
Copy link
Contributor Author

Note that this is to some extent a duplicate of #6354, although I prefer this implementation because it requires less configuration to override components.

@lpsinger
Copy link
Contributor Author

I rebased this. Could I get a review please?

You can use an [MDX provider](https://mdxjs.com/docs/using-mdx/#mdx-provider)
to override which React components are used for standard Markdown elements.
For example, suppose that for all routes under the `/products` path you want
to replace all Markdown links with [`<Link>`](../components/link) components
instead of HTML `<a>` elements. Create the following route module
`app/routes/products.tsx`:

```tsx filename=app/routes/products.tsx
import { MDXProvider } from "@mdx-js/react";
import { Link, Outlet } from "@remix-run/react";

export default function Products() {
  return (
    <MDXProvider
      components={{
        a: ({ children, href, ref, ...props }) =>
          href ? (
            <Link to={href} {...props}>
              {children}
            </Link>
          ) : (
            <a {...props}>{children}</a>
          ),
      }}
    >
      <Outlet />
    </MDXProvider>
  );
}
```

Then `app/routes/products.hammers.mdx` might look like this:

```mdx filename=app/routes/products.hammers.mdx

If you like our hammers, then you should also check out our
[nails](/products/nails)!
```
@lpsinger
Copy link
Contributor Author

Rebased again. Sorry to keep pestering, I know maintainers are busy! Could I get a review please?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants