【问题标题】:importing medium articles into gatsby将中型文章导入 gatsby
【发布时间】:2021-04-08 00:03:24
【问题描述】:

我正在尝试将我的媒体提要集成到 gatsby 中,并且只想选择几篇文章 - 没有最新的文章。我能够使用此代码获得最近的三篇文章:

index.config

 mediumRssFeed:
    "https://api.rss2json.com/v1/api.json?rss_url=https%3A%2F%2Fmedium.com%2Ffeed%2F%40maxgraze",
  shownArticles: 3,

Articles.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 进行过滤。谢谢你的帮助!

【问题讨论】:

    标签: graphql rss gatsby medium.com gatsby-plugin


    【解决方案1】:

    正如您所建议的,使用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
            }
          }
        }
      }
    }
    

    【讨论】:

    • 非常感谢您的帮助!你能给我举个例子,说明我会在“YourPublication”中放什么吗?文章标题?我不确定那应该代表什么。再次感谢!
    • 为了澄清,我尝试指定我发布的媒体博客,但没有运气(@maxgraze/nightingale),并且出现这种类型错误:'error TypeError: Cannot read property 'references' of undefined。'
    • 你试过localhost:8000/___graphql中的查询吗?
    • 通过添加我列出的配置 (@maxgraze/nightingale) 我无法运行该应用程序,因为我收到了该错误,因此我无法使用您列出的查询。
    • github.com/gatsbyjs/gatsby/issues/22491 这似乎是一个已知的错误
    猜你喜欢
    • 2015-05-25
    • 2020-08-25
    • 2020-09-24
    • 1970-01-01
    • 1970-01-01
    • 2018-10-13
    • 2020-07-24
    • 1970-01-01
    • 2021-04-17
    相关资源
    最近更新 更多