Skip to content

Instantly share code, notes, and snippets.

@bouiboui
Created February 17, 2021 10:41
Show Gist options
  • Save bouiboui/118142ec61eba63dd64af897d04e11de to your computer and use it in GitHub Desktop.
Save bouiboui/118142ec61eba63dd64af897d04e11de to your computer and use it in GitHub Desktop.
Useful to convert HTML attributes to CSS (to convert from chakra to styled components, for example)
// yarn add posthtml-parser
import parser from 'posthtml-parser'
// Paste your HTML component here
const html = `
<Box
fontSize="12px"
lineHeight="16px"
textAlign="right"
/>
`
const elements = (parser.default(html.trim()))[0].attrs
const res = Object.entries(elements)
.reduce((res, [k, v]) => (res.concat(
`${k.replace(/([A-Z])/g, '-$1').toLowerCase()}: ${v.replace(/[{}]/gis, '')};\n`
)), ``)
// Output:
// font-size: 12px;
// line-height: 16px;
// text-align: right;
console.log((res))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment