-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathrollup-plugin-content-loader.js
169 lines (151 loc) · 4.47 KB
/
rollup-plugin-content-loader.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
import * as contentful from 'contentful'
import { MARKS, BLOCKS } from '@contentful/rich-text-types'
import { documentToHtmlString } from '@contentful/rich-text-html-renderer'
import * as dotenv from 'dotenv'
// Rollup plugin that allows importing of Contentful entries.
// To get a specific entry:
// import entry from '@contentful-entry/{contentType}/{key}'
// If there is only one entry of that content type:
// import entry from '@contentful-entry/{contentType}'
const ENTRY_PREFIX = '@contentful-entry'
const ENTRIES_PREFIX = '@contentful-entries'
if (process.env.NODE_ENV === 'development') dotenv.config()
/** The Contentful client object. */
const client = contentful.createClient({
space: process.env.CONTENTFUL_SPACE_ID,
accessToken: process.env.CONTENTFUL_ACCESS_TOKEN,
})
async function mapEntry(entry, schema) {
if (!entry) {
return null
}
const { fields } = entry
await Promise.all(
schema.fields
.filter(({ id }) => fields[id])
.map(async ({ id, type, ...model }) => {
switch (type) {
case 'RichText':
fields[id] = {
html: toHtml(fields[id]),
inlineHtml: toInlineHtml(fields[id]),
}
break
case 'Link':
fields[id] = await mapLink(fields[id], model.linkType)
break
case 'Array':
fields[id] = await Promise.all(
fields[id].map(item => mapLink(item, model.items.linkType))
)
break
}
})
)
return fields
}
async function mapLink(link, linkType) {
switch (linkType) {
case 'Asset':
return toImg(link)
case 'Entry':
if (link.sys.contentType === undefined) return null
const contentType = link.sys.contentType.sys.id
const schema = await client.getContentType(contentType)
return await mapEntry(link, schema)
}
}
function getHtmlOptions({ renderMark = {}, renderNode = {}, ...options } = {}) {
return {
renderMark: {
[MARKS.BOLD]: text => `<strong>${text}</strong>`,
[MARKS.ITALIC]: text => `<em>${text}</em>`,
...renderMark,
},
renderNode: {
[BLOCKS.PARAGRAPH]: (node, next) =>
`<p>${next(node.content).replace(/\n/g, '<br/>')}</p>`,
...renderNode,
},
...options,
}
}
function toHtml(document) {
return documentToHtmlString(document, getHtmlOptions())
}
function toInlineHtml(document) {
return documentToHtmlString(
document,
getHtmlOptions({
renderNode: {
[BLOCKS.PARAGRAPH]: (node, next) =>
next(node.content).replace(/\n/g, '<br/>'),
},
})
)
}
function toImg(link) {
return {
src: link.fields.file.url,
alt: link.fields.title,
}
}
export default function contentLoader() {
return {
name: 'content-loader',
resolveId(source) {
const parts = source.split('/')
switch (parts[0]) {
case ENTRY_PREFIX:
case ENTRIES_PREFIX:
return source
default:
return null
}
},
async load(source) {
const parts = source.split('/')
switch (parts[0]) {
case ENTRY_PREFIX: {
const [, contentType, key] = parts
const schemaPromise = client.getContentType(contentType)
const entriesPromise = client.getEntries({
content_type: contentType,
'fields.key': key,
limit: 1,
include: 10,
})
const [schema, entries] = await Promise.all([
schemaPromise,
entriesPromise,
])
const item = await mapEntry(entries.items[0], schema)
return `export default ${JSON.stringify(item)}`
}
case ENTRIES_PREFIX: {
const [, contentType] = parts
const schemaPromise = client.getContentType(contentType)
const entriesPromise = client.getEntries({
content_type: contentType,
include: 10,
})
const [schema, entries] = await Promise.all([
schemaPromise,
entriesPromise,
])
let items = await Promise.all(
entries.items.map(entry => mapEntry(entry, schema))
)
if (items.length && typeof items[0].orderingIndex === 'number') {
items = items.sort(
(first, second) => first.orderingIndex - second.orderingIndex
)
}
return `export default ${JSON.stringify(items)}`
}
default:
return null
}
},
}
}