如果在 _posts/ 目录中,图像不会出现

Image does not appear if in _posts/ directory

如果图像保存在 _posts/ 目录中,则不会出现在本地主机中,但如果存储在另一个目录中,则会出现,例如posts/。有谁知道为什么?

.md 文件:

![pressure plot]({{ site.baseurl }}/posts/fig/pressure-1.png)  # appears
![pressure plot]({{ site.baseurl }}/_posts/fig/pressure-1.png) # doesnt appear
来自 .md 文件的

.html 文件:

<img src="/my-awesome-site/posts/fig/pressure-1.png" alt="pressure plot" />
<img src="/my-awesome-site/_posts/fig/pressure-1.png" alt="pressure plot" />

config_yml:

baseurl: /my-awesome-site

jekyll serve 之后 _posts/ 终端中的错误消息:

ERROR `/my-awesome-site/_posts/fig/pressure-1.png' not found.

而且错误中的路径实际上是正确的(是的,图像可以在 _posts/fig/ 中找到,我通过保存在不同的目录中来测试),但不知何故图像没有出现.谁能解释一下?

我不认为所有这些都可以解释:, , , Jekyll,除非我不理解这整件事。

Jekyll's documentation 中查找,_posts 是 "reserved" 目录之一,由 Jekyll 解析以格式 YEAR-MONTH-DAY-title.MARKUP 命名的文件。除了特定的目录:

Every other directory and file except for those listed above [...] will be copied verbatim to the generated site.

因此,您的图像虽然存在于您的源中,但不会复制到生成的站点中(请参阅 _site/ 的内容),因为它位于目录 _posts 中,但未命名使用所需的格式。

我今天 运行 遇到了同样的问题。原来有一个名为 jekyll-postfiles 的有用插件可以解决这个问题!

Link to jekyll-postfiles

要安装 jekyll postiles,请将以下内容添加到您的 GEMFILE

source 'https://rubygems.org'

gem 'jekyll'

group :jekyll_plugins do
  gem 'jekyll-postfiles'
end

然后运行bundle。就是这样!

假设您有以下结构:

_posts
    |
    |--> folder_a
         |
         |--> year-month-date-filename1.md
         |--> picture.png
    |
    |--> folder_b
         |
         |--> year-month-date-filename2.md
         |--> another_picture.ong

然后在 filename1.md 中,您可以将图像调用为:

<img src="picture1.png" width="700" height="500" class="center">

这是一种非常巧妙的方式,而不是将图片放在单独的 assets IMO 文件夹中。

注意:Github 页面不支持此插件,将您的网站托管在 https://netlify.com 等支持第三方插件的服务上。