From 3b300104aff266da1b8818b23f5b4465b5079c9b Mon Sep 17 00:00:00 2001 From: xaoxuu Date: Thu, 4 Jan 2024 22:50:57 +0800 Subject: [PATCH] [feat] author --- languages/en.yml | 1 + languages/zh-CN.yml | 1 + languages/zh-TW.yml | 1 + layout/_partial/main/navbar/author_banner.ejs | 31 +++++++ layout/_partial/main/navbar/breadcrumb.ejs | 22 ++++- layout/_partial/main/post_list/post_card.ejs | 14 ++- layout/archive.ejs | 91 +++++++++++-------- scripts/events/index.js | 1 + scripts/events/lib/authors.js | 21 +++++ scripts/events/lib/links.js | 2 +- scripts/generators/author.js | 22 +++++ source/css/_layout/list.styl | 6 +- source/css/_layout/main.styl | 4 + source/css/_layout/pages/archives.styl | 10 +- source/css/_layout/partial/bread-nav.styl | 8 +- source/css/_layout/tag-plugins/banner.styl | 24 ++++- 16 files changed, 203 insertions(+), 56 deletions(-) create mode 100644 layout/_partial/main/navbar/author_banner.ejs create mode 100644 scripts/events/lib/authors.js create mode 100644 scripts/generators/author.js diff --git a/languages/en.yml b/languages/en.yml index a5f1333..329fdfa 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' + created_author: '%s posted on' created: 'Posted on' updated: 'Updated on' license: License diff --git a/languages/zh-CN.yml b/languages/zh-CN.yml index 568ac08..f839359 100755 --- a/languages/zh-CN.yml +++ b/languages/zh-CN.yml @@ -24,6 +24,7 @@ meta: comment_title: 快来参与讨论吧 back_to_top: 回到顶部 more: '更多「%s」' + created_author: 本文由 %s 发布于 created: 发布于 updated: 更新于 license: 许可协议 diff --git a/languages/zh-TW.yml b/languages/zh-TW.yml index f2e49ae..083d140 100755 --- a/languages/zh-TW.yml +++ b/languages/zh-TW.yml @@ -24,6 +24,7 @@ meta: comment_title: 參與討論 back_to_top: 回到頁首 more: '更多「%s」' + created_author: 本文由 %s 發布於 created: 發布於 updated: 更新於 license: 授權條款 diff --git a/layout/_partial/main/navbar/author_banner.ejs b/layout/_partial/main/navbar/author_banner.ejs new file mode 100644 index 0000000..58e68f5 --- /dev/null +++ b/layout/_partial/main/navbar/author_banner.ejs @@ -0,0 +1,31 @@ +<% +function layoutDiv() { + const author = page.author + var el = `` + el += `` + return el +} +%> +<%- layoutDiv() %> \ No newline at end of file diff --git a/layout/_partial/main/navbar/breadcrumb.ejs b/layout/_partial/main/navbar/breadcrumb.ejs index 8a50c34..87f403c 100644 --- a/layout/_partial/main/navbar/breadcrumb.ejs +++ b/layout/_partial/main/navbar/breadcrumb.ejs @@ -35,13 +35,29 @@ function layoutDiv() { }); } el += ''; - // 发布日期 + // 作者 + var author = null + if (theme.authors) { + if (page.author?.length > 0 && theme.authors[page.author] != null) { + author = theme.authors[page.author] + } else { + author = theme.default_author + } + } el += '
'; + if (author) { + let link = `${author.name}` + el += `${__("meta.created_author", link)}` + } else { + el += `${__("meta.created")}` + } + // 发布日期 el += ` - ${__("meta.created")}  + `; + // 更新日期 el += ` - ${__("meta.updated")}  + ${__("symbol.comma") + __("meta.updated")}  `; el += '
'; diff --git a/layout/_partial/main/post_list/post_card.ejs b/layout/_partial/main/post_list/post_card.ejs index 382b489..dfeeef4 100755 --- a/layout/_partial/main/post_list/post_card.ejs +++ b/layout/_partial/main/post_list/post_card.ejs @@ -68,8 +68,20 @@ function div_default() { // meta el += '
'; - // time el += ''; + // author + var author = null + if (theme.authors) { + if (page.author?.length > 0 && theme.authors[page.author] != null) { + author = theme.authors[page.author] + } else { + author = theme.default_author + } + } + if (author) { + el += author.name + ' ' + } + // time el += __("meta.created") + ' ' + ''; el += ''; // cat diff --git a/layout/archive.ejs b/layout/archive.ejs index bdaeca1..05f005b 100755 --- a/layout/archive.ejs +++ b/layout/archive.ejs @@ -3,41 +3,58 @@ page.robots = 'noindex,follow'; if (page.menu_id == undefined) { page.menu_id = 'post'; } +if (page.author) { + page.header = 'auto'; +} +function layoutArchiveList() { + var el = '' + if (page.author) { + page.title = page.author.name + el += partial('_partial/main/navbar/author_banner') + } else { + page.title = __('btn.archives') + el += partial('_partial/main/navbar/list_post') + } + el += `
` + var years = [] + site.posts.sort('date', -1).each(function(post) { + post.year = date(post.date, 'YYYY') + if (post.year && (years.includes(post.year) == false) && (post.title || post.date)) { + years.push(post.year) + } + }) + for (let year of years) { + el += `` + } + el += `
` + return el +} +function layoutDiv() { + if (page.posts && (is_category() || is_tag())) { + return partial('index') + } else { + return layoutArchiveList() + } +} %> -<% if (page.posts && (is_category() || is_tag())) { %> - <%- partial('index') %> -<% } else { %> - <% page.title = __('btn.archives'); %> - <%- partial('_partial/main/navbar/list_post') %> -
- <% var years = []; %> - <% site.posts.sort('date', -1).each(function(post) { %> - <% post.year = date(post.date, 'YYYY'); %> - <% if (post.year && (years.includes(post.year) == false) && (post.title || post.date)) { %> - <% years.push(post.year); %> - <% } %> - <% }); %> - <% years.forEach((year, i) => { %> - - <% }); %> -
-<% } %> + +<%- layoutDiv() %> \ No newline at end of file diff --git a/scripts/events/index.js b/scripts/events/index.js index 8c315ce..b7e05c2 100644 --- a/scripts/events/index.js +++ b/scripts/events/index.js @@ -6,6 +6,7 @@ hexo.on('generateBefore', () => { // Merge config. require('./lib/config')(hexo); require('./lib/links')(hexo); + require('./lib/authors')(hexo); require('./lib/doc_tree')(hexo); require('./lib/utils')(hexo); }); diff --git a/scripts/events/lib/authors.js b/scripts/events/lib/authors.js new file mode 100644 index 0000000..d776fd8 --- /dev/null +++ b/scripts/events/lib/authors.js @@ -0,0 +1,21 @@ +/** + * authors.js v2 | https://github.com/xaoxuu/hexo-theme-stellar/ + */ + +'use strict'; + +module.exports = ctx => { + var authors = ctx.locals.get('data').authors || {} + let basePath = ctx.config.author_dir || 'author' + // url + for (let key of Object.keys(authors)) { + let author = authors[key] + author.path = `${basePath}/${key}/index.html` + } + // default author + const keys = Object.keys(authors) + if (keys.length > 0) { + ctx.theme.config.default_author = authors[keys[0]] + } + ctx.theme.config.authors = authors +} \ No newline at end of file diff --git a/scripts/events/lib/links.js b/scripts/events/lib/links.js index 59765f1..65d914d 100644 --- a/scripts/events/lib/links.js +++ b/scripts/events/lib/links.js @@ -1,5 +1,5 @@ /** - * doc_tree.js v2 | https://github.com/xaoxuu/hexo-theme-stellar/ + * links.js v1 | https://github.com/xaoxuu/hexo-theme-stellar/ */ 'use strict'; diff --git a/scripts/generators/author.js b/scripts/generators/author.js new file mode 100644 index 0000000..0af80c0 --- /dev/null +++ b/scripts/generators/author.js @@ -0,0 +1,22 @@ +/** + * author v1 | https://github.com/xaoxuu/hexo-theme-stellar/ + */ + +hexo.extend.generator.register('author', function (locals) { + const { authors } = hexo.theme.config + var pages = [] + for (let key of Object.keys(authors)) { + const author = authors[key] + if (author.hidden) { + continue + } + pages.push({ + path: author.path, + layout: ['archive'], + data: { + 'author': author + } + }) + } + return pages +}); diff --git a/source/css/_layout/list.styl b/source/css/_layout/list.styl index a0a6400..39202ef 100644 --- a/source/css/_layout/list.styl +++ b/source/css/_layout/list.styl @@ -170,4 +170,8 @@ p:last-child margin-bottom: 0 .cap+.cap - margin-left: 4px \ No newline at end of file + margin-left: 4px + +.post-list.archives + @media screen and (max-width: $device-mobile) + margin: 1rem 0 \ No newline at end of file diff --git a/source/css/_layout/main.styl b/source/css/_layout/main.styl index 1e66af0..c11df98 100644 --- a/source/css/_layout/main.styl +++ b/source/css/_layout/main.styl @@ -12,3 +12,7 @@ margin: 2rem 1rem 1rem .logo-wrap margin: 0 + +.l_main.list + @media screen and (max-width: $device-mobile-max) + padding-top: 0 \ No newline at end of file diff --git a/source/css/_layout/pages/archives.styl b/source/css/_layout/pages/archives.styl index 8c17a5e..f5378d1 100644 --- a/source/css/_layout/pages/archives.styl +++ b/source/css/_layout/pages/archives.styl @@ -24,14 +24,18 @@ align-items: baseline margin: 0.25rem 0 color: var(--text-p1) - &:hover - color: $color-hover + trans2 color border + border-bottom: 1px dashed var(--text-meta) time font-family: $ff-code margin-right: 1em font-weight: 700 flex-shrink: 0 - opacity .5 + color: var(--text-p4) + &:hover + border-bottom: 1px solid $color-theme + time + color: $color-theme &:hover .archive-header color: var(--text-p1) diff --git a/source/css/_layout/partial/bread-nav.styl b/source/css/_layout/partial/bread-nav.styl index b80d317..e1751c3 100644 --- a/source/css/_layout/partial/bread-nav.styl +++ b/source/css/_layout/partial/bread-nav.styl @@ -19,13 +19,11 @@ color: $color-hover div#post-meta margin-top: 2px - span+span - margin-left: 8px + span.updated visibility: hidden &:hover - div#post-meta - span+span - visibility: visible + div#post-meta span.updated + visibility: visible .bread-nav .ghrepo font-size: $fs-13 diff --git a/source/css/_layout/tag-plugins/banner.styl b/source/css/_layout/tag-plugins/banner.styl index d5ad30e..2714b45 100644 --- a/source/css/_layout/tag-plugins/banner.styl +++ b/source/css/_layout/tag-plugins/banner.styl @@ -6,7 +6,6 @@ flex-direction: column justify-content: flex-end align-items: flex-start - color: white height: 240px @media screen and (max-width: $device-mobile-max) height: 200px @@ -78,14 +77,11 @@ border-radius: 50% width: 48px height: 48px - border: 2px solid white .bottom display: flex padding: 1rem width: 100% box-sizing: border-box - background-image: linear-gradient(rgba(0,0,0,0),rgba(0,0,0,0.2)) - text-shadow: 0 0 1px rgba(0,0,0,0.12) .title font-size: 1.5rem font-weight: 600 @@ -99,9 +95,27 @@ font-size: 1.2rem .subtitle font-size: $fs-14 + .content:only-child + color: var(--text-p1) + .bg+.content + color: white + .avatar + border: 2px solid white + .bottom + background-image: linear-gradient(rgba(0,0,0,0),rgba(0,0,0,0.2)) + text-shadow: 0 0 1px rgba(0,0,0,0.12) .tag-plugin.banner trans1(transform, 2s) &:hover img.bg - transform: scale(1.01) \ No newline at end of file + transform: scale(1.01) + +.l_main.list .tag-plugin.banner + background: var(--block-hover) + .content .top + margin-top: 1.5rem + margin: 0 1rem + @media screen and (max-width: $device-mobile) + margin: 0 + border-radius: 0 \ No newline at end of file