如何解决将中等文章导入 gatsby
我正在尝试将我的媒体提要集成到 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
}
}
}
}
}
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。