首页 文章

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

提问于
浏览
1

我有一个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 集合的目录结构如下所示:course001 / index.md figures / figure01.jpg

生成的HTML如下所示:

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

而不是预期的:

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

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

谢谢你的帮助!

1 回答

  • 0

    我不知道这是不是你真正想要的 . 无论如何,我建议你为你的项目建一个新结构,假设你要创建很多课程,每个课程都有自己的讲座 . 如果您还有子讲座,代码很容易扩展和处理 .

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

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

    您必须同时创建 _courses_lectures 文件夹 .

    _lecture 文件夹中,您应该放置讲座的降价文件 . 每个讲座都应该有一个它所属的课程的标签 . 我还为讲座添加了一个标签,以方便路径的处理 . 这是带有图像的讲义文件的示例 .

    ---
    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 来包含模板 . 以下是一个示例,基本上与 page 布局相同 .

    ---
    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>
    

    现在你需要按课程分组 . 不幸的是,正如我之前所说,Jekyll不处理嵌套集合,因此我们在每个讲座中检查标签 course 以查看每门课程的讲座内容 . 因此,如果您想在每个课程页面的开头键入一个讲座列表,那么您应该有一个类似于 _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>
    

    课程的典型降价文件应存储在 _courses 中,并且类似于

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

    剩下的唯一一个是显示课程列表的页面 . 您可以使用此内容在项目文件夹中创建 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文件夹中 . 这是唯一的方法(我想,但也许我'm wrong). I' m不仅仅指代主目录中的 assets 文件夹,而且指代集合目录中的 assets 文件夹 .

相关问题