This is the multi-page printable view of this section. Click here to print.

Return to the regular view of this page.

内容管理(Content-Management)🔥

记录如何编写 content/<section_name>/**/<content_name>.md 文件。 (涉及东西多,篇幅长)

content 的目录结构

└── content
    ├── _index.md          // [home]            <- https://example.com/ **
    ├── about.md           // [page]            <- https://example.com/about/
    ├── posts               
    |   ├── _index.md      // [section]         <- https://example.com/posts/ **         
    |   ├── firstpost.md   // [page]            <- https://example.com/posts/firstpost/
    |   ├── happy           
    |   |   ├── _index.md  // [section]         <- https://example.com/posts/happy/ **
    |   |   └── ness.md    // [page]            <- https://example.com/posts/happy/ness/
    |   └── secondpost.md  // [page]            <- https://example.com/posts/secondpost/
    └── quote   
        ├── _index.md      // [section]         <- https://example.com/quote/ **           
        ├── first.md       // [page]            <- https://example.com/quote/first/
        └── second.md      // [page]            <- https://example.com/quote/second/

// hugo默认生成的页面, 没有对应的markdown文章
分类列表页面               // [taxonomyTerm]    <- https://example.com/categories/  **
某个分类下的所有文章的列表  // [taxonomy]        <- https://example.com/categories/one-category  **
标签列表页面               // [taxonomyTerm]    <- https://example.com/tags/  **
某个标签下的所有文章的列表  // [taxonomy]        <- https://example.com/tags/one-tag  **

中括号[]中标注的是页面的kind属性, 他们整体上分为两类: single(单页面 - page) 和 list(列表页 - home, section, taxonomyTerm, taxonomy).

1 - 前置元数据(Frontmatter)

设置文章的标题、创建日期、描述等。

官方文档:

使用

page

1# content/test/_index.md
2title: "My amazing new section"
3weight: 1
4type: docs
5description: >
6    A special section with a docs layout.    

list/section

1# content/news/_index.md
2title: "Latest News"
3linkTitle: "News"
4menu:
5main:
6    weight: 30
7cascade:
8    - type: "blog"
1# content/sections01/_index.md 
2
3# section 子文章"块"显示
4simple_list: true # 列表显示
5no_list: true # 不显示

链接 linking

  • ref —— 绝对路径
  • relref —— 相对路径
 1# hostname=localhost
 2# port=1313
 3# baseurl=/
 4.
 5└── content
 6    ├── document1.md
 7    ├── about
 8    |   ├── _index.md
 9    |   └── document1.md
10    ├── pages
11    |   ├── current.md <----------- current
12    |   ├── document1.md
13    |   └── document2.md // has anchor #anchor
14    ├── products
15    |   └── index.md
16    └── blog
17        └── my-post.md
18
19e.g. 
20{{< relref "document1.md" >}} --> 可以忽略后缀
21{{< relref "document1" >}} --> 输出: /content/pages/document/
22{{< ref "document1" >}}    --> 输出: //localhost:1313/content/pages/document/
23{{< ref "document2.md#anchor" >}}
24{{< ref "document1" >}}        --> 找到: /content/pages/document1.md
25{{< ref "/document1" >}}       --> 找到: /content/document1.md
26{{< ref "/about/document1" >}} --> 找到: /content/aboug/document1.md

路径不需要绝对"准确",hugo会(按照优先级)自动匹配最适合的结果。

优先级:

  1. ./*
  2. ./**
  3. content/*
  4. content/**

2 - 简码(Shortcode)

简码,顾名思义"用于简化操作的代码块"。

我们在文章内容 content/**.md 可以重复多处调用这种代码块来减少重复性操作。

区分: 简码(Shortcode)和模板变量与函数(Template variable and function)

模板的设计初衷是简化操作,避免重复编码。但是如果(表达作者思想的)文章中出现大量(页面渲染和逻辑判断相关的)模板语句会使文章管理变得混乱。

为了避免上述问题,Hugo 提出简码的概念: 文章作者不能直接在文章中使用模板语言,但是可以使用模板语言封装后的"简码(Shortcode)"。简码封装了模板语言涉及的html和逻辑判断。使用简码只需要传入必要参数即可。

  • 模板变量调用: {{ .Title }}
  • 模板函数调用: {{ dict "title" .Title content" "hello!" | jsonify }}
  • 简码调用: {{< highlight go >}} hello {{< /highlight >}}

总结: 模板只能在 layouts/ 中使用;简码只能在 content/ 中使用。这样就划分了两个角色: 编写模板的主题作者和编写文章的内容作者!

官方文档:

调用

有两种调用形式:

  • {{< ... >}} —— 不对传入参数进行处理
  • {{% ... %}} —— 对传入参数进行加工,如进行markdown标志的解析

有三种参数传递形式:

  • {{< shortcodename >}} —— 不传参
  • {{< shortcodename parameters >}} —— 传入 "string" 或者 key="value" 形式的参数 (简码中会通过 .Get 函数获取传入参数)
  • {{< shortcodename >}} inner string {{< /shortcodename >}} —— 用两个简码标记包裹字符串 (简码中会通过 .Inner 变量获取包裹的字符串)

简码"不解析"

有的时候,我们就希望简码字符直接以字符形式显示,像 “{{< string >}}” 这样。这时我们只需要将内容用 /* ... */ 包裹,如 “{{</* string */>}}” 写在 .md 文件中。

1{{</* string */>}} --解析--> {{< string >}} 
2{{%/* string */%}} --解析--> {{% string %}} 

3 - 分类法(Taxonomy)

给文章打标签,便于检索。

默认有 tagscategories 两个。添加更多只需要在配置文件配置:

1taxonomies:
2  tag: tags
3  category: categories
4  project: projects

默认会把全部标签显示在右侧,称为 “标签云(taxonomyCloud)"。 可以配置只显示一部分标签:

 1params:
 2  taxonomy:
 3    taxonomyCloud:
 4      - projects    # remove all entries
 5      - tags        # to hide taxonomy clouds
 6    taxonomyCloudTitle:   # if used, must have the same
 7      - Our Projects      # number of entries as taxonomyCloud
 8      - Tag Cloud
 9    taxonomyPageHeader:
10      - tags        # remove all entries
11      - categories  # to hide taxonomy clouds