Skip to content

Instantly share code, notes, and snippets.

@long-lazuli
Last active November 23, 2017 14:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save long-lazuli/087c910c08b1fe9418d41e99b6eb8ced to your computer and use it in GitHub Desktop.
Save long-lazuli/087c910c08b1fe9418d41e99b6eb8ced to your computer and use it in GitHub Desktop.
JSX to CKEditor5 Template
// Use `vdom` classic JSX syntax for CK templates.
import Template from '@ckeditor/ckeditor5-ui/src/template';
const CKTemplateFromJSX = ({elementName, attributes, children}) => new Template({
tag: elementName,
attributes: Object.keys(attributes).filter( key => {
return key.indexOf('on') !== 0
}).reduce((acc, key) => {
let ckKey = key;
if( key == "className" ) ckKey = 'class'
if( key == "htmlFor" ) ckKey = 'for'
acc[ckKey] = attributes[key]
return acc
}, {}),
on: Object.keys(attributes).filter( key => {
return key.indexOf('on') === 0
}).reduce( (acc, key) => {
const ckKey = key.slice(2).toLowerCase();
acc[ckKey] = attributes[key]
return acc
}, {}),
children: children
})
export default CKTemplateFromJSX
// Using https://www.npmjs.com/package/babel-plugin-transform-jsx :
use: {
loader: 'babel-loader',
options: {
plugins: [
["transform-jsx", { "function": "CKTemplateFromJSX" }]
]
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment