-
Notifications
You must be signed in to change notification settings - Fork 10.3k
/
index.js
222 lines (198 loc) · 6.68 KB
/
index.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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
const select = require(`unist-util-select`)
const path = require(`path`)
const isRelativeUrl = require(`is-relative-url`)
const _ = require(`lodash`)
const { sizes } = require(`gatsby-plugin-sharp`)
const Promise = require(`bluebird`)
const cheerio = require(`cheerio`)
const slash = require(`slash`)
// If the image is relative (not hosted elsewhere)
// 1. Find the image file
// 2. Find the image's size
// 3. Filter out any responsive image sizes that are greater than the image's width
// 4. Create the responsive images.
// 5. Set the html w/ aspect ratio helper.
module.exports = (
{ files, markdownNode, markdownAST, pathPrefix, getNode },
pluginOptions
) => {
const defaults = {
maxWidth: 650,
wrapperStyle: ``,
backgroundColor: `white`,
linkImagesToOriginal: true,
pathPrefix,
}
const options = _.defaults(pluginOptions, defaults)
// This will only work for markdown syntax image tags
const markdownImageNodes = select(markdownAST, `image`)
// This will also allow the use of html image tags
const rawHtmlNodes = select(markdownAST, `html`)
// Takes a node and generates the needed images and then returns
// the needed HTML replacement for the image
const generateImagesAndUpdateNode = async function(node, resolve) {
// Check if this markdownNode has a File parent. This plugin
// won't work if the image isn't hosted locally.
const parentNode = getNode(markdownNode.parent)
let imagePath
if (parentNode && parentNode.dir) {
imagePath = slash(path.join(parentNode.dir, node.url))
} else {
return null
}
const imageNode = _.find(files, file => {
if (file && file.absolutePath) {
return file.absolutePath === imagePath
}
return null
})
if (!imageNode || !imageNode.absolutePath) {
return resolve()
}
let responsiveSizesResult = await sizes({
file: imageNode,
args: options,
})
// Calculate the paddingBottom %
const ratio = `${1 / responsiveSizesResult.aspectRatio * 100}%`
const originalImg = responsiveSizesResult.originalImg
const fallbackSrc = responsiveSizesResult.src
const srcSet = responsiveSizesResult.srcSet
const presentationWidth = responsiveSizesResult.presentationWidth
// Generate default alt tag
const srcSplit = node.url.split(`/`)
const fileName = srcSplit[srcSplit.length - 1]
const fileNameNoExt = fileName.replace(/\.[^/.]+$/, ``)
const defaultAlt = fileNameNoExt.replace(/[^A-Z0-9]/gi, ` `)
// TODO
// Fade in images on load.
// https://www.perpetual-beta.org/weblog/silky-smooth-image-loading.html
// Construct new image node w/ aspect ratio placeholder
let rawHTML = `
<span
class="gatsby-resp-image-wrapper"
style="position: relative; display: block; ${
options.wrapperStyle
}; max-width: ${
presentationWidth
}px; margin-left: auto; margin-right: auto;"
>
<span
class="gatsby-resp-image-background-image"
style="padding-bottom: ${
ratio
}; position: relative; bottom: 0; left: 0; background-image: url('${
responsiveSizesResult.base64
}'); background-size: cover; display: block;"
>
<img
class="gatsby-resp-image-image"
style="width: 100%; height: 100%; margin: 0; vertical-align: middle; position: absolute; top: 0; left: 0; box-shadow: inset 0px 0px 0px 400px ${
options.backgroundColor
};"
alt="${node.alt ? node.alt : defaultAlt}"
title="${node.title ? node.title : ``}"
src="${fallbackSrc}"
srcset="${srcSet}"
sizes="${responsiveSizesResult.sizes}"
/>
</span>
</span>
`
// Make linking to original image optional.
if (options.linkImagesToOriginal) {
rawHTML = `
<a
class="gatsby-resp-image-link"
href="${originalImg}"
style="display: block"
target="_blank"
rel="noopener"
>
${rawHTML}
</a>
`
}
return rawHTML
}
return Promise.all(
// Simple because there is no nesting in markdown
markdownImageNodes.map(
node =>
new Promise(async (resolve, reject) => {
const fileType = node.url.slice(-3)
// Ignore gifs as we can't process them,
// svgs as they are already responsive by definition
if (
isRelativeUrl(node.url) &&
fileType !== `gif` &&
fileType !== `svg`
) {
const rawHTML = await generateImagesAndUpdateNode(node, resolve)
// Replace the image node with an inline HTML node.
node.type = `html`
node.value = rawHTML
return resolve(node)
} else {
// Image isn't relative so there's nothing for us to do.
return resolve()
}
})
)
).then(markdownImageNodes =>
// HTML image node stuff
Promise.all(
// Complex because HTML nodes can contain multiple images
rawHtmlNodes.map(
node =>
new Promise(async (resolve, reject) => {
if (!node.value) {
return resolve()
}
const $ = cheerio.load(node.value)
if ($(`img`).length === 0) {
// No img tags
return resolve()
}
let imageRefs = []
$(`img`).each(function() {
imageRefs.push($(this))
})
for (let thisImg of imageRefs) {
// Get the details we need.
let formattedImgTag = {}
formattedImgTag.url = thisImg.attr(`src`)
formattedImgTag.title = thisImg.attr(`title`)
formattedImgTag.alt = thisImg.attr(`alt`)
if (!formattedImgTag.url) {
return resolve()
}
const fileType = formattedImgTag.url.slice(-3)
// Ignore gifs as we can't process them,
// svgs as they are already responsive by definition
if (
isRelativeUrl(formattedImgTag.url) &&
fileType !== `gif` &&
fileType !== `svg`
) {
const rawHTML = await generateImagesAndUpdateNode(
formattedImgTag,
resolve
)
// Replace the image string
thisImg.replaceWith(rawHTML)
} else {
return resolve()
}
}
// Replace the image node with an inline HTML node.
node.type = `html`
node.value = $(`body`).html() // fix for cheerio v1
return resolve(node)
})
)
).then(htmlImageNodes =>
markdownImageNodes.concat(htmlImageNodes).filter(node => !!node)
)
)
}