[feat] related posts in first category

This commit is contained in:
xaoxuu 2024-01-13 21:10:26 +08:00
parent 99a68840b4
commit 73fa403a24
9 changed files with 70 additions and 37 deletions

View File

@ -40,7 +40,7 @@ sidebar:
others: search, recent, timeline # for 404 and ... others: search, recent, timeline # for 404 and ...
#### 手动创建的页面 #### #### 手动创建的页面 ####
# 文章内页 # 文章内页
post: search_blog, toc, ghrepo, ghissues # for pages using 'layout:post' post: search_blog, toc, related, ghrepo, ghissues # for pages using 'layout:post'
# 文档内页 # 文档内页
wiki: search, toc, ghissues, related # for pages using 'layout:wiki' wiki: search, toc, ghissues, related # for pages using 'layout:wiki'
# 其它 layout:page 的页面 # 其它 layout:page 的页面

View File

@ -24,6 +24,7 @@ meta:
comment_title: Join the discussion comment_title: Join the discussion
back_to_top: Back to top back_to_top: Back to top
more: 'More %s' more: 'More %s'
posts_in_category: 'Category: %s'
created_author: '%s posted on' created_author: '%s posted on'
created: 'Posted on' created: 'Posted on'
updated: 'Updated on' updated: 'Updated on'

View File

@ -24,6 +24,7 @@ meta:
comment_title: 快来参与讨论吧 comment_title: 快来参与讨论吧
back_to_top: 回到顶部 back_to_top: 回到顶部
more: '更多「%s」' more: '更多「%s」'
posts_in_category: '专栏:%s'
created_author: 本文由 %s 发布于 created_author: 本文由 %s 发布于
created: 发布于 created: 发布于
updated: 更新于 updated: 更新于

View File

@ -24,6 +24,7 @@ meta:
comment_title: 參與討論 comment_title: 參與討論
back_to_top: 回到頁首 back_to_top: 回到頁首
more: '更多「%s」' more: '更多「%s」'
posts_in_category: '專欄:%s'
created_author: 本文由 %s 發布於 created_author: 本文由 %s 發布於
created: 發布於 created: 發布於
updated: 更新於 updated: 更新於

View File

