Shadow queries
Shadow theme-ui index.js
I want to show the main page at sixian.li
and the blog page at sixian.li/blog
We need to sort the value based on its date in a descending order, e.g. From the latest post to the oldest one). Ghost has a different name for "date". To minimize modifications, I decided to change "published_at" to "date", so Ghost posts will be compatible with blog listing component.
import React from "react";
import Blog from "../../gatsby-theme-minimal-blog/components/blog";
// Recreate the blog template to use posts from ghost and MDX
// Rename "published_at" to "date" to normalize the result
const renameKey = (obj, oldKey, newKey) => {
if (obj[oldKey]) {
obj[newKey] = obj[oldKey];
delete obj[oldKey];
}
};
export default ({ data }) => {
const { allPost, allGhostPost } = data;
const combinedPosts = [...allPost.nodes, ...allGhostPost.nodes];
combinedPosts.map(post => renameKey(post, "published_at", "date"));
combinedPosts.sort((a, b) => new Date(b.date) - new Date(a.date));
// combinedPosts.nodes is an array of all posts
return <Blog posts={combinedPosts} />;
};
- Create pages for Ghost blog posts
gatsby-node.js
- Get reading_time for Ghost blog posts in
post-query
- Set up hooks for Ghost update with Netlify
- Add environment variables in Netlify because I don't want to expose my app API key
- Try to fix tags
mergeTwoResults
Another problem: how to handle posts from Ghost with existing tags? Expected result: Show the merged results in the tag page. Display all posts with the same tag no matter whatever the source is.
Solution:
The same as the homepage one. Combine posts from two sources with the same tag and sort by date. sortByDate
utility function
Some tags only exist at the Ghost side, so I need to create tags pages for them following /${basePath}/{$tagPath}/{tagName}
Get code injection from ghostPost.codeinjection_head
and wrap it in Helmet
This is tricky, we need helmet and gatsby-rss.js
import React from "react";
import { Helmet } from "react-helmet";
export default () => (
<Helmet>
<link
rel="stylesheet"
href="https://cdn.jsdelivr.net/npm/[email protected]/dist/katex.css"
crossorigin="anonymous"
/>
<script
defer
src="https://cdn.jsdelivr.net/npm/[email protected]/dist/katex.js"
crossorigin="anonymous"
></script>
<script
defer
src="https://cdn.jsdelivr.net/npm/[email protected]/dist/contrib/auto-render.min.js"
crossorigin="anonymous"
onload="renderMathInElement(document.body);"
></script>
<script
type="text/javascript"
async
src="https://cdnjs.cloudflare.com/ajax/libs/mathjax/2.7.3/MathJax.js?config=TeX-MML-AM_CHTML"
></script>
</Helmet>
);
medium zoom is not getting arguments from MDX plugin