1+ /* eslint-disable no-useless-escape */
12import { isWebUri } from "valid-url"
23import { fluid } from "gatsby-plugin-sharp"
34import Img from "gatsby-image"
@@ -30,7 +31,7 @@ const getNodeEditLink = node => {
3031
3132const findReferencedImageNodeIds = ( { nodeString, pluginOptions, node } ) => {
3233 // 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.
3435 if ( pluginOptions . type . MediaItem . lazyNodes ) {
3536 return [ ]
3637 }
@@ -327,6 +328,17 @@ const getCheerioElementFromMatch = wpUrl => ({ match, tag = `img` }) => {
327328 }
328329}
329330
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+
330342const getLargestSizeFromSizesAttribute = sizesString => {
331343 const sizesStringsArray = sizesString . split ( `,` )
332344
@@ -444,6 +456,28 @@ const cacheCreatedFileNodeBySrc = ({ node, src }) => {
444456 }
445457}
446458
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+
447481const replaceNodeHtmlImages = async ( {
448482 nodeString,
449483 node,
@@ -456,38 +490,15 @@ const replaceNodeHtmlImages = async ({
456490 return nodeString
457491 }
458492
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 )
460494
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 } )
480496
481497 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+ } )
491502
492503 const htmlMatchesToMediaItemNodesMap = await fetchNodeHtmlImageMediaItemNodes (
493504 {
0 commit comments