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

Refactor to generate JS AST #1382

Merged
merged 1 commit into from
Dec 18, 2020
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
21 changes: 21 additions & 0 deletions packages/babel-plugin-apply-mdx-type-props/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,27 @@ class BabelPluginApplyMdxTypeProp {
JSXOpeningElement(path) {
const jsxName = path.node.name.name

let parentPath = path.parentPath.parentPath
let parentName

while (parentPath) {
if (parentPath.node.type === 'JSXElement') {
parentName = parentPath.node.openingElement.name.name
break
}

parentPath = parentPath.parentPath
}

if (typeof parentName === 'string' && parentName !== 'MDXLayout') {
path.node.attributes.push(
t.jSXAttribute(
t.jSXIdentifier(`parentName`),
t.stringLiteral(parentName)
)
)
}

if (startsWithCapitalLetter(jsxName)) {
names.push(jsxName)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const transform = value => {
describe('babel-plugin-add-mdx-type-prop', () => {
test('should add `mdxType` to components', () => {
expect(transform('var d = <div><Button /></div>').code).toEqual(
'var d = <div><Button mdxType="Button" /></div>;'
'var d = <div><Button parentName="div" mdxType="Button" /></div>;'
wooorm marked this conversation as resolved.
Show resolved Hide resolved
)
})

Expand Down
2 changes: 2 additions & 0 deletions packages/mdx/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ const unified = require('unified')
const remarkParse = require('remark-parse')
const remarkMdx = require('remark-mdx')
const squeeze = require('remark-squeeze-paragraphs')
const minifyWhitespace = require('rehype-minify-whitespace')
const mdxAstToMdxHast = require('./mdx-ast-to-mdx-hast')
const mdxHastToJsx = require('./mdx-hast-to-jsx')

Expand All @@ -20,6 +21,7 @@ function createMdxAstCompiler(options = {}) {
function createCompiler(options = {}) {
return createMdxAstCompiler(options)
.use(options.rehypePlugins)
.use(minifyWhitespace, {newlines: true})
.use(mdxHastToJsx, options)
}

Expand Down
Loading