你如何在README.rst中链接到文档文件夹中的图片?

4
我很难找到一种在 README.rst 中使用图片的方法,这样既可以在 github 上显示,也可以在 PyPI 上显示。目前我正在使用以下标记:
.. image:: https://github.com/thebjorn/pydeps/blob/master/docs/_static/pydeps.svg

这在 Github 上看起来很棒(https://github.com/thebjorn/pydeps),但在 PyPI 上显示为损坏链接(https://pypi.org/project/pydeps/1.6.0/)。

这是否可能,还是我需要在其他地方托管这些图片?

1个回答

5

您需要使用raw.githubusercontent.com而不是github.com

.. image:: https://github.com/thebjorn/pydeps/blob/master/docs/_static/pydeps-pylib.svg

只有在Github本身上才能正确呈现,而

.. image:: https://raw.githubusercontent.com/thebjorn/pydeps/master/docs/_static/pydeps-pylib.svg?sanitize=true

将在任何网站上呈现。


第一次尝试(已损坏和删除)


第二次尝试:

  1. 正确替换URL(同时删除路径中的blob部分 - 是我的失误!),
  2. 在每个被替换的URL后面添加?sanitize=true

$ sed -i '' 's,.. image:: https://github.com/\(.*\)/blob/\(.*\).svg,.. image:: https://raw.githubusercontent.com/\1/\2.svg?sanitize=true,g' README.rst

测试它在Github上在TestPyPI上, 都看起来不错。


替代方案:使用RawGit

另一种可能性是使用RawGit,但缺点是它是第三方服务,因此当它停止运行时,图像不会被渲染,尽管可用。然而,为了完整起见:

$ sed -i '' 's,.. image:: https://github.com/\(.*\)/blob/\(.*\)$,.. image:: https://cdn.rawgit.com/\1/\2,g' README.rst

似乎不起作用(在GitHub上显示为损坏的图像)。起初我以为是由于内容类型为text/html,但我发现.png图像也存在问题(例如https://raw.githubusercontent.com/divio/django-cms/blob/develop/docs/_static/screen1.png). - thebjorn
实际上,如果您从我的先前评论中删除blob,则适用于.png文件。看起来github将.svg文件转换为数据URL,但.rst似乎不喜欢... :-( - thebjorn
现在已经得到了 sed 的一行命令,请再次验证。 - hoefling
很高兴能够帮助! - hoefling

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接