jekyll - 为什么 Jekyll 集合中的图像会出现在站点根目录中?

标签 jekyll

我有一个 Jekyll 项目设置,使用多个集合将文档分组在一起。我的 _config.yml 如下所示:

collections:
  course001:
    output: true
    title: "Theme structure"
    relative_url: "/theme-structure"    # The subpath of the the site, 
    permalink: /theme-structure/:name

course001集合的目录结构如下所示: 类(class)001/ 索引.md 人物/ 图01.jpg

生成的 HTML 如下所示:

_site/
    figure01.jpg
theme-structure/
    index.html

而不是预期的:

_site/
    theme-structure/
        index.html
        figure01.jpg

为什么图像出现在站点根文件夹中?由于可能发生名称冲突,我不希望图像出现在根文件夹中。这只是图像的问题,而不是最终到达预期位置的文档的问题。

感谢您的帮助!

最佳答案

我不知道这是否是您真正想要的。不管怎样,我为你的项目建议一个新的结构,假设你要创建很多类(class),每个类(class)都有自己的讲座。如果您还有子讲座,则代码很容易扩展和处理它们。

首先是您的 _config.yml 文件。您应该为类(class)和讲座创建两个集合(不幸的是 Jekyll 不处理子集合)。

collections:
  courses:
    output: true
  lectures:
    output: true

您必须创建 _courses_lectures 文件夹。

_lecture 文件夹中,您应该放置讲座的 Markdown 文件。每个讲座都应该有一个它所属类(class)的标签。我还为讲座添加了一个标签,以方便路径的处理。这是带有图像的讲座文件的示例。

---
layout: lecture
title: My first lecture
course: 001
lecture: my-first-lecture
---
This is my first lecture!!

![fish][fish]

[fish]: assets/img/{{page.lecture}}/fish.png

如您所见,您的 _lecture 文件夹中需要一个文件夹 assets。您可以使用 _layout 文件夹中的文件 lecture.html 来包含您的模板。下面只是一个示例,基本上与页面布局相同。

---
layout: default
---
<article class="post">

  <header class="post-header">
    <h1 class="post-title">{{ page.title | escape }}</h1>
  </header>

  <div class="post-content">    
    {{ content }}
  </div>

</article>

现在您需要按类(class)对它们进行分组。不幸的是,正如我之前所说,Jekyll 不处理嵌套集合,因此我们检查每个讲座中的标签 course 来查看每个类(class)中的讲座是什么。因此,如果您想在每个类(class)页面的开头输入讲座列表,那么您应该有一个类似于 _layout/course.html 的文件

---
layout: default
---
<article class="post">

  <header class="post-header">
    <h1 class="post-title">{{ page.title | escape }}</h1>
  </header>

  <div class="post-content">

    <ol>
    {% for lecture in site.lectures %}
    {% if lecture.course == page.course %}
      <li><a href="{{lecture.url}}">{{lecture.title}}</a></li>
    {% endif %}
    {% endfor %}
    </ol>

    {{ content }}
  </div>

</article>

类(class)的典型 Markdown 文件应存储在 _courses 中,并且类似于

---
layout: course
title: My first course
course: 001
---
This is my first course!!

剩下的就是一个显示类(class)列表的页面。您可以使用此内容在项目文件夹中创建一个 coursed.md 文件。

---
layout: page
title: Courses
permalink: /courses/
---

These are my courses:

<ol>
{% for course in site.courses %}
  <li><a href="{{course.url}}">{{course.title}}</a></li>
{% endfor %}
</ol>

我知道这可能超出了您在问题中所问的内容,并且您仍然想知道为什么会有这种奇怪的行为。我认为这是因为您应该将构建站点中所需的文件存储在 Assets 文件夹中。这是唯一的方法(我想,但也许我错了)。我不仅指主目录中的 assets 文件夹,还指集合目录中的 assets 文件夹。

关于jekyll - 为什么 Jekyll 集合中的图像会出现在站点根目录中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50980759/

相关文章:

ruby - 处理 Jekyll 内容以将任何帖子标题的第一次出现替换为具有该标题的帖子的超链接

ruby - 运行 Jekyll 时出错 – 库未加载 :/usr/local/opt/ruby/lib/libruby. 3.0.dylib (LoadError)

ruby - 杰基尔服务错误 : no implicit conversion of nil into String

css - Jekyll 和自定义 CSS

jekyll - 在不使用自定义插件的情况下在 Jekyll 中格式化数字

html - 在 Jekyll 构建中使用 @font-face webfonts

ruby - Octopress 错误 - rake 预览、观察或生成

jekyll - Baseurl 不适用于 jekyll 帖子。

jekyll - 如何阻止 "jekyll build"覆盖输出目录中的现有文件?

github - Jekyll 站点不会显示在 GitHub 页面上