Skip to content

Commit

Permalink
feat: add general params that will be used to fetch from swiftype
Browse files Browse the repository at this point in the history
  • Loading branch information
jerelmiller committed Aug 13, 2020
1 parent 27c6fe9 commit 27c1481
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 4 deletions.
6 changes: 6 additions & 0 deletions gatsby-config.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,12 @@ module.exports = {
resolve: 'gatsby-source-swiftype',
options: {
file: `${__dirname}/src/data/related-pages.json`,
engineKey: 'Ad9HfGjDw4GRkcmJjUut',
pageLimit: 5,
getPath: ({ node }) => node.frontmatter.path,
filterNode: ({ node }) =>
node.internal.type === 'Mdx' &&
node.frontmatter.template === 'GuideTemplate',
},
},
'gatsby-plugin-sass',
Expand Down
36 changes: 32 additions & 4 deletions plugins/gatsby-source-swiftype/gatsby-node.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
const fs = require('fs');

const appendTrailingSlash = (url) =>
url.pathname.endsWith('/') ? url : new URL(`${url.pathname}/`, url.origin);

const stripTrailingSlash = (url) =>
url.pathname.endsWith('/')
? new URL(url.replace(/\/$/, ''), url.origin)
: url;

exports.onPreBootstrap = ({ reporter }, pluginOptions) => {
const { file } = pluginOptions;

Expand All @@ -10,10 +18,30 @@ exports.onPreBootstrap = ({ reporter }, pluginOptions) => {
}
};

exports.onCreatePage = async ({ page }, pluginOptions) => {
const includePage = pluginOptions.filterPage || (() => false);
exports.onCreateNode = async ({ node, getNodesByType }, pluginOptions) => {
const {
filterNode = () => false,
getPath,
pageLimit,
engineKey,
} = pluginOptions;

if (includePage({ page })) {
console.log(`Fetching ${page.path}`);
if (!filterNode({ node })) {
return;
}

const [{ siteMetadata }] = getNodesByType('Site');
const { siteUrl } = siteMetadata;
const pathname = getPath({ node });
const url = new URL(pathname, siteUrl);

const params = {
engine_key: engineKey,
per_page: pageLimit,
filters: {
page: {
url: [`!${appendTrailingSlash(url)}`, `!${stripTrailingSlash(url)}`],
},
},
};
};

0 comments on commit 27c1481

Please sign in to comment.