gatsby-source-ghost
Advanced tools
Comparing version 2.1.2 to 2.2.0
16
api.js
const axios = require('axios'); | ||
const qs = require('qs'); | ||
const printError = (...args) => console.error('\n', ...args); // eslint-disable-line no-console | ||
const reset = '\x1b[0m'; | ||
const printWarning = (...args) => console.error('\n\x1b[33m', ...args, reset); // eslint-disable-line no-console | ||
const printError = (...args) => console.error('\n\x1b[41m\x1b[37m', ...args, reset); // eslint-disable-line no-console | ||
const printAPIError = ({message, context, errorType}) => console.error('\n\x1b[31m', errorType + ':', message, context || '', reset); // eslint-disable-line no-console | ||
@@ -18,3 +21,3 @@ module.exports.fetchAllPosts = (options) => { | ||
if (options.apiUrl.substring(0, 8) !== 'https://') { | ||
printError('Ghost apiUrl should be served over HTTPS, are you sure you want:', options.apiUrl, '?'); | ||
printWarning('Ghost apiUrl should be served over HTTPS, are you sure you want:', options.apiUrl, '?'); | ||
} | ||
@@ -36,4 +39,9 @@ | ||
.then(res => res.data.posts) | ||
.catch((err) => { | ||
printError('Error:', err); | ||
.catch((error) => { | ||
if (error.response && error.response.data && error.response.data.errors) { | ||
printAPIError(error.response.data.errors[0]); | ||
} else { | ||
printError('Error:', error.message); | ||
} | ||
printError('Unable to fetch data from your Ghost API. Perhaps your credentials or apiUrl are incorrect?'); | ||
@@ -40,0 +48,0 @@ process.exit(1); |
const GhostAPI = require('./api'); | ||
const _ = require('lodash'); | ||
const {PostNode, PageNode, TagNode, AuthorNode} = require('./nodes'); | ||
const getPostCount = function getPostCount(posts, taxonomie) { | ||
let allTaxonomies = []; | ||
// Get all possible taxonimies that are being used and | ||
// create a usable array | ||
posts.forEach(post => allTaxonomies.push(post[taxonomie] || [])); | ||
allTaxonomies = _.flatten(allTaxonomies); | ||
allTaxonomies = _.transform(_.uniqBy(allTaxonomies, item => item.id), (result, item) => { | ||
(result[item.slug] || (result[item.slug] = 0)); | ||
}, {}); | ||
// Now collect all post slugs per taxonomie | ||
posts.forEach((post) => { | ||
if (post[taxonomie] && post[taxonomie].length) { | ||
post[taxonomie].forEach((item) => { | ||
allTaxonomies[item.slug] += 1; | ||
}); | ||
} | ||
}); | ||
return allTaxonomies; | ||
}; | ||
exports.sourceNodes = ({boundActionCreators}, configOptions) => { | ||
@@ -10,2 +34,5 @@ const {createNode} = boundActionCreators; | ||
.then((posts) => { | ||
const tagPostCount = getPostCount(posts, 'tags'); | ||
const authorPostCount = getPostCount(posts, 'authors'); | ||
posts.forEach((post) => { | ||
@@ -20,2 +47,5 @@ if (post.page) { | ||
post.tags.forEach((tag) => { | ||
// find the number of posts that have this tag | ||
tag.postCount = tagPostCount[tag.slug] || 0; | ||
createNode(TagNode(tag)); | ||
@@ -27,2 +57,5 @@ }); | ||
post.authors.forEach((author) => { | ||
// find the number of posts that include this author | ||
author.postCount = authorPostCount[author.slug] || 0; | ||
createNode(AuthorNode(author)); | ||
@@ -29,0 +62,0 @@ }); |
{ | ||
"name": "gatsby-source-ghost", | ||
"version": "2.1.2", | ||
"version": "2.2.0", | ||
"description": "Gatsby source plugin for building websites using the Ghost API as a data source.", | ||
@@ -24,2 +24,3 @@ "repository": "git@github.com:TryGhost/gatsby-plugin-ghost.git", | ||
"eslint-plugin-ghost": "0.0.26", | ||
"lodash": "^4.17.11", | ||
"mocha": "5.2.0", | ||
@@ -26,0 +27,0 @@ "should": "13.2.3", |
@@ -76,2 +76,17 @@ # Gatsby Source Ghost | ||
A common but tricky example of filtering posts by tag, can be achieved like this (Gatsby v2+): | ||
``` | ||
{ | ||
allGhostPost(filter: {tags: {elemMatch {slug: {eq: $slug}}}}) { | ||
edges { | ||
node { | ||
slug | ||
... | ||
} | ||
} | ||
} | ||
} | ||
``` | ||
You can query Page nodes created from Ghost like the following: | ||
@@ -133,2 +148,2 @@ | ||
Copyright (c) 2018 Ghost Foundation - Released under the [MIT license](LICENSE). | ||
Copyright (c) 2018-2019 Ghost Foundation - Released under the [MIT license](LICENSE). |
@@ -21,7 +21,7 @@ // Switch these lines once there are useful utils | ||
{slug: 'welcome-to-ghost', page: false, tags: [ | ||
{slug: 'getting-started'}, | ||
{slug: 'hash-feature-img'} | ||
{slug: 'getting-started', id: '1'}, | ||
{slug: 'hash-feature-img', id: '2'} | ||
], authors: [ | ||
{name: 'Ghost Writer'}, | ||
{name: 'Ghost Author'} | ||
{name: 'Ghost Writer', id: '1'}, | ||
{name: 'Ghost Author', id: '2'} | ||
]}, | ||
@@ -28,0 +28,0 @@ {slug: 'about', page: true} |
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
12543
188
148
6
1