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

Property adjustments for "style", "aria-*" and "data-*" #261

Merged
merged 2 commits into from Sep 19, 2018
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
17 changes: 17 additions & 0 deletions packages/mdx/mdx-hast-to-jsx.js
@@ -1,3 +1,6 @@
const toStyleObject = require('to-style').object
const { paramCase } = require('change-case')

function toJSX(node, parentNode = {}, options = {}) {
const {
// default options
Expand All @@ -6,6 +9,20 @@ function toJSX(node, parentNode = {}, options = {}) {
} = options
let children = ''

if (node.properties != null) {
if (typeof node.properties.style === 'string') {
node.properties.style = toStyleObject(node.properties.style, { camelize: true })
}

// ariaProperty => aria-property
// dataProperty => data-property
const paramCaseRe = /^(aria[A-Z])|(data[A-Z])/
node.properties = Object.entries(node.properties).reduce((properties, [key, value]) => ({
...properties,
[paramCaseRe.test(key) ? paramCase(key) : key]: value,
}), {})
}

if (node.type === 'root') {
const importNodes = []
const exportNodes = []
Expand Down
7 changes: 7 additions & 0 deletions packages/mdx/package.json
Expand Up @@ -22,9 +22,11 @@
"mdx"
],
"dependencies": {
"change-case": "^3.0.2",
"mdast-util-to-hast": "^3.0.0",
"remark-parse": "^5.0.0",
"remark-squeeze-paragraphs": "^3.0.1",
"to-style": "^1.3.3",
"unified": "^6.1.6",
"unist-util-visit": "^1.3.0"
},
Expand All @@ -35,6 +37,11 @@
"@mapbox/rehype-prism": "^0.2.0",
"hast-util-select": "^1.0.1",
"jest": "^22.4.3",
"rehype-katex": "^1.1.1",
"remark-math": "^1.0.4",
"request-image-size": "^2.1.0"
},
"jest": {
"testEnvironment": "node"
}
}
27 changes: 27 additions & 0 deletions packages/mdx/test/index.test.js
Expand Up @@ -6,6 +6,8 @@ const path = require('path')
const { select } = require('hast-util-select')
const requestImageSize = require('request-image-size')
const prism = require('@mapbox/rehype-prism')
const math = require('remark-math')
const katex = require('rehype-katex')

const fixtureBlogPost = fs.readFileSync(
path.join(__dirname, './fixtures/blog-post.md')
Expand Down Expand Up @@ -102,6 +104,31 @@ A paragraph
expect(result).toContain('{/* a nested Markdown comment */}')
})

it('Should convert style strings to camelized objects', async () => {
const result = await mdx(`
$$
\\sum{1}
$$
`, {
mdPlugins: [math],
hastPlugins: [katex],
})
expect(result).not.toContain('"style":"')
expect(result).toContain('"style":{')
})

it('Should convert data-* and aria-* properties to param-case', async () => {
const result = await mdx(`
$$
\\sum{1}
$$
`, {
mdPlugins: [math],
hastPlugins: [katex],
})
expect(result).toContain('"aria-hidden":"true"')
})

it('Should not include export wrapper if skipExport is true', async () => {
const result = await mdx('> test\n\n> `test`', { skipExport: true })

Expand Down