# gatsby-wpgraphql-inline-images

> A Gatsby plugin to turn remote inline images into local static images

Latest version **0.2.5** (published 2020-01-17) · MIT license · 0 weekly downloads

## Install

```sh
npm install gatsby-wpgraphql-inline-images
pnpm add gatsby-wpgraphql-inline-images
yarn add gatsby-wpgraphql-inline-images
bun add gatsby-wpgraphql-inline-images
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.2.5 |
| Published | 2020-01-17 |
| First published | 2019-07-21 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 13 |
| Unpacked size | 24 KB |
| Known vulnerabilities | 0 (+2 in 2 direct dependencies) |
| Install scripts | no |
| GitHub stars | 33 |
| Author | Andrey Shalashov |
| Maintainers | progital |
| Keywords | gatsby, gatsby-plugin, image, wordpress, wpgraphql |

## Links

- npm: https://www.npmjs.com/package/gatsby-wpgraphql-inline-images
- Repository: https://github.com/progital/gatsby-wpgraphql-inline-images
- Homepage: https://github.com/progital/gatsby-wpgraphql-inline-images#readme
- Issues: https://github.com/progital/gatsby-wpgraphql-inline-images/issues
- npm.io page: https://npm.io/package/gatsby-wpgraphql-inline-images

## Dependencies (13)

- [urijs](https://npm.io/package/urijs.md) ^1.19.1
- [gatsby](https://npm.io/package/gatsby.md) ^2.0.0
- [lodash](https://npm.io/package/lodash.md) ^4.17.11
- [cheerio](https://npm.io/package/cheerio.md) ^1.0.0-rc.3
- [theme-ui](https://npm.io/package/theme-ui.md) ^0.2.14
- [gatsby-image](https://npm.io/package/gatsby-image.md) ^2.1.4
- [@emotion/core](https://npm.io/package/@emotion/core.md) ^10.0.14
- [@mdx-js/react](https://npm.io/package/@mdx-js/react.md) ^1.0.27
- [@babel/runtime](https://npm.io/package/@babel/runtime.md) ^7.0.0
- [html-react-parser](https://npm.io/package/html-react-parser.md) ^0.9.0
- [gatsby-plugin-sharp](https://npm.io/package/gatsby-plugin-sharp.md) ^2.0.0
- [gatsby-source-filesystem](https://npm.io/package/gatsby-source-filesystem.md) ^2.0.0
- [gatsby-transformer-sharp](https://npm.io/package/gatsby-transformer-sharp.md) ^2.2.4

## Alternatives

- [apollo-link-http-common](https://npm.io/package/apollo-link-http-common.md) — 879.0K weekly downloads
- [react-relay](https://npm.io/package/react-relay.md) — 336.8K weekly downloads
- [relay-test-utils](https://npm.io/package/relay-test-utils.md) — 181.6K weekly downloads
- [@vendure/core](https://npm.io/package/@vendure/core.md) — 14.8K weekly downloads
- [@pnpm/deps.graph-sequencer](https://npm.io/package/@pnpm/deps.graph-sequencer.md) — 13.4K weekly downloads

## Recent versions

- 0.2.5 (latest) — 2020-01-17
- 0.2.4 — 2020-01-15
- 0.2.3 — 2019-10-21
- 0.2.2 — 2019-10-05
- 0.2.1 — 2019-10-05
- 0.2.0 — 2019-09-25
- 0.1.6 — 2019-09-13
- 0.1.5 — 2019-09-11
- 0.1.4 — 2019-08-02
- 0.1.3 — 2019-07-27
- 0.1.2 — 2019-07-21
- 0.1.1 — 2019-07-21
- 0.1.0 — 2019-07-21

## README

# gatsby-wpgraphql-inline-images

## Description

Source plugins don't process links and images in blocks of text (i.e. post contents) which makes sourcing from CMS such as WordPress problematic. This plugin solves that for content sourced from WordPress using GraphQL by doing the following:

- Downloads images and other files to Gatsby `static` folder
- Replaces `<a>` linking to site's pages with `<Link>` component
- Replaces `<img>` with Gatsby `<Img>` component leveraging all of the [gatsby-image](https://www.gatsbyjs.org/docs/using-gatsby-image/) rich functionality

A [major update](https://github.com/gatsbyjs/gatsby/issues/19292) is in the works for the WordPress source plugin and WPGraphQL. This plugin will be radically changed or even become redundant after V4 is completed. 

### Dependencies

This plugin processes WordPress content sourced with GraphQL. Therefore you must use `gatsby-source-graphql` and your source WordPress site must use [WPGraphQL](https://github.com/wp-graphql/wp-graphql).

_Attention:_ does not work with `gatsby-source-wordpress`.

## How to install

```bash
yarn add gatsby-wpgraphql-inline-images
```

```javascript
{
  resolve: 'gatsby-wpgraphql-inline-images',
  options: {
    wordPressUrl: 'https://mydomain.com/',
    uploadsUrl: 'https://mydomain.com/wp-content/uploads/',
    processPostTypes: ['Page', 'Post', 'CustomPost'],
    graphqlTypeName: 'WPGraphQL',
    httpHeaders: {
      Authorization: `Bearer ${process.env.GITHUB_TOKEN}`,
    }
  },
},
```

## Available options

`wordPressUrl` and `uploadsUrl` contain URLs of the source WordPress site and it's `uploads` folder respectively.

`processPostTypes` determines which post types to process. You can include [custom post types](https://docs.wpgraphql.com/getting-started/custom-post-types) as defined in WPGraphQL.

`graphqlTypeName` should contain the same `typeName` used in `gatsby-source-graphql` parameters.

`generateWebp` _(boolean)_ adds [WebP images](https://www.gatsbyjs.org/docs/gatsby-image/#about-withwebp).

`httpHeaders` Adds extra http headers to download request if passed in.

`debugOutput` _(boolean)_ Outputs extra debug messages.

## How do I use this plugin?

Downloading and optimizing images is done automatically via resolvers. You need to include `uri` in all queries that will be processed by the plugin otherwise they will be ignored.

```
query GET_PAGES {
  wpgraphql {
    pages {
      nodes {
        uri
        content
      }
    }
  }
}
```

There is an additional step of processing content that must be added manually to a page template. This additional processing replaces remote urls with links to downloaded files in Gatsby's static folder.

```javascript
import contentParser from 'gatsby-wpgraphql-inline-images';
```

replace `<div dangerouslySetInnerHTML={{ __html: content }} />` with this

```javascript
<div>{contentParser({ content }, { wordPressUrl, uploadsUrl })}</div>
```

Where `content` is the original HTML content and URLs should use the same values as in the options above. `contenParser` returns React object.

### Featured image

The recommended handling of `featuredImage` and WordPress media in general is described by [henrikwirth](https://github.com/henrikwirth) in [this article](https://dev.to/nevernull/gatsby-with-wpgraphql-acf-and-gatbsy-image-72m) and [this gist](https://gist.github.com/henrikwirth/4ba900b7f89b9e28ec81497466b12710).

### WordPress galleries

WordPress galleries may need some additional styling applied and this was intentionally left out of the scope of this plugin. Emotion [Global Styles](https://emotion.sh/docs/globals) may be used or just import sass/css file.

```css
.gallery {
  display: flex;
  flex-direction: row;
  flex-wrap: wrap;
}
.gallery-item {
  margin-right: 10px;
}
```

## Gatsby themes support

Inserted `<Img>` components have `variant: 'styles.SourcedImage'` applied to them.

## Examples of usage

I'm going to use [gatsby-wpgraphql-blog-example](https://github.com/wp-graphql/gatsby-wpgraphql-blog-example) as a starter and it will source data from a demo site at [yourdomain.dev](https://yourdomain.dev/).

Add this plugin to the `gatsby-config.js`

```javascript
{
  resolve: 'gatsby-wpgraphql-inline-images',
  options: {
    wordPressUrl: `https://yourdomain.dev/`,
    uploadsUrl: `https://yourdomain.dev/wp-content/uploads/`,
    processPostTypes: ["Page", "Post"],
    graphqlTypeName: 'WPGraphQL',
  },
},
```

Change `url` in `gatsby-source-graphql` options to `https://yourdomain.dev/graphql`

Page templates are stored in `src/templates`. Let's modify `post.js` as an example.

Importing `contentParser`

```javascript
import contentParser from 'gatsby-wpgraphql-inline-images';
```

For simplicty's sake I'm just going to add URLs directly in the template.

```javascript
const pluginOptions = {
  wordPressUrl: `https://yourdomain.dev/`,
  uploadsUrl: `https://yourdomain.dev/wp-content/uploads/`,
};
```

and replace `dangerouslySetInnerHTML` with this

```javascript
<div>{contentParser({ content }, pluginOptions)}</div>
```

The modified example starter is available at [github.com/progital/gatsby-wpgraphql-blog-example](https://github.com/progital/gatsby-wpgraphql-blog-example).

## How to contribute

This is a WIP and any contribution, feedback and PRs are very welcome. Issues is a preferred way of submitting feedback, but you can also email to [andrey@progital.io](mailto:andrey@progital.io).

---
_Source: https://npm.io/package/gatsby-wpgraphql-inline-images · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
