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

Fix default export postprocessing #2

Merged
merged 1 commit into from
May 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"deepscan.enable": true
}
65 changes: 65 additions & 0 deletions src/mdx2.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,71 @@ describe('mdx2', () => {
const mdxStoryNameToKey = { foo: 'foo' };
`);
});

it('full snapshot', () => {
const input = dedent`
# hello

<Meta title="foobar" />

world {2 + 1}

<Story name="foo">bar</Story>
`;
// @ts-ignore
expect(compileSync(input)).toMatchInlineSnapshot(`
/*@jsxRuntime automatic @jsxImportSource react*/
import { assertIsFn, AddContext } from "@storybook/addon-docs";
import {Fragment as _Fragment, jsx as _jsx, jsxs as _jsxs} from "react/jsx-runtime";
function MDXContent(props = {}) {
const {wrapper: MDXLayout} = props.components || ({});
return MDXLayout ? _jsx(MDXLayout, Object.assign({}, props, {
children: _jsx(_createMdxContent, {})
})) : _createMdxContent();
function _createMdxContent() {
const _components = Object.assign({
h1: "h1",
p: "p"
}, props.components), {Meta, Story} = _components;
if (!Meta) _missingMdxReference("Meta", true);
if (!Story) _missingMdxReference("Story", true);
return _jsxs(_Fragment, {
children: [_jsx(_components.h1, {
children: "hello"
}), "\\n", _jsx(Meta, {
title: "foobar"
}), "\\n", _jsxs(_components.p, {
children: ["world ", 2 + 1]
}), "\\n", _jsx(Story, {
name: "foo",
children: "bar"
})]
});
}
}
function _missingMdxReference(id, component) {
throw new Error("Expected " + (component ? "component" : "object") + " \`" + id + "\` to be defined: you likely forgot to import, pass, or provide it.");
}
// =========
export const foo = () => (
"bar"
);
foo.storyName = 'foo';
foo.parameters = { storySource: { source: '\\"bar\\"' } };

const componentMeta = { title: 'foobar', includeStories: ["foo"], };

const mdxStoryNameToKey = {"foo":"foo"};

componentMeta.parameters = componentMeta.parameters || {};
componentMeta.parameters.docs = {
...(componentMeta.parameters.docs || {}),
page: () => <AddContext mdxStoryNameToKey={mdxStoryNameToKey} mdxComponentAnnotations={componentMeta}><MDXContent /></AddContext>,
};

export default componentMeta;
`);
});
});

describe('docs-mdx-compiler-plugin', () => {
Expand Down
6 changes: 3 additions & 3 deletions src/mdx2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -115,14 +115,14 @@ export const plugin = (store: any) => (root: any) => {

export const postprocess = (code: string, extractedExports: string) => {
const lines = code.toString().trim().split('\n');
const last = lines.pop();

// /*@jsxRuntime automatic @jsxImportSource react*/
const first = lines.shift();

return [
first,
'import { assertIsFn, AddContext } from "@storybook/addon-docs";',
...lines,
last,
...lines.filter((line) => !line.match(/^export default/)),
SEPARATOR,
extractedExports,
].join('\n');
Expand Down