Skip to content

Commit 3e28588

Browse files
committed
Feat: 增加目录显示
resolved #9
1 parent 46d6109 commit 3e28588

File tree

6 files changed

+100
-2
lines changed

6 files changed

+100
-2
lines changed

README.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,16 @@ Use the `gh-repo-card-container` and `gh-repo-card` shortcodes in any page to di
117117
{{< /gh-repo-card-container >}}
118118
```
119119

120+
### Custom Blocks
121+
122+
You can implement these blocks through `define`.
123+
124+
| Block Name | Description |
125+
| :---------------- | :------------------------------------------------ |
126+
| `projects-aside` | Displayed in the aside of the projects page |
127+
| `projects-meta` | Displayed in the post meta of the projects page |
128+
| `projects-footer` | Displayed in the post footer of the projects page |
129+
120130
## Scheduled tasks
121131

122132
Since it uses server-side rendering, all data is fetched at build time and not requested from the GitHub API on each visit. Therefore, we can use scheduled tasks to update the data to keep it up to date.

README.zh-cn.md

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,16 @@ Some text to display at the start of the page.
117117
{{< /gh-repo-card-container >}}
118118
```
119119

120+
### 自定义块
121+
122+
你可以通过 `define` 来实现这些块。
123+
124+
| 块名称 | 描述 |
125+
| :---------------- | :----------------------- |
126+
| `projects-aside` | 显示在项目页面的侧边栏中 |
127+
| `projects-meta` | 显示在项目页面的开始处 |
128+
| `projects-footer` | 显示在项目页面的底部处 |
129+
120130
## 定时任务
121131

122132
由于采用服务端渲染,所有数据是在构建时获取的,而不会在每次访问时都请求 GitHub API。因此,我们可以使用定时任务来更新数据,从而保持其最新状态。

assets/css/component-projects/index.scss

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,12 @@
3333
}
3434
}
3535

36+
.component-projects-powered {
37+
text-align: right;
38+
font-size: 80%;
39+
color: var(--color-muted);
40+
}
41+
3642
#comments {
3743
padding: 2rem 0;
3844
margin-top: 2rem;

i18n/en.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
[projects]
55
relatedArticles = "Related Articles"
66
errorGetGhRepo = "failed to fetch repo info from GitHub API, Please check the network!"
7+
poweredBy = "This page is powered by <a href=\"https://github.com/hugo-fixit/component-projects\">Component Projects</a>."
78

89
[projects.totalStarCount]
910
one = "Total one star"

i18n/zh-CN.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
[projects]
66
relatedArticles = "相关文章"
77
errorGetGhRepo = "从 GitHub API 获取仓库信息失败,请检查网络!"
8+
poweredBy = "本页面由 <a href=\"https://github.com/hugo-fixit/component-projects\">Component Projects</a> 强力驱动。"
89

910
[projects.totalStarCount]
1011
other = "累计 {{ .Count }} stars"

layouts/section/projects.html

Lines changed: 72 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,22 @@
66
{{- define "content" -}}
77
{{- $params := partial "function/params.html" -}}
88
{{- $fingerprint := .Scratch.Get "fingerprint" -}}
9+
{{- $data := index .Site.Data (printf "projects.%v" .Language) | default .Site.Data.projects -}}
10+
{{- $toc := .Scratch.Get "toc" -}}
11+
{{- $hasToc := (gt (len .Fragments.Headings) 0) | or (gt (len $data) 0) | or (gt .Pages.Len 0) -}}
12+
{{- $showToc := $toc.enable | and $hasToc -}}
913

1014
{{- /* component-projects.min.css */ -}}
1115
{{- $options := dict "Source" "css/component-projects/index.scss" "Fingerprint" $fingerprint -}}
1216
{{- $toCSS := dict "targetPath" "css/component-projects.min.css" "enableSourceMap" true -}}
1317
{{- $options = dict "Context" . "ToCSS" $toCSS | merge $options -}}
1418
{{- partial "plugin/style.html" $options -}}
1519

