Skip to content

Commit

Permalink
feat: add tags to search query and write data to file in plugin
Browse files Browse the repository at this point in the history
  • Loading branch information
jerelmiller committed Aug 13, 2020
1 parent 7a13ad7 commit 9e3b486
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 13 deletions.
16 changes: 13 additions & 3 deletions gatsby-config.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
const quote = (str) => `"${str}"`;

module.exports = {
siteMetadata: {
title: 'New Relic Developers',
Expand Down Expand Up @@ -48,9 +50,17 @@ module.exports = {
file: `${__dirname}/src/data/related-pages.json`,
engineKey: 'Ad9HfGjDw4GRkcmJjUut',
pageLimit: 5,
getParams: ({ node }) => ({
path: node.frontmatter.path,
}),
getParams: ({ node }) => {
const { tags, title } = node.frontmatter;

return {
path: node.frontmatter.path,
q: tags ? tags.map(quote).join(' OR ') : title,
search_fields: {
page: ['tags^10', 'body^5', 'title^1.5', '*'],
},
};
},
filterNode: ({ node }) =>
node.internal.type === 'Mdx' &&
node.frontmatter.template === 'GuideTemplate',
Expand Down
20 changes: 10 additions & 10 deletions plugins/gatsby-source-swiftype/gatsby-node.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
const fs = require('fs');

const data = {};

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

Expand All @@ -8,16 +10,6 @@ const stripTrailingSlash = (url) =>
? new URL(url.replace(/\/$/, ''), url.origin)
: url;

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

if (!fs.existsSync(file)) {
reporter.info('gatsby-source-swiftype: Creating data file');

fs.writeFileSync(file, '{}');
}
};

exports.onCreateNode = async ({ node, getNodesByType }, pluginOptions) => {
const {
filterNode = () => false,
Expand Down Expand Up @@ -52,4 +44,12 @@ exports.onCreateNode = async ({ node, getNodesByType }, pluginOptions) => {
},
},
};

data[pathname] = allParams;
};

exports.onPostBootstrap = (_, pluginOptions) => {
const { file } = pluginOptions;

fs.writeFileSync(file, JSON.stringify(data, null, 2), { flag: 'w' });
};

0 comments on commit 9e3b486

Please sign in to comment.