From 73fa403a24ee9a573d7f9082208aab9413629d49 Mon Sep 17 00:00:00 2001 From: xaoxuu Date: Sat, 13 Jan 2024 21:10:26 +0800 Subject: [PATCH] [feat] related posts in first category --- _config.yml | 2 +- languages/en.yml | 1 + languages/zh-CN.yml | 1 + languages/zh-TW.yml | 1 + layout/_partial/head.ejs | 2 +- .../_partial/main/article/article_footer.ejs | 20 +++--- layout/_partial/widgets/related.ejs | 65 +++++++++++++------ layout/_partial/widgets/toc.ejs | 12 ++-- source/css/_layout/partial/related.styl | 3 + 9 files changed, 70 insertions(+), 37 deletions(-) diff --git a/_config.yml b/_config.yml index 22bdc07..98a7d7b 100755 --- a/_config.yml +++ b/_config.yml @@ -40,7 +40,7 @@ sidebar: 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' # 其它 layout:page 的页面 diff --git a/languages/en.yml b/languages/en.yml index 329fdfa..4421f00 100755 --- a/languages/en.yml +++ b/languages/en.yml @@ -24,6 +24,7 @@ meta: comment_title: Join the discussion back_to_top: Back to top more: 'More %s' + posts_in_category: 'Category: %s' created_author: '%s posted on' created: 'Posted on' updated: 'Updated on' diff --git a/languages/zh-CN.yml b/languages/zh-CN.yml index f839359..dc0d8a1 100755 --- a/languages/zh-CN.yml +++ b/languages/zh-CN.yml @@ -24,6 +24,7 @@ meta: comment_title: 快来参与讨论吧 back_to_top: 回到顶部 more: '更多「%s」' + posts_in_category: '专栏:%s' created_author: 本文由 %s 发布于 created: 发布于 updated: 更新于 diff --git a/languages/zh-TW.yml b/languages/zh-TW.yml index 083d140..9128091 100755 --- a/languages/zh-TW.yml +++ b/languages/zh-TW.yml @@ -24,6 +24,7 @@ meta: comment_title: 參與討論 back_to_top: 回到頁首 more: '更多「%s」' + posts_in_category: '專欄:%s' created_author: 本文由 %s 發布於 created: 發布於 updated: 更新於 diff --git a/layout/_partial/head.ejs b/layout/_partial/head.ejs index 33d5f1d..a822a45 100755 --- a/layout/_partial/head.ejs +++ b/layout/_partial/head.ejs @@ -26,7 +26,7 @@ function generate_description() { if (theme.open_graph && theme.open_graph.enable) { return ''; } - if (page.layout == 'wiki' && page.wiki) { + if (page.wiki) { let proj = theme.wiki.tree[page.wiki]; if (proj && proj.description) { return ''; diff --git a/layout/_partial/main/article/article_footer.ejs b/layout/_partial/main/article/article_footer.ejs index 0524213..0a91d90 100644 --- a/layout/_partial/main/article/article_footer.ejs +++ b/layout/_partial/main/article/article_footer.ejs @@ -38,11 +38,7 @@ function layoutDiv() { } var license = '' - if (page.layout == 'post') { - if (theme.article.license && (page.license != false)) { - license = page.license || theme.article.license - } - } else if (page.layout == 'wiki' && page.wiki) { + if (page.wiki) { let proj = theme.wiki.tree[page.wiki] if (page.license != null) { license = page.license || theme.article.license @@ -53,6 +49,10 @@ function layoutDiv() { 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) { var author = null @@ -75,15 +75,15 @@ function layoutDiv() { } var showSharePlugin = false - if (page.layout == 'post') { - showSharePlugin = page.share != false - } else if (page.layout == 'wiki' && page.wiki) { + if (page.wiki) { let proj = theme.wiki.tree[page.wiki] if (page.share != null) { showSharePlugin = page.share == true } else if (proj != null) { showSharePlugin = proj.share == true } + } else if (page.layout == 'post') { + showSharePlugin = page.share != false } if (theme.article.share && showSharePlugin) { function socialButtons() { @@ -101,8 +101,8 @@ function layoutDiv() { el += '&title=' + (page.seo_title || (page.title + ' - ' + config.title)); if (page.layout == 'post' && page.cover) { el += '&pics=' + page.cover; - } else if (page.layout == 'wiki' && page.logo && page.logo.src) { - el += '&pics=' + page.logo.src; + } else if (page.layout == 'wiki' && page.icon) { + el += '&pics=' + page.icon; } el += '&summary=' + truncate(page.description || strip_html(page.excerpt || page.content), {length: 120}); el += '"'; diff --git a/layout/_partial/widgets/related.ejs b/layout/_partial/widgets/related.ejs index deafec0..a902ed3 100644 --- a/layout/_partial/widgets/related.ejs +++ b/layout/_partial/widgets/related.ejs @@ -1,34 +1,59 @@ <% -function layoutDiv() { - if (page.layout !== "wiki") { - return ''; +function relatedPosts() { + const { categories } = page; + if (categories.length == 0) { + return '' } - let thisItemObject = theme.wiki.tree[page.wiki]; + const relatedItem = categories.data[0] + var el = '' + el += `` + el += `
` + el += `${__('meta.posts_in_category', relatedItem.name)}` + el += `
` + el += `` + el += `
` + return el +} +function relatedWiki() { + let thisItemObject = theme.wiki.tree[page.wiki] if (thisItemObject == null) { - return ''; + return '' } - const relatedItems = thisItemObject.relatedItems; - var el = ''; + const relatedItems = thisItemObject.relatedItems + var el = '' for (let relatedItem of relatedItems) { - el += ''; - el += '
'; - el += '' + __('meta.more', relatedItem.name) + ''; - el += '
'; - el += '` + el += `
` + } + return el +} +function layoutDiv() { + if (page.layout == 'wiki') { + return relatedWiki() + } else if (page.layout == 'post') { + return relatedPosts() } - return el; } %> <%- layoutDiv() %> diff --git a/layout/_partial/widgets/toc.ejs b/layout/_partial/widgets/toc.ejs index 8730eee..9b1de3e 100644 --- a/layout/_partial/widgets/toc.ejs +++ b/layout/_partial/widgets/toc.ejs @@ -1,21 +1,23 @@ <% let proj = theme.wiki.tree[page.wiki]; - +var hasTOC = false function layoutToc() { if (toc(page.content).length > 0) { + hasTOC = true return toc(page.content, { list_number: item.list_number, min_depth: item.min_depth, max_depth: item.max_depth }); } - return ''; + hasTOC = false + return '' } function layoutTocHeader(title) { var el = ''; el += '
'; - el += '' + (title || page.title || __("meta.toc")) + ''; + el += '' + (title || __("meta.toc")) + ''; el += '
'; return el; } @@ -45,7 +47,7 @@ function layoutDocTree(pages) { } el += ''; } - return el; + return hasTOC ? el : '' } @@ -102,7 +104,7 @@ function layoutDiv(fallback) { } else if (item.fallback) { el += partial(item.fallback, {item: theme.data.widgets[item.fallback]}); } - return el; + return hasTOC ? el : '' } %> diff --git a/source/css/_layout/partial/related.styl b/source/css/_layout/partial/related.styl index 5adcf9b..15ff5ae 100644 --- a/source/css/_layout/partial/related.styl +++ b/source/css/_layout/partial/related.styl @@ -45,6 +45,9 @@ article.md-text.content+.related-wrap -webkit-box-orient: vertical overflow: hidden -webkit-line-clamp: 2 + &.active + .title + border-bottom: 1px dashed $color-theme &:hover .title color: $color-hover