Skip to content

Instantly share code, notes, and snippets.

@ajmalafif
Last active January 12, 2021 09:32
Show Gist options
  • Save ajmalafif/4824ac8826590eadd88cac399e278fcf to your computer and use it in GitHub Desktop.
Save ajmalafif/4824ac8826590eadd88cac399e278fcf to your computer and use it in GitHub Desktop.
single rss.xml from 2 sources (MDX & Sanity.io)
// Load variables from `.env` as soon as possible
require('dotenv').config({
path: `.env.${process.env.NODE_ENV || 'development'}`
})
const clientConfig = require('./client-config')
const isProd = process.env.NODE_ENV === 'production'
// Portable Text Serialization
const PortableText = require("@sanity/block-content-to-html")
const imageUrlBuilder = require("@sanity/image-url")
const h = PortableText.h
const imageUrlFor = source => imageUrlBuilder(clientConfig.sanity).image(source)
// Helper functions for Portable Text
const { isFuture } = require("date-fns")
function filterOutDocsPublishedInTheFuture({ publishedAt }) {
return !isFuture(publishedAt);
}
function getBlogUrl(slug) {
return `/journal/${slug.current || slug}/`
}
module.exports = {
siteMetadata: {
title: "Ajmal Afif",
siteUrl: "https://afif.dev",
description: "🌐 of Ajmal Afif",
},
plugins: [
'gatsby-plugin-react-helmet',
{
resolve: 'gatsby-source-sanity',
options: {
...clientConfig.sanity,
token: process.env.SANITY_READ_TOKEN,
watchMode: !isProd,
overlayDrafts: !isProd
}
},
{
resolve: "gatsby-plugin-sanity-image",
options: {
...clientConfig.sanity,
defaultImageConfig: {
quality: 75,
fit: "max",
auto: "format",
},
},
},
{
resolve: `gatsby-source-filesystem`,
options: {
name: `pages`,
path: `${__dirname}/src/content`,
},
},
{
resolve: `gatsby-source-filesystem`,
options: {
name: `work`,
path: `${__dirname}/src/content/work`,
},
},
{
resolve: `gatsby-source-filesystem`,
options: {
path: `${__dirname}/src/pages`,
},
},
'gatsby-plugin-mdx',
'gatsby-plugin-emotion',
'gatsby-plugin-theme-ui',
{
resolve: `gatsby-plugin-feed`,
options: {
query: `
{
site {
siteMetadata {
title
description
siteUrl
site_url: siteUrl
}
}
}
`,
feeds: [
{
serialize: ({ query: { site, allSanityPost = [], allMdx } }) => {
const records = [];
allSanityPost.edges
.filter(({ node }) => filterOutDocsPublishedInTheFuture(node))
.filter(({ node }) => node.slug)
.forEach(({ node }) => {
const { title, publishedAt, slug, _rawBody, _rawExcerpt } = node;
const url = site.siteMetadata.siteUrl + getBlogUrl(slug.current);
records.push({
title: title,
date: publishedAt,
url,
guid: url,
description: PortableText({
blocks: _rawExcerpt,
serializers: {
types: {
code: ({ node }) =>
h("pre", h("code", { lang: node.language }.node.code))
}
}
}),
custom_elements: [
{
"content:encoded": PortableText({
blocks: _rawBody,
serializers: {
types: {
code: ({ node }) =>
h("pre", h("code", { lang: node.language }, node.code)),
mainImage: ({ node }) =>
h("img", {
src: imageUrlFor(node.asset).url()
}),
authorReference: ({ node }) => h("p", "Author: " + node.author.name),
}
}
})
}
]
});
});
allMdx.edges.forEach(edge => {
records.push(Object.assign({}, edge.node.frontmatter, {
description: edge.node.excerpt,
date: edge.node.frontmatter.date,
url: site.siteMetadata.siteUrl + edge.node.fields.slug,
guid: site.siteMetadata.siteUrl + edge.node.fields.slug,
custom_elements: [{ 'content:encoded': edge.node.html }],
}));
});
return records;
},
query: `
{
allSanityPost(sort: { fields: publishedAt, order: DESC }) {
edges {
node {
_rawExcerpt
_rawBody(resolveReferences: {maxDepth: 10})
title
publishedAt
slug {
current
}
}
}
}
allMdx(
limit: 1000,
sort: { order: DESC, fields: [frontmatter___date] },
filter: { frontmatter: { template: { regex: "/work-post/" } }}
) {
edges {
node {
fields {
slug
}
frontmatter {
title
date
}
html
}
}
}
}
`,
output: "/rss.xml",
title: "Ajmal Afif — RSS Feed",
}
]
}
},
]
}
@ajmalafif
Copy link
Author

credits to @dylanjha for figuring it out!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment