Skip to content

Commit 59935ba

Browse files
committed
docs(example): share include assets across languages
1 parent e2ff2c3 commit 59935ba

6 files changed

Lines changed: 28 additions & 45 deletions

File tree

File renamed without changes.
File renamed without changes.
Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,12 @@ Content that appears on one page belongs on that page.
2020
`include` takes one required parameter, `file`:
2121

2222
```markdown {title="Source"}
23-
{{</* include file="parts/install-oink.en.md" */>}}
23+
{{</* include file="parts/install-oink.md" */>}}
2424
```
2525

26-
The file it pulls in is ordinary Markdown living in the same page bundle:
26+
The file it pulls in is ordinary Markdown living under `assets/`:
2727

28-
````markdown {title="content/docs/include/parts/install-oink.en.md"}
28+
````markdown {title="assets/parts/install-oink.md"}
2929
Installing OINK into an existing Hugo site takes three commands:
3030

3131
```sh
@@ -43,7 +43,7 @@ The current release is {{</* param version */>}}.
4343
The result is what you would get by writing it here: the code block has its copy
4444
button and the callout is a callout.
4545

46-
{{< include file="parts/install-oink.en.md" >}}
46+
{{< include file="parts/install-oink.md" >}}
4747

4848
The file that gets included is not a page of its own: it is absent from the
4949
sidebar, it takes no part in translation pairing, and it has no URL.
@@ -56,19 +56,19 @@ sidebar, it takes no part in translation pairing, and it has no URL.
5656
| --- | --- | --- |
5757
| 1 | A page resource — a file in this page's bundle | `file="config.yaml"` |
5858
| 2 | A global resource under `assets/` | `file="snippets/dsn.txt"` |
59-
| 3 | A file under `content/`: a leading `/` is the content root, otherwise relative to the page's directory | `file="parts/install-oink.en.md"`, `file="/shared/notice.md"` |
59+
| 3 | A file under `content/`: a leading `/` is the content root, otherwise relative to the page's directory | `file="notes/caveat.md"`, `file="/shared/notice.md"` |
6060

6161
Missing in all three, the build fails; nothing is emitted as a placeholder. A
6262
`..` in the path fails the build too: include reads from `content/` and
6363
`assets/` and nowhere else.
6464

65-
For a Markdown fragment, write the file's real name on disk, language suffix
66-
included — `parts/install-oink.en.md`, `parts/install-oink.zh.md`. A bare
67-
`parts/install-oink.md` also builds, but it matches step 1: Hugo attaches a
68-
language-suffixed resource under its stripped name, so `include` receives
69-
already-rendered HTML instead of the source, and `<div class="td-code">` turns
70-
up in the Markdown output. Non-Markdown files (`.yaml`, `.sh`, `.txt`) do not
71-
have this distinction — both paths read the same bytes.
65+
A Markdown fragment is read as source, so write the file's real name on disk.
66+
One trap belongs to step 1 alone: Hugo attaches a language-suffixed page
67+
resource such as `notice.zh.md` under its stripped name, so asking a bundle for
68+
`notice.md` hands `include` already-rendered HTML instead of the source, and
69+
`<div class="td-code">` turns up in the Markdown output. Under `assets/` and
70+
`content/` the name you write is the file you get. Non-Markdown files
71+
(`.yaml`, `.sh`, `.txt`) never have this distinction.
7272

7373
## Including code files {#code}
7474

@@ -225,8 +225,9 @@ Any other parameter name fails the build, with the file and line in the error.
225225
`collapse`). For per-platform variants, write two fragments and use
226226
[tabs](/docs/tabs/).
227227
- Fragment languages are yours to maintain: `include` does no language
228-
fallback. A Chinese page includes a Chinese fragment, an English page an
229-
English one, and the two files sit side by side.
228+
fallback and takes the exact path you write. Share one fragment across
229+
languages — this page's Chinese translation includes the same English file —
230+
or write one per language and point each page at its own.
230231
- `param` prints scalars only: structured data — version matrices, download
231232
lists — belongs in `data/` and is rendered by the matching component.
232233
- `comment` is not "unpublish for now": the content is discarded on every build.

exampleSite/content/docs/include/index.zh.md renamed to exampleSite/content/docs/include.zh.md

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,13 @@ params:
1515
`include` 只有一个必填参数 `file`
1616

1717
```markdown {title="源码"}
18-
{{</* include file="parts/install-oink.zh.md" */>}}
18+
{{</* include file="parts/install-oink.md" */>}}
1919
```
2020

21-
被引的文件是一段普通 Markdown,与本页放在同一个页面包里
21+
被引的文件是一段普通 Markdown,放在 `assets/` 下。中英两个版本引的是同一份英文片段
2222

23-
````markdown {title="content/docs/include/parts/install-oink.zh.md"}
24-
OINK 安装到一个已有的 Hugo 站点,三条命令:
23+
````markdown {title="assets/parts/install-oink.md"}
24+
Installing OINK into an existing Hugo site takes three commands:
2525

2626
```sh
2727
hugo mod init github.com/you/your-site
@@ -30,14 +30,14 @@ hugo server
3030
```
3131

3232
> [!NOTE]
33-
> `hugo mod get` 需要本机安装 Go;用离线归档或 submodule 时不需要。
33+
> `hugo mod get` needs Go on the machine; an offline archive or a submodule does not.
3434
35-
当前发布版本是 {{</* param version */>}}
35+
The current release is {{</* param version */>}}.
3636
````
3737

3838
渲染结果与写在本页里相同:代码块有复制按钮,提示块是提示块。
3939

40-
{{< include file="parts/install-oink.zh.md" >}}
40+
{{< include file="parts/install-oink.md" >}}
4141

4242
被引的文件不是一篇独立页面:它不出现在侧栏、不参与翻译配对、没有自己的 URL。
4343

@@ -48,26 +48,26 @@ hugo server
4848
| --- | --- | --- |
4949
| 1 | 当前页面的页面资源(页面包里的文件) | `file="config.yaml"` |
5050
| 2 | 全局资源 `assets/` 下的文件 | `file="snippets/dsn.txt"` |
51-
| 3 | `content/` 下的文件:`/` 开头是内容根目录,否则相对当前页面所在目录 | `file="parts/install-oink.zh.md"``file="/shared/notice.zh.md"` |
51+
| 3 | `content/` 下的文件:`/` 开头是内容根目录,否则相对当前页面所在目录 | `file="notes/caveat.md"``file="/shared/notice.md"` |
5252

5353
三处都找不到时构建失败,不输出占位内容。路径里含 `..` 也让构建失败:引用只能在 `content/``assets/` 中取文件。
5454

55-
Markdown 片段时写文件在磁盘上的真名,中文片段带 `.zh.`,如 `parts/install-oink.zh.md`。写成 `parts/install-oink.md` 也能构建通过,但会命中第 1 步:Hugo `.zh.md` 资源按去掉语言后缀的名字挂在中文页上,此时 `include` 得到的是已渲染的 HTML 而不是源码,Markdown 输出里会出现 `<div class="td-code">`。非 Markdown 文件(`.yaml``.sh``.txt`)没有这个区别,两条路径取到同一份原文
55+
Markdown 片段按源码读入,写文件在磁盘上的真名即可。有一个坑只属于第 1 步:Hugo 把带语言后缀的页面资源(如 `notice.zh.md`)按去掉后缀的名字挂在页面上,于是向页面包要 `notice.md` 时,`include` 拿到的是已渲染的 HTML 而不是源码,Markdown 输出里会出现 `<div class="td-code">``assets/``content/` 下写什么名字就取什么文件。非 Markdown 文件(`.yaml``.sh``.txt`)没有这个区别。
5656

5757
## 引入代码文件 {#code}
5858

5959
`code=true` 让文件按代码块渲染,`lang=` 指定高亮语言。引用仓库里的真实配置文件,文档与实际文件不会不一致。
6060

6161
```markdown {title="源码"}
62-
{{</* include file="parts/module.zh.yml" code=true lang="yaml" */>}}
62+
{{</* include file="parts/module.yml" code=true lang="yaml" */>}}
6363
```
6464

65-
{{< include file="parts/module.zh.yml" code=true lang="yaml" >}}
65+
{{< include file="parts/module.yml" code=true lang="yaml" >}}
6666

6767
代码块与围栏走同一条渲染管线:高亮、行号、复制按钮都有。围栏属性(`title=``collapse``hl_lines=`)传不进来,需要它们时把文件内容写成普通[代码块](/zh/docs/code/)
6868

6969
## 片段内容 {#snippet-content}
70-
片段是页面级 Markdown,在当前页面的上下文里渲染:提示块、表格、列表、图片、步骤与 shortcode 都可以用。上面那段片段结尾的「当前发布版本是 v0.5.0」,是片段里的 `{{</* param version */>}}` 在本页展开的结果。
70+
片段是页面级 Markdown,在当前页面的上下文里渲染:提示块、表格、列表、图片、步骤与 shortcode 都可以用。上面那段片段结尾的「The current release is v0.5.0」,是片段里的 `{{</* param version */>}}` 在本页展开的结果 —— 片段是英文的,`param` 取的却是本页的上下文
7171

7272
一个片段被两页引用时,两页各自渲染一遍,各自生成标题锚点与代码块 ID,互不冲突。
7373

@@ -180,7 +180,7 @@ Markdown 输出里片段是源码而不是 HTML,片段里的 shortcode 保持
180180
## 限制与常见问题 {#limits}
181181

182182
- `include` 不是模板:不能向片段传变量、不能条件引入、不能给引入的代码块加围栏属性(`title=``collapse`)。按平台分版本时写两个片段配[标签页](/zh/docs/tabs/)
183-
- 片段的语言要自己维护:`include` 不做语言回退。中文页引中文片段,英文页引英文片段,两份文件并列存放(`install-oink.zh.md``install-oink.md`
183+
- 片段的语言要自己维护:`include` 不做语言回退,写什么路径就取什么文件。可以像本页一样中英共用同一份英文片段,也可以每种语言各写一份、各自指向自己那份
184184
- `param` 只打印标量:结构化数据(版本矩阵、下载列表)用 `data/` 目录里的数据配对应组件渲染。
185185
- `comment` 不是「暂时不发布」:内容每次构建都被丢弃,临时下线整页用 `draft: true`
186186
- 不把 `include` 当目录页:一页引入十个片段时,读者需要的是十条链接。

exampleSite/content/docs/include/parts/install-oink.zh.md

Lines changed: 0 additions & 12 deletions
This file was deleted.

exampleSite/content/docs/include/parts/module.zh.yml

Lines changed: 0 additions & 6 deletions
This file was deleted.

0 commit comments

Comments
 (0)