Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix source medium Plugin #2515

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions packages/gatsby-source-medium/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ something like [gatsby-source-rss](https://github.com/jondubin/gatsby-source-rss

## How to use


```javascript
// In your gatsby-config.js
plugins: [
Expand All @@ -22,6 +23,8 @@ plugins: [
}
]
```
###### PS.Note
if you wanna get user put username with `@`.

## How to query

Expand Down
38 changes: 15 additions & 23 deletions packages/gatsby-source-medium/src/gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,24 @@ exports.sourceNodes = async ({ boundActionCreators }, { username }) => {
const { createNode } = boundActionCreators

try {
const isPublication = username[0] !== `@` // username's start with `@` symbol
const result = await fetch(username)
const json = JSON.parse(strip(result.data))

const { posts } = json.payload
const collectionKeys = Object.keys(json.payload.references.Collection)
let importableResources = []
const userKeys = Object.keys(json.payload.references.User)

const importableResources = [
userKeys.map(key => json.payload.references.User[key]),
posts,
collectionKeys.map(key => json.payload.references.Collection[key]),
]
if(!isPublication){
const postKeys = Object.keys(json.payload.references.Post)
importableResources = [
userKeys.map(key => json.payload.references.User[key]),
postKeys.map(key => json.payload.references.Post[key]),
]
} else {
const posts = json.payload.posts
importableResources = [
posts,
userKeys.map(key => json.payload.references.User[key]),
]
}

const resources = Array.prototype.concat(...importableResources)
resources.map(resource => {
Expand All @@ -51,19 +57,6 @@ exports.sourceNodes = async ({ boundActionCreators }, { username }) => {
.update(JSON.stringify(resource))
.digest(`hex`)

const links =
resource.type === `Post`
? {
author___NODE: resource.creatorId,
}
: resource.type === `User`
? {
posts___NODE: posts
.filter(post => post.creatorId === resource.userId)
.map(post => post.id),
}
: {}

const node = Object.assign(
resource,
{
Expand All @@ -75,7 +68,6 @@ exports.sourceNodes = async ({ boundActionCreators }, { username }) => {
contentDigest: digest,
},
},
links
)

createNode(node)
Expand Down