Skip to content
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
8 changes: 1 addition & 7 deletions packages/mdx/mdx-hast-to-jsx.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,10 @@ const declare = require('@babel/helper-plugin-utils').declare
const {types: t} = require('@babel/core')
const toStyleObject = require('to-style').object
const uniq = require('lodash.uniq')
const {toTemplateLiteral} = require('./util')
const {paramCase, toTemplateLiteral} = require('./util')

const STARTS_WITH_CAPITAL_LETTER = /^[A-Z]/

const paramCase = string =>
string
.replace(/([a-z0-9])([A-Z])/g, '$1-$2')
.replace(/([a-z])([0-9])/g, '$1-$2')
.toLowerCase()

class BabelPluginExtractJsxNames {
constructor() {
const names = []
Expand Down
14 changes: 14 additions & 0 deletions packages/mdx/test/util.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
const {paramCase} = require('../util')

const PARAM_CASE_FIXTURES = [
['ariaHidden', 'aria-hidden'],
['data123', 'data-123']
]

PARAM_CASE_FIXTURES.forEach(([input, expected]) => {
test(`it turns ${input} into ${expected}`, () => {
const result = paramCase(input)

expect(result).toEqual(expected)
})
})
7 changes: 7 additions & 0 deletions packages/mdx/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,12 @@ const EXPORT_DEFAULT_REGEX = /^export default/
const BLOCKS_REGEX = '[a-z\\.]+(\\.){0,1}[a-z\\.]'
const EMPTY_NEWLINE = '\n\n'

const paramCase = string =>
string
.replace(/([a-z0-9])([A-Z])/g, '$1-$2')
.replace(/([a-z])([0-9])/g, '$1-$2')
.toLowerCase()

const toTemplateLiteral = text => {
const escaped = text
.replace(/\\/g, '\\\\') // Escape all "\" to avoid unwanted escaping in text nodes
Expand All @@ -18,4 +24,5 @@ module.exports.BLOCKS_REGEX = BLOCKS_REGEX
module.exports.isImport = text => IMPORT_REGEX.test(text)
module.exports.isExport = text => EXPORT_REGEX.test(text)
module.exports.isExportDefault = text => EXPORT_DEFAULT_REGEX.test(text)
module.exports.paramCase = paramCase
module.exports.toTemplateLiteral = toTemplateLiteral