Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Refactor relate page #1371

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
133 changes: 93 additions & 40 deletions assets/css/_partial/_single/_related.scss
Original file line number Diff line number Diff line change
@@ -1,52 +1,105 @@
.related-container {
display: grid;
grid-template-columns: repeat(4, 1fr);
justify-content: space-between;
gap: 1rem;
// margin-top: 2rem;
padding: 0.8rem;
}

.related-item-container {
flex: 1;
min-width: 0; /* 允许flex项目缩小到比内容更小 */
background-color: $related-background-color;
@include border-radius(0.25rem);
@include box-shadow(0 2px 4px rgba(0, 0, 0, 0.1));
@include transition(transform 0.3s ease, box-shadow 0.3s ease);
overflow: hidden;
display: flex;
flex-wrap: nowrap;
overflow-x: scroll;
width: 100%;
height: 280px;

.related-item-container {
flex-basis: 350px;
flex-grow: 1;
flex-shrink: 1;
height: 270px;
min-width: 300px;
margin-right: 20px;
background: $related-background-color;
position: relative;
aspect-ratio: 2 / 1.5; /* 设置固定的宽高比 */
}

.related-item-container:hover {
transform: translateY(-5px);
@include box-shadow(0 4px 8px rgba(0, 0, 0, 0.2));
h2.related-title {
color: $global-link-hover-color;
}
}

.related-image {
@include transition(transform 0.4s ease);
.related-item-container > a {
// display: flex;
flex-direction: column;
height: 100%;
width: 100%;
text-decoration: none;
color: $related-color;
}

img {
width: 100%;
height: 200px;
object-fit: cover;
}
.related-image {
width: 100%;
height: 0;
padding-bottom: 50%; /* 保持 2:1 的宽高比 */
position: relative;
overflow: hidden;
}

&:hover {
@include transform(scale(1.01));
}
}
.related-image img {
position: absolute;
top: 0;
left: 0;
width: 100%;
height: 100%;
object-fit: cover;
}

.related-title {
font-size: 0.85rem;
margin: 0.3rem;
overflow: hidden;
}

/* 有图片时的标题样式 */
.related-item-container:has(.related-image) .related-title {
white-space: nowrap;
text-overflow: ellipsis;
}

/* 没有图片时的标题样式 */
.related-item-container:not(:has(.related-image)) .related-title {
// display: -webkit-box;
// -webkit-line-clamp: 5; /* 显示5行 */
// -webkit-box-orient: vertical;
// max-height: 7.8em; /* 大约5行的高度,根据实际情况调整 */
display: flex; /* 使用标准的 Flexbox */
flex-direction: column; /* 垂直方向排列 */
overflow: hidden; /* 隐藏超出部分 */
line-height: 1.5em;
}

/* 当没有图片时,确保整个区域可点击 */
.related-item-container:not(:has(.related-image)) > a {
padding: 0.5rem;
}

@media (max-width: 768px) {
.related-container {
grid-template-columns: repeat(2, 1fr);
padding: 0.5rem;
}

.related-title {
position: absolute;
text-overflow: ellipsis;
overflow: hidden;
white-space: nowrap;
top: 210px;
width: 95%;
margin: 10px;
font-size: 1.25rem;
line-height: 140%;
font-size: 1rem;
}
}

.related-title a {
color: $related-color;

&:hover {
color: $related-hover-color;
}
@media (max-width: 480px) {
.related-container {
grid-template-columns: 1fr;
gap: 0.5rem;
}

.related-title {
font-size: 0.9rem;
}
}
1 change: 0 additions & 1 deletion exampleSite/config/_default/params.toml
Original file line number Diff line number Diff line change
Expand Up @@ -508,7 +508,6 @@ bundle = false
# 相关文章推荐配置
[page.related]
enable = true
count = 5
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure about this change. Why do you think it's a better idea to fix recommendation count to 4?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. It defaults to displaying 5, but only displays 3 at the maximum page width
  2. Almost no one pulls the scrollbar to browse more, the scrollbar is meaningless
  3. 4 relate items in 800px, it will look more beautiful.
  4. This recommendation algorithm is very rough, making it meaningless for users to choose to configure the number of recommendations

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I imagine configuring the count to 2 or 3 makes sense equally. The 800px argument does not hold on mobile, where maybe only one recommendation can fit on screen and you will need to handle the overflow with a scrollbar anyway.

Copy link
Contributor Author

@wu0407 wu0407 Oct 22, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dynamically adapt to screen width,maybe change from 4 to 3 would be more appropriate for 800px.

@media (max-width: 768px) {
.related-container {
grid-template-columns: repeat(2, 1fr);
padding: 0.5rem;
}
.related-title {
font-size: 1rem;
}
}
@media (max-width: 480px) {
.related-container {
grid-template-columns: 1fr;
gap: 0.5rem;
}
.related-title {
font-size: 0.9rem;
}


# Sponsor config
# 赞助设置
Expand Down
1 change: 0 additions & 1 deletion exampleSite/content/posts/tests/related-tests/index.en.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ hiddenFromHomePage: true
hiddenFromSearch: true
related:
enable: true
count: 2
---

<!--more-->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -749,7 +749,6 @@ Please open the code block below to view the complete sample configuration {{< f
# {{< version 0.2.14>}} Related content config
[params.page.related]
enable = true
count = 5


# {{< version 0.2.13 >}} Sponsor config
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -752,7 +752,6 @@ optimizeImages = true
# {{< version 0.2.14>}} 相关文章推荐配置
[params.page.related]
enable = true
count = 5


# {{< version 0.2.13 >}} 赞赏配置
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,6 @@ sponsor:
# ...
related:
enable: false
count: 5
---
```

Expand Down
51 changes: 23 additions & 28 deletions layouts/partials/related.html
Original file line number Diff line number Diff line change
@@ -1,42 +1,37 @@
{{- $params := .Scratch.Get "params" -}}

{{ $related := first $params.related.count (where (where .Site.Pages ".Params.tags" "intersect" .Params.tags) "Permalink" "!=" .Permalink) }}
{{ $related := first 4 (where (where $.Site.Pages ".Params.tags" "intersect" .Params.tags) "Permalink" "!=" .Permalink) }}
{{ with $related }}

<h2>{{- T "relatedContent" -}}</h2>
<div class="related-container">
{{ range . }}
{{- $params := .Params | merge .Site.Params.page -}}
<div class="related-item-container">
{{ $relPermalink := .RelPermalink}}
{{ $pageRelPermalink := .RelPermalink}}
{{- $title := .Title -}}
{{- $resource := .Resources -}}
{{- $image := $params.featuredImagePreview | default $params.featuredImage -}}
{{- $height := "auto" -}}
{{- $width := "auto" -}}
{{- with .Resources.GetMatch (printf "%s" ($image)) -}}
{{- $image = .RelPermalink -}}
{{- $height = .Height -}}
{{- $width = .Width -}}
{{- if not $image -}}
{{- with .Resources.GetMatch "{featured-image, featured-image-preview}" -}}
{{- $image = strings.TrimPrefix $pageRelPermalink .RelPermalink -}}
{{- end -}}
{{- end -}}
{{- with .Resources.GetMatch "featured-image" -}}
{{- $image = .RelPermalink -}}
{{- $height = .Height -}}
{{- $width = .Width -}}
{{- end -}}
{{- with .Resources.GetMatch "featured-image-preview" -}}
{{- $image = .RelPermalink -}}
{{- $height = .Height -}}
{{- $width = .Width -}}
{{- end -}}
{{- with $image -}}
<div class="related-image">
<a href="{{ $relPermalink }}">
{{- dict "Src" $image "Height" "200" "Width" "400" "Title" $.Description | partial "plugin/image.html" -}}
</a>
</div>
{{- end -}}
<h2 class="related-title">
<a href="{{ .RelPermalink }}">{{ .Title }}</a>
</h2>
<a href="{{ $pageRelPermalink }}">
{{- with $image -}}
<div class="related-image">
{{- $optimize := slice
(dict "Process" "resize 800x webp q75" "descriptor" "800w")
(dict "Process" "resize 1200x webp q75" "descriptor" "1200w")
(dict "Process" "resize 1600x webp q75" "descriptor" "1600w")
-}}
{{- dict "Src" $image "Resources" $resource "OptimConfig" $optimize "Title" $title | partial "plugin/image.html" -}}
</div>
{{- end -}}
<h2 class="related-title">
{{ .Title }}
</h2>
</a>
</div>
{{ end }}

Expand Down
Loading