# 使用 Discourse API 从 Github .md 文件上传图片

**URL:** <https://meta.discourse.org/t/upload-images-from-github-md-file-using-discourse-api/198661>\
**Category:** Development\
**Created:** [2021年七月30日 10:42 UTC](https://meta.discourse.org/t/upload-images-from-github-md-file-using-discourse-api/198661 "2021-07-30T10:42:18Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![marielavd](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/marielavd/32/215907_2.png) [@marielavd](https://meta.discourse.org/u/marielavd)\
**Post date:** [2021年七月30日 10:42 UTC](https://meta.discourse.org/t/upload-images-from-github-md-file-using-discourse-api/198661/1 "2021-07-30T10:42:18Z")

</div>

大家好，

我使用 Node.js 工作流通过 Discourse API 将 GitHub 仓库与 Discourse 连接起来。每当有人在仓库中推送 .md 文件时，系统会自动通过 POST 请求 https://{defaultHost}/posts.json 创建 Discourse 主题；如果主题已存在，则通过 PUT 请求 https://{defaultHost}/posts/{id}.json 进行更新。

我在图片处理上遇到了问题：在 GitHub 的 .md 文件中包含图片有几种方式。以下两种情况运行正常，图片已成功上传至 Discourse：

- 图片托管在 [https://user-images.githubusercontent.com/](https://user-images.githubusercontent.com/) 上
- 图片直接上传到仓库，并在图片链接末尾附加 ?raw=true  
[https://github.com/[username]/[reponame]/blob/[branch]/image.jpg?raw=true](https://github.com/%5Busername%5D/%5Breponame%5D/blob/%5Bbranch%5D/image.jpg?raw=true)

但以下两种情况无法正常工作：

- 使用了相对链接  
relative/path/to/img.jpg
- 缺少 ?raw=true 部分  
[https://github.com/[username]/[reponame]/blob/[branch]/image.jpg](https://github.com/%5Busername%5D/%5Breponame%5D/blob/%5Bbranch%5D/image.jpg)

请问有什么方法可以确保 .md 文件中的所有图片都能上传到 Discourse 吗？

---

<div class="post-metadata">

**Author:** ![pfaffman](https://sea3.discourse-cdn.com/meta/user_avatar/meta.discourse.org/pfaffman/32/120154_2.png) [@pfaffman](https://meta.discourse.org/u/pfaffman)\
**Post date:** [2021年七月30日 12:26 UTC](https://meta.discourse.org/t/upload-images-from-github-md-file-using-discourse-api/198661/2 "2021-07-30T12:26:17Z")

</div>

如果你的脚本将来自 GitHub 的原始 Markdown 推送到 Discourse，那么你需要让脚本找到这些相对链接并添加 GitHub 域名。
