-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathextend-node-type.js
90 lines (82 loc) · 2.32 KB
/
extend-node-type.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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
const { GraphQLJSON } = require('gatsby/graphql')
const sanitizeHtml = require('sanitize-html')
const styler = require('react-styling')
const HtmlToReactParser = require('html-to-react').Parser
const htmlToReactParser = new HtmlToReactParser()
const { TYPE_PREFIX_COCKPIT } = require('./src/constants')
module.exports = async ({ type }) => {
if (!type.name.startsWith(TYPE_PREFIX_COCKPIT)) {
return {}
}
const parseLayout = layout => {
if (layout == null || layout.length === 0) {
return layout
}
return layout.map(node => {
if (node.settings) {
node = parseSettings(node)
}
Object.entries(node).forEach(([key, value]) => {
if (value instanceof Array) {
parseLayout(node[key])
}
if (value instanceof Object && node[key].settings) {
node[key].settings = parseSettings(node.settings)
}
})
return node
})
}
const parseHtml = (type, node) => {
const { settings } = node
if (settings[type] === '') {
node.settings.html = null
} else if (settings[type] && settings[type].length > 0) {
node.settings.html = settings[type]
node.settings.html_sanitize = sanitizeHtml(settings[type], {})
node.settings.html_react = htmlToReactParser.parse(settings[type])
}
return node
}
const parseSettings = node => {
const { settings } = node
if (settings.html) {
node = parseHtml('html', node)
}
if (settings.text) {
node = parseHtml('text', node)
}
if (settings.id === '') {
settings.id = null
}
if (settings.class === '') {
settings.className = null
} else {
settings.className = settings.class
}
delete settings.class
if (settings.style === '' || settings.style == null) {
settings.style = null
} else {
settings.style = styler(settings.style)
}
return node
}
let nodeExtendType = {}
if (type.name === 'CockpitLayoutNode') {
nodeExtendType['parsed'] = {
type: GraphQLJSON,
resolve(Item) {
return parseLayout(JSON.parse(Item.internal.content))
},
}
} else if (type.name === 'CockpitObjectNode') {
nodeExtendType['data'] = {
type: GraphQLJSON,
resolve(Item) {
return JSON.parse(Item.internal.content)
},
}
}
return nodeExtendType
}