Skip to content

Commit

Permalink
feat: add exports
Browse files Browse the repository at this point in the history
  • Loading branch information
kellyjosephprice committed Jan 15, 2025
1 parent 23e7751 commit 1dd7e44
Show file tree
Hide file tree
Showing 7 changed files with 32 additions and 6 deletions.
4 changes: 2 additions & 2 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
ARG NODE_VERSION=18
FROM node:${NODE_VERSION}-alpine3.18
ARG NODE_VERSION=22.13
FROM node:${NODE_VERSION}-alpine

ARG NODE_VERSION
ENV NODE_VERSION=$NODE_VERSION
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions __tests__/browser/markdown.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ describe('visual regression tests', () => {
// skipping this because they sporadically failure with network timing
// issues
//'embeds',
'exportTests',
//'features',
'headings',
'images',
Expand Down
9 changes: 9 additions & 0 deletions docs/export-tests.mdx
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
## Same component file, but different export name

```
- <One />
- <Two />
```

- <One />
- <Two />
6 changes: 6 additions & 0 deletions example/Doc.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@ export const Test = ({ color = 'thistle' } = {}) => {
export default Test;
`,
MultipleExports: `
export const One = () => "One";
export const Two = () => "Two";
`,
};

const executedComponents = {};
Expand Down Expand Up @@ -82,6 +87,7 @@ const Doc = () => {

try {
const code = mdx.compile(doc, { ...opts, components });
console.log(executedComponents);
const content = await mdx.run(code, { components: executedComponents, terms, variables });

setError(() => null);
Expand Down
3 changes: 3 additions & 0 deletions example/docs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import codeBlocks from '../docs/code-blocks.md';
// @ts-ignore
import embeds from '../docs/embeds.md';
// @ts-ignore
import exportTests from '../docs/export-tests.mdx';
// @ts-ignore
import features from '../docs/features.md';
// @ts-ignore
import gettingStarted from '../docs/getting-started.md';
Expand Down Expand Up @@ -44,6 +46,7 @@ const fixtures = Object.entries({
codeBlockTests,
codeBlocks,
embeds,
exportTests,
features,
gettingStarted,
headings,
Expand Down
15 changes: 11 additions & 4 deletions lib/run.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,15 @@ 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 defaults = Object.fromEntries(
Object.entries(components).map(([tag, module]) => [tag, 'default' in module ? module.default : module]),
);
const defaults = Object.entries(components).reduce((memo, [tag, mod]) => {
memo[tag] = mod.default;

Object.entries(mod.exports).forEach(([subTag, component]) => {

Check failure on line 60 in lib/run.tsx

View workflow job for this annotation

GitHub Actions / Test (latest, 18)

__tests__/custom-components/index.test.tsx > Custom Components > renders custom components

TypeError: Cannot convert undefined or null to object ❯ lib/run.tsx:60:12 ❯ Module.run lib/run.tsx:57:47 ❯ Module.execute __tests__/helpers.ts:21:24 ❯ __tests__/custom-components/index.test.tsx:21:24

Check failure on line 60 in lib/run.tsx

View workflow job for this annotation

GitHub Actions / Test (latest, 18)

__tests__/custom-components/index.test.tsx > Custom Components > renders custom components recursively

TypeError: Cannot convert undefined or null to object ❯ lib/run.tsx:60:12 ❯ Module.run lib/run.tsx:57:47 ❯ Module.execute __tests__/helpers.ts:21:24 ❯ __tests__/custom-components/index.test.tsx:32:24
memo[subTag] = component;
});

return memo;
}, {});

const exec = (text: string, { useMDXComponents = makeUseMDXComponents(defaults) }: RunOpts = {}) => {
return mdxRun(text, {
Expand All @@ -69,7 +75,7 @@ const run = async (string: string, _opts: RunOpts = {}) => {
}) as Promise<RMDXModule>;
};

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

const tocMdx = tocToMdx(toc, components);
const { default: Toc } = await exec(compile(tocMdx), { useMDXComponents: () => ({ p: Fragment }) });
Expand All @@ -80,6 +86,7 @@ const run = async (string: string, _opts: RunOpts = {}) => {
<Content />
</Contexts>
),
exports,
toc,
Toc: () =>
tocMdx &&
Expand Down

0 comments on commit 1dd7e44

Please sign in to comment.