HUGO
Menu
GitHub 87548 stars Mastodon

目录结构

Hugo 目录结构概述。

每个 Hugo 项目都是一个目录,包含贡献于网站内容、结构、行为和展示的多个子目录。

站点骨架

创建新站点时,Hugo 会生成项目骨架。例如,此命令:

hugo new site my-site

创建此目录结构:

my-site/
├── archetypes/
│   └── default.md
├── assets/
├── content/
├── data/
├── i18n/
├── layouts/
├── static/
├── themes/
└── hugo.toml         <-- 站点配置

根据需求,您可能希望将站点配置组织到子目录中:

my-site/
├── archetypes/
│   └── default.md
├── assets/
├── config/           <-- 站点配置
│   └── _default/
│       └── hugo.toml
├── content/
├── data/
├── i18n/
├── layouts/
├── static/
└── themes/

构建站点时,Hugo 会创建 public 目录,通常还会创建 resources 目录:

my-site/
├── archetypes/
│   └── default.md
├── assets/
├── config/       
│   └── _default/
│       └── hugo.toml
├── content/
├── data/
├── i18n/
├── layouts/
├── public/       <-- 构建站点时创建
├── resources/    <-- 构建站点时创建
├── static/
└── themes/

目录说明

每个子目录都对网站的内容、结构、行为或展示有所贡献。

archetypes
archetypes 目录包含新内容的模板。详见 archetypes
assets
assets 目录包含全局资源,通常通过资源管道处理。这包括图片、CSS、Sass、JavaScript 和 TypeScript 等资源。详见 Hugo Pipes
config
config 目录包含站点配置,可能分为多个子目录和文件。对于配置简单或不需要在不同环境中表现不同的项目,在项目根目录中使用单个配置文件 hugo.toml 就足够了。详见 配置目录
content
content 目录包含构成网站内容的标记文件(通常是 Markdown)和页面资源。详见 内容组织
data
data 目录包含数据文件(JSON、TOML、YAML 或 XML),用于增强内容、配置、本地化和导航。详见 数据源
i18n
i18n 目录包含多语言站点的翻译表。详见 多语言
layouts
layouts 目录包含将内容、数据和资源转换为完整网站的模板。详见 模板
public
public 目录包含运行 hugohugo server 命令时生成的已发布网站。详见 构建站点
resources
resources 目录包含 Hugo 资源管道的缓存输出,在运行 hugohugo server 命令时生成。默认情况下,此缓存目录包括 CSS 和图片。Hugo 会根据需要重新创建此目录及其内容。
static
static 目录包含构建站点时将复制到 public 目录的文件。例如:favicon.icorobots.txt 和验证网站所有权的文件。在引入 页面捆绑资源管道 之前,static 目录也用于存放图片、CSS 和 JavaScript 文件。
themes
themes 目录包含一个或多个 主题,每个主题都有自己的子目录。

联合文件系统

Hugo 创建联合文件系统,允许您将两个或多个目录挂载到同一位置。例如,假设您的主目录在一个目录中包含 Hugo 项目,在另一个目录中包含共享内容:

home/
└── user/
    ├── my-site/            
    │   ├── content/
    │   │   ├── books/
    │   │   │   ├── _index.md
    │   │   │   ├── book-1.md
    │   │   │   └── book-2.md
    │   │   └── _index.md
    │   ├── themes/
    │   │   └── my-theme/
    │   └── hugo.toml
    └── shared-content/     
        └── films/
            ├── _index.md
            ├── film-1.md
            └── film-2.md

您可以使用挂载在构建站点时包含共享内容。在站点配置中:

module:
  mounts:
  - source: content
    target: content
  - source: /home/user/shared-content
    target: content
[module]
  [[module.mounts]]
    source = 'content'
    target = 'content'
  [[module.mounts]]
    source = '/home/user/shared-content'
    target = 'content'
{
   "module": {
      "mounts": [
         {
            "source": "content",
            "target": "content"
         },
         {
            "source": "/home/user/shared-content",
            "target": "content"
         }
      ]
   }
}

当一个目录叠加在另一个目录之上时,您必须挂载这两个目录。

Hugo 不遵循符号链接。如果您需要符号链接提供的功能,请改用 Hugo 的联合文件系统。

挂载后,联合文件系统具有此结构:

home/
└── user/
    └── my-site/
        ├── content/
        │   ├── books/
        │   │   ├── _index.md
        │   │   ├── book-1.md
        │   │   └── book-2.md
        │   ├── films/
        │   │   ├── _index.md
        │   │   ├── film-1.md
        │   │   └── film-2.md
        │   └── _index.md
        ├── themes/
        │   └── my-theme/
        └── hugo.toml

当两个或多个文件具有相同路径时,优先顺序遵循挂载顺序。例如,如果共享内容目录包含 books/book-1.md,它将被忽略,因为项目的 content 目录首先被挂载。

您可以将目录挂载到 archetypesassetscontentdatai18nlayoutsstatic。详见 挂载

您还可以使用 Hugo Modules 从 Git 仓库挂载目录。详见 Hugo Modules

主题骨架

创建新主题时,Hugo 会生成一个功能齐全的主题骨架。例如,此命令:

hugo new theme my-theme

创建此目录结构(未显示子目录):

my-theme/
├── archetypes/
├── assets/
├── content/
├── data/
├── i18n/
├── layouts/
├── static/
└── hugo.toml

使用上述联合文件系统,Hugo 将这些目录中的每一个挂载到项目中的相应位置。当两个文件具有相同路径时,项目目录中的文件优先。这允许您通过将副本放在项目目录中的相同位置来覆盖主题的模板。

如果您同时使用两个或多个主题或模块的组件,并且发生路径冲突,则第一个挂载优先。


Last updated: January 1, 0001
Improve this page