Created
February 17, 2021 10:41
-
-
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)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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