This repository has been archived by the owner on Apr 12, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
serializers.js
172 lines (140 loc) · 4.93 KB
/
serializers.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
const objectAssign = require('object-assign')
const getImageUrl = require('./getImageUrl')
module.exports = (h, serializerOpts) => {
const serializeOptions = serializerOpts || {useDashedStyles: false}
// Low-level block serializer
function BlockSerializer(props) {
const {node, serializers, options, isInline, children} = props
const blockType = node._type
const serializer = serializers.types[blockType]
if (!serializer) {
throw new Error(
`Unknown block type "${blockType}", please specify a serializer for it in the \`serializers.types\` prop`
)
}
return h(serializer, {node, options, isInline}, children)
}
// Low-level span serializer
function SpanSerializer(props) {
const {mark, children} = props.node
const isPlain = typeof mark === 'string'
const markType = isPlain ? mark : mark._type
const serializer = props.serializers.marks[markType]
if (!serializer) {
// @todo Revert back to throwing errors?
// eslint-disable-next-line no-console
console.warn(
`Unknown mark type "${markType}", please specify a serializer for it in the \`serializers.marks\` prop`
)
return h(props.serializers.markFallback, null, children)
}
return h(serializer, props.node, children)
}
// Low-level list serializer
function ListSerializer(props) {
const tag = props.type === 'bullet' ? 'ul' : 'ol'
return h(tag, null, props.children)
}
// Low-level list item serializer
function ListItemSerializer(props) {
const children =
!props.node.style || props.node.style === 'normal'
? // Don't wrap plain text in paragraphs inside of a list item
props.children
: // But wrap any other style in whatever the block serializer says to use
h(props.serializers.types.block, props, props.children)
return h('li', null, children)
}
// Renderer of an actual block of type `block`. Confusing, we know.
function BlockTypeSerializer(props) {
const style = props.node.style || 'normal'
if (/^h\d/.test(style)) {
return h(style, null, props.children)
}
return style === 'blockquote'
? h('blockquote', null, props.children)
: h('p', null, props.children)
}
// Serializers for things that can be directly attributed to a tag without any props
// We use partial application to do this, passing the tag name as the first argument
function RawMarkSerializer(tag, props) {
return h(tag, null, props.children)
}
function UnderlineSerializer(props) {
const style = serializeOptions.useDashedStyles
? {'text-decoration': 'underline'}
: {textDecoration: 'underline'}
return h('span', {style}, props.children)
}
function StrikeThroughSerializer(props) {
return h('del', null, props.children)
}
function LinkSerializer(props) {
return h('a', {href: props.mark.href}, props.children)
}
function ImageSerializer(props) {
if (!props.node.asset) {
return null
}
const img = h('img', {src: getImageUrl(props)})
return props.isInline ? img : h('figure', null, img)
}
// Serializer that recursively calls itself, producing a hyperscript tree of spans
function serializeSpan(span, serializers, index, options) {
if (span === '\n' && serializers.hardBreak) {
return h(serializers.hardBreak, {key: `hb-${index}`})
}
if (typeof span === 'string') {
return serializers.text ? h(serializers.text, {key: `text-${index}`}, span) : span
}
let children
if (span.children) {
children = {
children: span.children.map((child, i) =>
options.serializeNode(child, i, span.children, true)
)
}
}
const serializedNode = objectAssign({}, span, children)
return h(serializers.span, {
key: span._key || `span-${index}`,
node: serializedNode,
serializers
})
}
const HardBreakSerializer = () => h('br')
const defaultMarkSerializers = {
strong: RawMarkSerializer.bind(null, 'strong'),
em: RawMarkSerializer.bind(null, 'em'),
code: RawMarkSerializer.bind(null, 'code'),
underline: UnderlineSerializer,
'strike-through': StrikeThroughSerializer,
link: LinkSerializer
}
const defaultSerializers = {
// Common overrides
types: {
block: BlockTypeSerializer,
image: ImageSerializer
},
marks: defaultMarkSerializers,
// Less common overrides
list: ListSerializer,
listItem: ListItemSerializer,
block: BlockSerializer,
span: SpanSerializer,
hardBreak: HardBreakSerializer,
// Container element
container: 'div',
// When we can't resolve the mark properly, use this renderer as the container
markFallback: 'span',
// Allow overriding text renderer, but leave undefined to just use plain strings by default
text: undefined,
// Empty nodes (React uses null, hyperscript with empty strings)
empty: ''
}
return {
defaultSerializers,
serializeSpan
}
}