20+
<aside class="animate__animated animate__fadeIn animate__faster">
21+
{{- /* Projects Aside */ -}}
22+
{{- block "projects-aside" . }}{{ end -}}
23+
</aside>
24+
1625
<div class="page archive projects single">
1726
<div class="header">
1827
{{- /* Title */ -}}
@@ -25,11 +34,55 @@ <h1 class="single-title animate__animated animate__pulse animate__faster">
2534
{{- /* Subtitle */ -}}
2635
{{- with $params.subtitle -}}<p class="single-subtitle animate__animated animate__fadeIn">{{ . | $.RenderString }}</p>{{- end -}}
2736
</div>
28-
37+
38+
{{- /* Meta */ -}}
39+
<div class="post-meta">
40+
{{- /* Projects Meta */ -}}
41+
{{- block "projects-meta" . }}{{ end -}}
42+
</div>
43+
44+
{{- /* Featured image */ -}}
45+
{{- $image := $params.featuredimage -}}
46+
{{- with .Resources.GetMatch "featured-image" -}}
47+
{{- $image = .RelPermalink -}}
48+
{{- end -}}
49+
{{- with $image -}}
50+
<div class="featured-image">
51+
{{- dict "Src" . "Title" $.Description | partial "plugin/image.html" -}}
52+
</div>
53+
{{- end -}}
54+
55+
{{- /* Static TOC */ -}}
56+
{{- if $showToc -}}
57+
{{- $tableOfContents := .Fragments.ToHTML ($toc.startLevel | int) ($toc.endLevel | int) false -}}
58+
{{- $tocFromData := "" -}}
59+
{{- range $group := $data -}}
60+
{{- if $group.title -}}
61+
{{- $tocFromData = add $tocFromData (printf "<li><a href=\"#%v\">%v</a></li>" ($group.title | anchorize) $group.title) -}}
62+
{{- end -}}
63+
{{- end -}}
64+
{{- $tocFromData = add $tocFromData (printf "<li><a href=\"#related-articles\">%v</a></li>" (T "projects.relatedArticles")) -}}
65+
{{- with $tocFromData -}}
66+
{{- if gt (len $.Fragments.Headings) 0 -}}
67+
{{- $tableOfContents = replaceRE "</ul>" (printf "%v</ul>" .) $tableOfContents -}}
68+
{{- else -}}
69+
{{- $tableOfContents = replaceRE "</nav>" (printf "<ul>%v</ul></nav>" .) $tableOfContents -}}
70+
{{- end -}}
71+
{{- end -}}
72+
<div class="details toc{{ with $params.password }} encrypted-hidden{{ end }}" id="toc-static" data-kept="{{ if $toc.keepStatic }}true{{ else }}false{{ end }}">
73+
<div class="details-summary toc-title">
74+
<span>{{ T "single.contents" }}</span>
75+
<span>{{ dict "Class" "details-icon fa-solid fa-angle-right" | partial "plugin/icon.html" }}</span>
76+
</div>
77+
<div class="details-content toc-content" id="toc-content-static">
78+
{{- dict "Content" $tableOfContents "Ruby" $params.ruby "Fraction" $params.fraction "Fontawesome" $params.fontawesome | partial "function/content.html" | safeHTML -}}
79+
</div>
80+
</div>
81+
{{- end -}}
82+
2983
{{- /* Content */ -}}
3084
<div class="content" id="content">
3185
{{- dict "Content" .Content "Ruby" $params.ruby "Fraction" $params.fraction "Fontawesome" $params.fontawesome | partial "function/content.html" | safeHTML -}}
32-
{{- $data := index .Site.Data (printf "projects.%v" .Language) | default .Site.Data.projects -}}
3386
{{- $headers := dict "headers" (.Scratch.Get "githubTokenHeader") -}}
3487
{{- $githubLangColors := partial "function/get-remote-json" (dict "URL" "https://raw.githubusercontent.com/ozh/github-colors/master/colors.json" "OPTIONS" $headers ) -}}
3588
{{- range $group := $data -}}
@@ -113,7 +166,24 @@ <h3 class="group-title">
113166
{{- partial "paginator.html" . -}}
114167
{{- end -}}
115168

169+
{{- /* Footer */ -}}
170+
<div class="post-footer" id="post-footer">
171+
{{- /* Projects Footer */ -}}
172+
{{- block "projects-footer" . }}<div class="component-projects-powered">{{ (T "projects.poweredBy") | safeHTML }}</div>{{ end -}}
173+
</div>
174+
116175
{{- /* Comment */ -}}
117176
{{- partial "single/comment.html" . -}}
118177
</div>
178+
179+
<aside class="toc" id="toc-auto" aria-label="{{ T "single.contents" }}">
180+
{{- /* Auto TOC */ -}}
181+
{{- if $showToc -}}
182+
<h2 class="toc-title{{ with $params.password }} encrypted-hidden{{ end }}">
183+
{{- T "single.contents" -}}&nbsp;
184+
{{- dict "Class" "toc-icon fa-solid fa-angle-down fa-fw" | partial "plugin/icon.html" -}}
185+
</h2>
186+
<div class="toc-content{{ if eq $toc.auto false }} always-active{{ end }}{{ with $params.password }} encrypted-hidden{{ end }}" id="toc-content-auto"></div>
187+
{{- end -}}
188+
</aside>
119189
{{- end -}}

0 commit comments

Comments
 (0)