1
+ /* eslint-disable no-useless-escape */
1
2
import { isWebUri } from "valid-url"
2
3
import { fluid } from "gatsby-plugin-sharp"
3
4
import Img from "gatsby-image"
@@ -30,7 +31,7 @@ const getNodeEditLink = node => {
30
31
31
32
const findReferencedImageNodeIds = ( { nodeString, pluginOptions, node } ) => {
32
33
// if the lazyNodes plugin option is set we don't need to find
33
- // image node id's because those nodes will be fetched lazily in resolvers
34
+ // image node id's because those nodes will be fetched lazily in resolvers.
34
35
if ( pluginOptions . type . MediaItem . lazyNodes ) {
35
36
return [ ]
36
37
}
@@ -327,6 +328,17 @@ const getCheerioElementFromMatch = wpUrl => ({ match, tag = `img` }) => {
327
328
}
328
329
}
329
330
331
+ const getCheerioElementsFromMatches = ( { imgTagMatches, wpUrl } ) =>
332
+ imgTagMatches
333
+ . map ( getCheerioElementFromMatch ( wpUrl ) )
334
+ . filter ( ( { cheerioImg : { attribs } } ) => {
335
+ if ( ! attribs . src ) {
336
+ return false
337
+ }
338
+
339
+ return isWebUri ( encodeURI ( attribs . src ) )
340
+ } )
341
+
330
342
const getLargestSizeFromSizesAttribute = sizesString => {
331
343
const sizesStringsArray = sizesString . split ( `,` )
332
344
@@ -444,6 +456,28 @@ const cacheCreatedFileNodeBySrc = ({ node, src }) => {
444
456
}
445
457
}
446
458
459
+ const imgSrcRemoteFileRegex = / (?: s r c = \\ " ) ( (?: (?: h t t p s ? | f t p | f i l e ) : \/ \/ | w w w \. | f t p \. | \/ ) (?: [ ^ ' " ] ) * \. (?: j p e g | j p g | p n g | g i f | i c o | m p g | o g v | s v g | b m p | t i f | t i f f ) ) ( \? [ ^ \\ " \. ] * | ) (? = \\ " | | \. ) / gim
460
+
461
+ export const getImgSrcRemoteFileMatchesFromNodeString = nodeString =>
462
+ execall ( imgSrcRemoteFileRegex , nodeString ) . filter ( ( { subMatches } ) => {
463
+ // if our match is json encoded, that means it's inside a JSON
464
+ // encoded string field.
465
+ const isInJSON = subMatches [ 0 ] . includes ( `\\/\\/` )
466
+
467
+ // we shouldn't process encoded JSON, so skip this match if it's JSON
468
+ return ! isInJSON
469
+ } )
470
+
471
+ export const getImgTagMatchesWithUrl = ( { nodeString, wpUrl } ) =>
472
+ execall (
473
+ / < i m g ( [ \w \W ] + ?) [ \/ ] ? > / gim,
474
+ nodeString
475
+ // we don't want to match images inside pre
476
+ . replace ( / < p r e ( [ \w \W ] + ?) [ \/ ] ? > .* ( < \/ p r e > ) / gim, `` )
477
+ // and code tags, so temporarily remove those tags and everything inside them
478
+ . replace ( / < c o d e ( [ \w \W ] + ?) [ \/ ] ? > .* ( < \/ c o d e > ) / gim, `` )
479
+ ) . filter ( filterMatches ( wpUrl ) )
480
+
447
481
const replaceNodeHtmlImages = async ( {
448
482
nodeString,
449
483
node,
@@ -456,38 +490,15 @@ const replaceNodeHtmlImages = async ({
456
490
return nodeString
457
491
}
458
492
459
- const imgSrcRemoteFileRegex = / (?: s r c = \\ " ) ( (?: (?: h t t p s ? | f t p | f i l e ) : \/ \/ | w w w \. | f t p \. | \/ ) (?: \( [ - A - Z 0 - 9 + & @ # / % = ~ _ | $ ? ! : , . ] * \) | [ - A - Z 0 - 9 + & @ # / % = ~ _ | $ ? ! : , . ] ) * (?: \( [ - A - Z 0 - 9 + & @ # / % = ~ _ | $ ? ! : , . ] * \) | [ A - Z 0 - 9 + & @ # / % = ~ _ | $ ] ) \. (?: j p e g | j p g | p n g | g i f | i c o | m p g | o g v | s v g | b m p | t i f | t i f f ) ) ( \? [ ^ \\ " . ] * | ) (? = \\ " | | \. ) / gim
493
+ const imageUrlMatches = getImgSrcRemoteFileMatchesFromNodeString ( nodeString )
460
494
461
- const imageUrlMatches = execall ( imgSrcRemoteFileRegex , nodeString ) . filter (
462
- ( { subMatches } ) => {
463
- // if our match is json encoded, that means it's inside a JSON
464
- // encoded string field.
465
- const isInJSON = subMatches [ 0 ] . includes ( `\\/\\/` )
466
-
467
- // we shouldn't process encoded JSON, so skip this match if it's JSON
468
- return ! isInJSON
469
- }
470
- )
471
-
472
- const imgTagMatches = execall (
473
- / < i m g ( [ \w \W ] + ?) [ / ] ? > / gim,
474
- nodeString
475
- // we don't want to match images inside pre
476
- . replace ( / < p r e ( [ \w \W ] + ?) [ / ] ? > .* ( < \/ p r e > ) / gim, `` )
477
- // and code tags, so temporarily remove those tags and everything inside them
478
- . replace ( / < c o d e ( [ \w \W ] + ?) [ / ] ? > .* ( < \/ c o d e > ) / gim, `` )
479
- ) . filter ( filterMatches ( wpUrl ) )
495
+ const imgTagMatches = getImgTagMatchesWithUrl ( { nodeString, wpUrl } )
480
496
481
497
if ( imageUrlMatches . length && imgTagMatches . length ) {
482
- const cheerioImages = imgTagMatches
483
- . map ( getCheerioElementFromMatch ( wpUrl ) )
484
- . filter ( ( { cheerioImg : { attribs } } ) => {
485
- if ( ! attribs . src ) {
486
- return false
487
- }
488
-
489
- return isWebUri ( attribs . src )
490
- } )
498
+ const cheerioImages = getCheerioElementsFromMatches ( {
499
+ imgTagMatches,
500
+ wpUrl,
501
+ } )
491
502
492
503
const htmlMatchesToMediaItemNodesMap = await fetchNodeHtmlImageMediaItemNodes (
493
504
{
0 commit comments