@ -26,7 +26,7 @@ function generate_description() {
if (theme.open_graph && theme.open_graph.enable) { if (theme.open_graph && theme.open_graph.enable) {
return ''; return '';
} }
if (page.layout == 'wiki' && page.wiki) { if (page.wiki) {
let proj = theme.wiki.tree[page.wiki]; let proj = theme.wiki.tree[page.wiki];
if (proj && proj.description) { if (proj && proj.description) {
return '<meta name="description" content="' + proj.description + '">'; return '<meta name="description" content="' + proj.description + '">';

View File

@ -38,11 +38,7 @@ function layoutDiv() {
} }
var license = '' var license = ''
if (page.layout == 'post') { if (page.wiki) {
if (theme.article.license && (page.license != false)) {
license = page.license || theme.article.license
}
} else if (page.layout == 'wiki' && page.wiki) {
let proj = theme.wiki.tree[page.wiki] let proj = theme.wiki.tree[page.wiki]
if (page.license != null) { if (page.license != null) {
license = page.license || theme.article.license license = page.license || theme.article.license
@ -53,6 +49,10 @@ function layoutDiv() {
license = proj.license license = proj.license
} }
} }
} else if (page.layout == 'post') {
if (theme.article.license && (page.license != false)) {
license = page.license || theme.article.license
}
} }
if (license.length > 0) { if (license.length > 0) {
var author = null var author = null
@ -75,15 +75,15 @@ function layoutDiv() {
} }
var showSharePlugin = false var showSharePlugin = false
if (page.layout == 'post') { if (page.wiki) {
showSharePlugin = page.share != false
} else if (page.layout == 'wiki' && page.wiki) {
let proj = theme.wiki.tree[page.wiki] let proj = theme.wiki.tree[page.wiki]
if (page.share != null) { if (page.share != null) {
showSharePlugin = page.share == true showSharePlugin = page.share == true
} else if (proj != null) { } else if (proj != null) {
showSharePlugin = proj.share == true showSharePlugin = proj.share == true
} }
} else if (page.layout == 'post') {
showSharePlugin = page.share != false
} }
if (theme.article.share && showSharePlugin) { if (theme.article.share && showSharePlugin) {
function socialButtons() { function socialButtons() {
@ -101,8 +101,8 @@ function layoutDiv() {
el += '&title=' + (page.seo_title || (page.title + ' - ' + config.title)); el += '&title=' + (page.seo_title || (page.title + ' - ' + config.title));
if (page.layout == 'post' && page.cover) { if (page.layout == 'post' && page.cover) {
el += '&pics=' + page.cover; el += '&pics=' + page.cover;
} else if (page.layout == 'wiki' && page.logo && page.logo.src) { } else if (page.layout == 'wiki' && page.icon) {
el += '&pics=' + page.logo.src; el += '&pics=' + page.icon;
} }
el += '&summary=' + truncate(page.description || strip_html(page.excerpt || page.content), {length: 120}); el += '&summary=' + truncate(page.description || strip_html(page.excerpt || page.content), {length: 120});
el += '"'; el += '"';

View File

@ -1,34 +1,59 @@
<% <%
function layoutDiv() { function relatedPosts() {
if (page.layout !== "wiki") { const { categories } = page;
return ''; if (categories.length == 0) {
return ''
} }
let thisItemObject = theme.wiki.tree[page.wiki]; const relatedItem = categories.data[0]
var el = ''
el += `<widget class="widget-wrapper recent">`
el += `<div class="widget-header cap theme dis-select">`
el += `<span class="name">${__('meta.posts_in_category', relatedItem.name)}</span>`
el += `</div>`
el += `<div class="widget-body related-posts">`
relatedItem.posts.sort('date', -1).each(function(post) {
el += `<a class="item${post.path == page.path ? ' active' : ''}" href="${url_for(post.path)}">`
el += `<span class="title">${post.title}</span>`
el += `</a>`
})
el += `</div>`
el += `</widget>`
return el
}
function relatedWiki() {
let thisItemObject = theme.wiki.tree[page.wiki]
if (thisItemObject == null) { if (thisItemObject == null) {
return ''; return ''
} }
const relatedItems = thisItemObject.relatedItems; const relatedItems = thisItemObject.relatedItems
var el = ''; var el = ''
for (let relatedItem of relatedItems) { for (let relatedItem of relatedItems) {
el += '<widget class="widget-wrapper related">'; el += `<widget class="widget-wrapper related">`
el += '<div class="widget-header cap theme dis-select">'; el += `<div class="widget-header cap theme dis-select">`
el += '<span class="name">' + __('meta.more', relatedItem.name) + '</span>'; el += `<span class="name">${__('meta.more', relatedItem.name)}</span>`
el += '</div>'; el += `</div>`
el += '<div class="widget-body related-posts">'; el += `<div class="widget-body related-posts">`
for (let id of relatedItem.items) { for (let id of relatedItem.items) {
// 同一个分组中的其它项目 // 同一个分组中的其它项目
let item = theme.wiki.tree[id]; let item = theme.wiki.tree[id]
el += '<a class="item wiki" href="' + url_for(item.homepage?.path) + '">'; el += `<a class="item wiki" href="${url_for(item.homepage?.path)}">`
el += '<span class="title">' + item.title + '</span>'; el += `<span class="title">${item.title}</span>`
if (item.description && item.description.length > 0) { if (item.description && item.description.length > 0) {
el += '<span class="excerpt">' + item.description + '</span>'; el += `<span class="excerpt">${item.description}</span>`
} }
el += '</a>'; el += `</a>`
} }
el += '</div>'; el += `</div>`
el += '</widget>'; el += `</widget>`
}
return el
}
function layoutDiv() {
if (page.layout == 'wiki') {
return relatedWiki()
} else if (page.layout == 'post') {
return relatedPosts()
} }
return el;
} }
%> %>
<%- layoutDiv() %> <%- layoutDiv() %>

View File

@ -1,21 +1,23 @@
<% <%
let proj = theme.wiki.tree[page.wiki]; let proj = theme.wiki.tree[page.wiki];
var hasTOC = false
function layoutToc() { function layoutToc() {
if (toc(page.content).length > 0) { if (toc(page.content).length > 0) {
hasTOC = true
return toc(page.content, { return toc(page.content, {
list_number: item.list_number, list_number: item.list_number,
min_depth: item.min_depth, min_depth: item.min_depth,
max_depth: item.max_depth max_depth: item.max_depth
}); });
} }
return ''; hasTOC = false
return ''
} }
function layoutTocHeader(title) { function layoutTocHeader(title) {
var el = ''; var el = '';
el += '<div class="widget-header cap dis-select">'; el += '<div class="widget-header cap dis-select">';
el += '<span class="name">' + (title || page.title || __("meta.toc")) + '</span>'; el += '<span class="name">' + (title || __("meta.toc")) + '</span>';
el += '</div>'; el += '</div>';
return el; return el;
} }
@ -45,7 +47,7 @@ function layoutDocTree(pages) {
} }
el += '</div>'; el += '</div>';
} }
return el; return hasTOC ? el : ''
} }
@ -102,7 +104,7 @@ function layoutDiv(fallback) {
} else if (item.fallback) { } else if (item.fallback) {
el += partial(item.fallback, {item: theme.data.widgets[item.fallback]}); el += partial(item.fallback, {item: theme.data.widgets[item.fallback]});
} }
return el; return hasTOC ? el : ''
} }
%> %>

View File

@ -45,6 +45,9 @@ article.md-text.content+.related-wrap
-webkit-box-orient: vertical -webkit-box-orient: vertical
overflow: hidden overflow: hidden
-webkit-line-clamp: 2 -webkit-line-clamp: 2
&.active
.title
border-bottom: 1px dashed $color-theme
&:hover &:hover
.title .title
color: $color-hover color: $color-hover