Skip to content

Commit

Permalink
fixes gatsbyjs#11085 handle imageReference nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
artbycrunk committed Jan 15, 2019
1 parent 5461cfb commit de9c732
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions packages/gatsby-remark-images/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ const {
imageWrapperClass,
} = require(`./constants`)
const visitWithParents = require(`unist-util-visit-parents`)
const getDefinitions = require('mdast-util-definitions');
const path = require(`path`)
const isRelativeUrl = require(`is-relative-url`)
const _ = require(`lodash`)
Expand Down Expand Up @@ -41,6 +42,9 @@ module.exports = (
node.type === `link`
)

// Get all the available definitions in the markdown tree
const definitions = getDefinitions(markdownAST);

// This will allow the use of html image tags
// const rawHtmlNodes = select(markdownAST, `html`)
let rawHtmlNodes = []
Expand All @@ -53,7 +57,7 @@ module.exports = (
// This will only work for markdown syntax image tags
let markdownImageNodes = []

visitWithParents(markdownAST, `image`, (node, ancestors) => {
visitWithParents(markdownAST, [`image`, `imageReference`], (node, ancestors) => {
const inLink = ancestors.some(findParentLinks)

markdownImageNodes.push({ node, inLink })
Expand Down Expand Up @@ -226,6 +230,12 @@ module.exports = (
markdownImageNodes.map(
({ node, inLink }) =>
new Promise(async (resolve, reject) => {
let refNode;
if (!node.hasOwnProperty('url') && node.hasOwnProperty('identifier')) {
//consider as imageReference node
refNode = node;
node = definitions(refNode.identifier);
}
const fileType = node.url.slice(-3)

// Ignore gifs as we can't process them,
Expand All @@ -242,7 +252,10 @@ module.exports = (
)

if (rawHTML) {
// Replace the image node with an inline HTML node.
// Replace the image or ref node with an inline HTML node.
if (refNode) {
node = refNode;
}
node.type = `html`
node.value = rawHTML
}
Expand Down

0 comments on commit de9c732

Please sign in to comment.