-
Notifications
You must be signed in to change notification settings - Fork 7
/
block-meta.js
163 lines (153 loc) · 3.77 KB
/
block-meta.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
// Used to define what fields are available in metadata editor
import encode from '../util/encode'
const blockMetaSchema =
{ image:
{ title: true,
description: true,
caption: false,
isBasedOnUrl: true,
cover: true,
changeCover: true,
removeCover: false,
author: true,
publisher: true,
via: true,
makeHtml: makeImage,
},
video:
{ title: true,
description: true,
caption: false,
isBasedOnUrl: true,
cover: true,
changeCover: false,
removeCover: false,
author: true,
publisher: true,
via: true,
},
article:
{ title: true,
description: true,
caption: false,
isBasedOnUrl: true,
cover: true,
changeCover: true,
removeCover: true,
author: true,
publisher: true,
via: true,
makeHtml: makeArticle,
},
quote:
{ title: false,
description: true,
caption: false,
isBasedOnUrl: true,
cover: false,
changeCover: false,
removeCover: false,
author: true,
publisher: true,
via: true,
makeHtml: makeQuote,
},
cta:
{ label: true,
link: true,
canFrame: true,
makeHtml: makeCTA,
},
default:
{ title: true,
description: true,
caption: false,
isBasedOnUrl: true,
cover: true,
changeCover: false,
removeCover: false,
author: true,
publisher: true,
via: true,
},
}
function makeImage (block) {
const {metadata, cover} = block
let htmlString = '<img'
if (cover && cover.src) {
htmlString += ` src="${cover.src}"`
}
if (metadata && metadata.title) {
htmlString += ` title="${encode(metadata.title)}"`
}
if (metadata && metadata.description) {
htmlString += ` alt="${encode(metadata.description)}"`
}
htmlString += '>'
return htmlString
}
// function makeFigure (metadata, cover) {
// let htmlString = `<figure>`
// if (cover && cover.src) {
// htmlString += `<img src="${cover.src}">`
// }
// htmlString += makeTitleDescription('figcaption', metadata)
// htmlString += `</figure>`
// return htmlString
// }
// function makeQuote (metadata, _) {
// return `<blockquote>${encode(metadata.description)}</blockquote>`
// }
function makeArticle (block) {
const {metadata, cover} = block
let htmlString = '<article>'
if (cover && cover.src) {
htmlString += `<img src="${cover.src}">`
}
htmlString += makeTitleDescription(null, metadata)
htmlString += '</article>'
return htmlString
}
function makeTitleDescription (tag, metadata) {
let htmlString = ''
if (metadata && metadata.title) {
htmlString += `<h1>${encode(metadata.title)}</h1>`
}
if (metadata && metadata.description) {
htmlString += `<p>${encode(metadata.description)}</p>`
}
if (tag && htmlString) {
htmlString = `<${tag}>${htmlString}</${tag}>`
}
return htmlString
}
function makeCTA (block) {
const {url} = block
let {label} = block
const dataString = makeDataString(block)
label = label || 'Open'
if (url) {
return `<a href="${url}" data-role="cta"${dataString}>${encode(label)}</a>`
}
return `<button data-role="cta"${dataString}>${encode(label)}</button>`
}
function makeDataString (block) {
const fields = ['cta', 'price'] // TODO type? item?
let str = ''
for (let i = 0, len = fields.length; i < len; i++) {
const field = fields[i]
if (!block[field]) continue
str += ` data-${field}="${encode(block[field])}"`
}
return str
}
function makeQuote (block) {
if (block.metadata && block.metadata.hasOwnProperty('description')) {
return `<blockquote>${encode(block.metadata.description)}</blockquote>`
}
if (block.html) {
return block.html
}
return '<blockquote></blockquote>'
}
export default blockMetaSchema