问题描述
我正在尝试将我的媒体提要集成到 gatsby 中,并且只想选择几篇文章 - 没有最新的文章。我能够使用此代码获取最近的三篇文章:
index.config
mediumRSSFeed:
"https://api.RSS2json.com/v1/api.json?RSS_url=https%3A%2F%2Fmedium.com%2FFeed%2F%40maxgraze",shownArticles: 3,
文章.js
const Articles = () => {
const MAX_ARTICLES = shownArticles
const { isIntrodone,darkMode } = useContext(Context).state
const [articles,setArticles] = useState()
const articlesControls = useAnimation()
// Load and display articles after the splashScreen sequence is done
useEffect(() => {
const loadArticles = async () => {
if (isIntrodone) {
await articlesControls.start({
opacity: 1,y: 0,transition: { delay: 1 },})
fetch(mediumRSSFeed,{ headers: { Accept: "application/json" } })
.then(res => res.json())
// Feed also contains comments,therefore we filter for articles only
.then(data => data.items.filter(item => item.categories.length > 0))
.then(newArticles => newArticles.slice(0,MAX_ARTICLES))
.then(articles => setArticles(articles))
.catch(error => console.log(error))
}
}
loadArticles()
},[isIntrodone,articlesControls,MAX_ARTICLES])
但我希望使用 gatsby-source-medium 查询特定文章。但是,它只返回 4 个(甚至不是最近的那个)。
有没有办法通过 gatsby-source-medium 获取我所有的文章?否则,有没有办法“硬编码”我想要的文章?我不确定如何使用 RSS Feed api 进行过滤。谢谢你的帮助!
解决方法
正如您所建议的,使用 gatsby-source-medium
有一种更原生的方式,但文档缺乏很好的示例。
// In your gatsby-config.js
plugins: [
{
resolve: `gatsby-source-medium`,options: {
username: `username/publication`,},]
更新:
这似乎是 Medium 源的一个已知错误,我们对我们的项目无能为力。欲知更多详情: gatsbyjs/gatsby#22491
像下面这样的查询将收集预览图像中用户的所有帖子:
query {
allMediumPost(sort: { fields: [createdAt],order: DESC }) {
edges {
node {
id
title
virtuals {
subtitle
previewImage {
imageId
}
}
author {
name
}
}
}
}
}