Skip to content

Commit 6efd3c3

Browse files
committed
refactor code duplication
1 parent 4f0067b commit 6efd3c3

File tree

1 file changed

+42
-104
lines changed
  • packages/gatsby-remark-copy-linked-files/src

1 file changed

+42
-104
lines changed

packages/gatsby-remark-copy-linked-files/src/index.js

Lines changed: 42 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -206,127 +206,65 @@ module.exports = (
206206
visit(markdownAST, `html`, node => {
207207
const $ = cheerio.load(node.value)
208208

209-
// Handle Images
210-
const imageRefs = []
211-
$(`img`).each(function() {
212-
try {
213-
if (isRelativeUrl($(this).attr(`src`))) {
214-
imageRefs.push($(this))
215-
}
216-
} catch (err) {
217-
// Ignore
218-
}
219-
})
220-
221-
for (let thisImg of imageRefs) {
209+
function processUrl({ url }) {
222210
try {
223-
const ext = thisImg
224-
.attr(`src`)
225-
.split(`.`)
226-
.pop()
227-
if (!options.ignoreFileExtensions.includes(ext)) {
228-
generateImagesAndUpdateNode(thisImg, node)
229-
}
230-
} catch (err) {
231-
// Ignore
232-
}
233-
}
234-
235-
// Handle video tags.
236-
const videoRefs = []
237-
$(`video source, video`).each(function() {
238-
try {
239-
if (isRelativeUrl($(this).attr(`src`))) {
240-
videoRefs.push($(this))
241-
}
242-
} catch (err) {
243-
// Ignore
244-
}
245-
})
246-
247-
for (let thisVideo of videoRefs) {
248-
try {
249-
const ext = thisVideo
250-
.attr(`src`)
251-
.split(`.`)
252-
.pop()
211+
const ext = url.split(`.`).pop()
253212
if (!options.ignoreFileExtensions.includes(ext)) {
254213
// The link object will be modified to the new location so we'll
255214
// use that data to update our ref
256-
const link = { url: thisVideo.attr(`src`) }
215+
const link = { url }
257216
visitor(link)
258-
node.value = node.value.replace(
259-
new RegExp(thisVideo.attr(`src`), `g`),
260-
link.url
261-
)
217+
node.value = node.value.replace(new RegExp(url, `g`), link.url)
262218
}
263219
} catch (err) {
264220
// Ignore
265221
}
266222
}
267223

268-
// Handle audio tags.
269-
const audioRefs = []
270-
$(`audio source`).each(function() {
271-
try {
272-
if (isRelativeUrl($(this).attr(`src`))) {
273-
audioRefs.push($(this))
274-
}
275-
} catch (err) {
276-
// Ignore
277-
}
278-
})
279-
280-
for (let thisAudio of audioRefs) {
281-
try {
282-
const ext = thisAudio
283-
.attr(`src`)
284-
.split(`.`)
285-
.pop()
286-
if (!options.ignoreFileExtensions.includes(ext)) {
287-
const link = { url: thisAudio.attr(`src`) }
288-
visitor(link)
289-
node.value = node.value.replace(
290-
new RegExp(thisAudio.attr(`src`), `g`),
291-
link.url
292-
)
293-
}
294-
} catch (err) {
295-
// Ignore
296-
}
224+
// extracts all elements that have the provided url attribute
225+
function extractUrlAttributeAndElement(selection, attribute) {
226+
return (
227+
selection
228+
// extract the elements that have the attribute
229+
.map(function() {
230+
const element = $(this)
231+
const url = $(this).attr(attribute)
232+
if (url && isRelativeUrl(url)) {
233+
return { url, element }
234+
}
235+
return undefined
236+
})
237+
// cheerio object -> array
238+
.toArray()
239+
// filter out empty or undefined values
240+
.filter(Boolean)
241+
)
297242
}
298243

299-
// Handle a tags.
300-
const aRefs = []
301-
$(`a`).each(function() {
302-
try {
303-
if (isRelativeUrl($(this).attr(`href`))) {
304-
aRefs.push($(this))
244+
// Handle Images
245+
extractUrlAttributeAndElement($(`img`), `src`).forEach(
246+
({ url, element }) => {
247+
try {
248+
const ext = url.split(`.`).pop()
249+
if (!options.ignoreFileExtensions.includes(ext)) {
250+
generateImagesAndUpdateNode(element, node)
251+
}
252+
} catch (err) {
253+
// Ignore
305254
}
306-
} catch (err) {
307-
// Ignore
308255
}
309-
})
256+
)
310257

311-
for (let thisATag of aRefs) {
312-
try {
313-
const ext = thisATag
314-
.attr(`href`)
315-
.split(`.`)
316-
.pop()
317-
if (!options.ignoreFileExtensions.includes(ext)) {
318-
const link = { url: thisATag.attr(`href`) }
319-
visitor(link)
258+
// Handle video tags.
259+
extractUrlAttributeAndElement($(`video source, video`), `src`).forEach(
260+
processUrl
261+
)
320262

321-
node.value = node.value.replace(
322-
new RegExp(thisATag.attr(`href`), `g`),
323-
link.url
324-
)
325-
}
326-
} catch (err) {
327-
// Ignore
328-
}
329-
}
263+
// Handle audio tags.
264+
extractUrlAttributeAndElement($(`audio source`), `src`).forEach(processUrl)
265+
266+
// Handle a tags.
267+
extractUrlAttributeAndElement($(`a`), `href`).forEach(processUrl)
330268

331269
return
332270
})

0 commit comments

Comments
 (0)