Skip to content

Commit

Permalink
Make short codes function conditional. (#1088)
Browse files Browse the repository at this point in the history
* Make short codes function conditional.

* Fix failing tests

Co-authored-by: John Otander <johnotander@gmail.com>
  • Loading branch information
ankeetmaini and johno committed Jul 29, 2020
1 parent d7aff71 commit 7454a69
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 27 deletions.
25 changes: 14 additions & 11 deletions packages/mdx/mdx-hast-to-jsx.js
Expand Up @@ -165,20 +165,23 @@ MDXContent.isMDXComponent = true`
const jsxNames = allJsxNames.filter(name => name !== 'MDXLayout')

const importExportNames = importNames.concat(exportNames)
const fakedModulesForGlobalScope =
`const makeShortcode = name => function MDXDefaultShortcode(props) {
console.warn("Component '" + name + "' was not imported, exported, or provided by MDXProvider as global scope")
return <div {...props}/>
};
` +
uniq(jsxNames)
.filter(name => !importExportNames.includes(name))
.map(name => `const ${name} = makeShortcode("${name}");`)
.join('\n')
const shortCodeDef = `const makeShortcode = name => function MDXDefaultShortcode(props) {
console.warn("Component " + name + " was not imported, exported, or provided by MDXProvider as global scope")
return <div {...props}/>
};
`
const fakedModulesForGlobalScope = uniq(jsxNames)
.filter(name => !importExportNames.includes(name))
.map(name => `const ${name} = makeShortcode("${name}");`)
.join('\n')
const fakedModules =
(fakedModulesForGlobalScope &&
[shortCodeDef, fakedModulesForGlobalScope].join('')) ||
''

const moduleBase = `${importStatements}
${exportStatementsPostMdxTypeProps}
${fakedModulesForGlobalScope}
${fakedModules}
${layoutProps}
${mdxLayout}`

Expand Down
3 changes: 3 additions & 0 deletions packages/mdx/test/fixtures/with-shortcode.mdx
@@ -0,0 +1,3 @@
# Hello, world!

<Foo />
5 changes: 5 additions & 0 deletions packages/mdx/test/fixtures/without-shortcode.mdx
@@ -0,0 +1,5 @@
import Foo from 'baz'

# Hello, world!

<Foo />
34 changes: 21 additions & 13 deletions packages/mdx/test/index.test.js
Expand Up @@ -16,6 +16,14 @@ const fixtureBlogPost = fs.readFileSync(
path.join(__dirname, './fixtures/blog-post.mdx')
)

const fixtureWithoutShortcode = fs.readFileSync(
path.join(__dirname, './fixtures/without-shortcode.mdx')
)

const fixtureWithShortcode = fs.readFileSync(
path.join(__dirname, './fixtures/with-shortcode.mdx')
)

it('Should output parseable JSX', async () => {
const result = await mdx('Hello World')

Expand Down Expand Up @@ -44,16 +52,6 @@ it('Should match sample blog post snapshot', async () => {
expect(prettier.format(result, {parser: 'babel'})).toMatchInlineSnapshot(`
"/* @jsx mdx */
const makeShortcode = (name) =>
function MDXDefaultShortcode(props) {
console.warn(
\\"Component '\\" +
name +
\\"' was not imported, exported, or provided by MDXProvider as global scope\\"
);
return <div {...props} />;
};
const layoutProps = {};
const MDXLayout = \\"wrapper\\";
export default function MDXContent({ components, ...props }) {
Expand Down Expand Up @@ -333,9 +331,9 @@ test('Should handle layout props', () => {
authors: ['fred', 'sally']
};
const makeShortcode = name => function MDXDefaultShortcode(props) {
console.warn(\\"Component '\\" + name + \\"' was not imported, exported, or provided by MDXProvider as global scope\\")
return <div {...props}/>
};
console.warn(\\"Component \\" + name + \\" was not imported, exported, or provided by MDXProvider as global scope\\")
return <div {...props}/>
};
const Foo = makeShortcode(\\"Foo\\");
const Bar = makeShortcode(\\"Bar\\");
const layoutProps = {
Expand Down Expand Up @@ -420,6 +418,16 @@ test('Should handle layout props', () => {
`)
})

test('Should not add shortCodes definition if no shortCodes present', async () => {
const result = await mdx(fixtureWithoutShortcode)
expect(result).not.toContain('const makeShortcode = name')
})

test('Should contain shortcode definition', async () => {
const result = await mdx(fixtureWithShortcode)
expect(result).toContain('const makeShortcode = name')
})

it('Should include file name in Babel error', async () => {
try {
await mdx(`<br>`, {filepath: '/path/to/file.mdx'})
Expand Down
6 changes: 3 additions & 3 deletions packages/remark-mdx/test/__snapshots__/test.js.snap
Expand Up @@ -5,9 +5,9 @@ exports[`correctly transpiles 1`] = `
export { Baz } from './foo';
const makeShortcode = name => function MDXDefaultShortcode(props) {
console.warn(\\"Component '\\" + name + \\"' was not imported, exported, or provided by MDXProvider as global scope\\")
return <div {...props}/>
};
console.warn(\\"Component \\" + name + \\" was not imported, exported, or provided by MDXProvider as global scope\\")
return <div {...props}/>
};
const Baz = makeShortcode(\\"Baz\\");
const Paragraph = makeShortcode(\\"Paragraph\\");
const Button = makeShortcode(\\"Button\\");
Expand Down

1 comment on commit 7454a69

@vercel
Copy link

@vercel vercel bot commented on 7454a69 Jul 29, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.