Skip to content

Commit

Permalink
fix: imports (#1050)
Browse files Browse the repository at this point in the history
| [![PR App][icn]][demo] | Fix RM-11562 |
| :--------------------: | :----------: |

## 🧰 Changes

Fixes user variables in the mdx-renderer.

The mdx-renderer passes in `{ imports: { React } }` as they have to
provide their own compiled version. `lib/run` was not properly merging
that with it's own imports, and that was causing `user` to be undefined!

## 🧬 QA & Testing

- [Broken on production][prod].
- [Working in this PR app][demo].

[demo]: https://markdown-pr-PR_NUMBER.herokuapp.com
[prod]: https://SUBDOMAIN.readme.io
[icn]:
https://user-images.githubusercontent.com/886627/160426047-1bee9488-305a-4145-bb2b-09d8b757d38a.svg
  • Loading branch information
kellyjosephprice authored Jan 28, 2025
1 parent 6e98c0d commit f4aca39
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 3 deletions.
24 changes: 24 additions & 0 deletions __tests__/lib/run.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import React from 'react';
import { render, screen } from '@testing-library/react';

import { execute } from '../helpers';

describe('run', () => {
it('allows providing imports', async () => {
const mdx = `Hello, world!`;
const Component = await execute(mdx, {}, { imports: { React } });

render(<Component />);

expect(screen.getByText('Hello, world!')).toBeInTheDocument();
});

it('merges the imports with the built-ins', async () => {
const mdx = `{user.test}`;
const Component = await execute(mdx, {}, { imports: { React } });

render(<Component />);

expect(screen.getByText('TEST')).toBeInTheDocument();
});
});
6 changes: 3 additions & 3 deletions lib/run.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ const makeUseMDXComponents = (more: ReturnType<UseMdxComponents> = {}): UseMdxCo

const run = async (string: string, _opts: RunOpts = {}) => {
const { Fragment } = runtime as any;
const { components = {}, terms, variables, baseUrl, ...opts } = _opts;
const { components = {}, terms, variables, baseUrl, imports = {}, ...opts } = _opts;
const executedComponents = Object.entries(components).reduce((memo, [tag, mod]) => {
const { default: Content, toc, Toc, ...rest } = mod;
memo[tag] = Content;
Expand All @@ -68,10 +68,10 @@ const run = async (string: string, _opts: RunOpts = {}) => {
...runtime,
Fragment,
baseUrl: import.meta.url,
imports: { React, user: User(variables) },
imports: { React, user: User(variables), ...imports },
useMDXComponents,
...opts,
}) as Promise<RMDXModule>;
} as RunOptions) as Promise<RMDXModule>;
};

const { Toc: _Toc, toc, default: Content, ...exports } = await exec(string);
Expand Down

0 comments on commit f4aca39

Please sign in to comment.