-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgenerator.js
47 lines (39 loc) · 1.27 KB
/
generator.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
const API = {
"wp.rich": "wp.blockEditor.RichText",
"wp.plain": "wp.blockEditor.PlainText",
}
const toSaveFunction = ({ type, props, children }) => `
(({ attributes }) => ${toCode({ type, props, children })})`
const toCode = ({ type, props, children }) =>
`el(${toComponent(type)}, ${toAttrs(Object(props))}, ${
children.map(
c => typeof c === 'string' ? toText(c) : toCode(c)
).join(', ')
})`
const toComponent = type =>
API.hasOwnProperty(type) ? API[type] : `"${type}"`
const toAttrs = obj => [
'{',
Object.entries(mapValues(toAttr, obj))
.map(([k, v]) => `"${k}": ${v}`).join(', '),
'}',
].join('')
// Escapes attributes while interpolating fragments in the "{data}" format.
const toAttr = val =>
JSON.stringify(val.replace(/{([A-z0-9_]+)}/g, '${attributes.$1}'))
.replace(/`/g, "\\`")
.replace(/^"/, "`")
.replace(/"$/, "`")
// Escapes text fragments while interpolating fragments in the "{{data}}" format.
const toText = val =>
JSON.stringify(val.replace(/{{([A-z0-9_]+)}}/g, '${attributes.$1}'))
.replace(/`/g, "\\`")
.replace(/^"/, "`")
.replace(/"$/, "`")
// Similar to lodash.mapValues(obj, f).
const mapValues = (f, obj) =>
Object.keys(obj).reduce((acc, k) => {
acc[k] = f(obj[k])
return acc
}, {})
module.exports = { generate: toSaveFunction }