使用 Discourse 作为无头 CMS 的 Gatsby 插件

我开发了一个插件,不是针对 Discourse,而是针对 Gatsby。该插件从 Discourse 实例中获取主题列表,并将其转换为 Gatsby 节点。它简化了将 Discourse 用作无头 CMS 的过程。

我们在一个项目中使用了它,来构建一个微博客和一个日历,当论坛中添加新内容时,日历会自动更新。

以下是微博客示例:

来源:https://foodshift.se/t/forenade-inkop-nyheter-blogg/290/9
结果:https://forenadeinkop.se/blogg

以下是日历示例:

来源:https://foodshift.se/tags/c/events/13/forenade-inkop/l/agenda
结果:https://forenadeinkop.se(页面底部)

希望其他人也能觉得它有用!


npm 版本 npm 下载量

gatsby-source-discourse-topic-list 帮助你从 Discourse 讨论论坛平台获取主题列表,并将其转换为 Gatsby 节点。

除了 Discourse API 提供的主题列表外,该插件还会获取每个主题的原始文本。

入门指南

  1. 使用 yarnnpm 安装该包

yarn add gatsby-source-discourse-topic-list

  1. 在 gatsby-config.js 中添加至插件列表
module.exports = {
    plugins: [
        {
            resolve: "gatsby-source-discourse-topic-list",
            options: {
                url: "https://my-discourse-server.com"
                endPoint: 'top.json',
            }
        }
    ]
};

选项

名称 类型 描述
url 对象或字符串 必填。 你的 Discourse 实例的 URL(字符串格式)。如果你有分别用于开发和生产的两个不同 API,可以定义一个包含 productiondevelopment 键的对象。
endPoint 字符串 必填。 任何返回 topic_list 的 Discourse API 端点。
rootKey 字符串 可选。 为你的 API 命名。

Discourse API 端点示例

某个分类下的最新主题:

options: {
    url: "https://meta.discourse.org"
    endPoint: 'c/1.json',
}

按标签过滤的某个分类下的最新主题:

options: {
    url: "https://meta.discourse.org"
    endPoint: 'tags/c/bug/1/pr-welcome.json',
}

某个分类下的热门主题:

options: {
    url: "https://meta.discourse.org"
    endPoint: 'c/support/6/l/top.json',
}

查看 Discourse API 文档 以获取完整的端点列表。

示例结果

注意新增的 raw 字段,其中包含完整的未处理帖子主题文本。

{
  "users": [
    {
      "id": 0,
      "username": "string",
      "avatar_template": "string"
    }
  ],
  "topic_list": {
    "can_create_topic": true,
    "draft": {},
    "draft_key": "string",
    "draft_sequence": 0,
    "for_period": "string",
    "per_page": 0,
    "topics": [
      {
        "id": 0,
        "title": "string",
        "fancy_title": "string",
        "slug": "string",
        "raw": "The full unprocessed topic text in markdown format", 
        ...
      }
    ]
  }
}

多个数据源?多实例化即可!

如果你希望在项目中获取多个端点的数据,只需多次实例化该插件。请确保为每个实例设置不同的 rootKey

致谢

本插件基于 Andreas Faust 开发的优秀项目 gatsby-source-custom-api 构建。:folded_hands:

贡献

我们非常欢迎各种形式的贡献。
欢迎提交 Bug、功能请求和 Pull Request。

:heart: 如果这个插件对你有帮助,请在 GitHub 上给它点个星。

看起来不错。这能否与私密分类中的主题一起使用?我指的是将 Discourse 用作无头 CMS 的场景。

目前还没有,但这是个不错的主意,添加起来并不困难。访问私有内容需要身份验证。可以在 Gatsby 配置中添加用户名和 API 密钥。