From 397b3b50543c6e0743af41c5b2693516d903c492 Mon Sep 17 00:00:00 2001 From: TriDiamond Date: Thu, 8 Apr 2021 15:38:38 +0800 Subject: [PATCH 1/9] Added separate generation for author. (#26) - Now article detail page shows the author of the post - Stats are generated separately base on author --- _config.yml | 8 ++ scripts/lib/generators/index.js | 1 + scripts/lib/generators/post.js | 72 ++++++++++++++ scripts/lib/helpers/mapper.js | 43 +++++++-- scripts/lib/helpers/utils.js | 9 ++ src/api/index.ts | 8 ++ src/components/PageContainer.vue | 2 +- src/components/Sidebar/src/Profile.vue | 126 ++++++++++++++++++------- src/models/Post.class.ts | 35 +++++++ src/stores/author.ts | 20 ++++ src/views/Home.vue | 3 +- src/views/Post.vue | 2 +- 12 files changed, 288 insertions(+), 41 deletions(-) create mode 100644 src/stores/author.ts diff --git a/_config.yml b/_config.yml index b25b16f0..b437844d 100644 --- a/_config.yml +++ b/_config.yml @@ -26,6 +26,14 @@ site: #! @docs https://obsidianext.tridiamond.tech/guide/authors.html #! --------------------------------------------------------------- authors: + ##! example + # TriDiamond: + # name: TriDiamond + # avatar: https://up.enterdesk.com/edpic_source/44/ff/3d/44ff3d6bd2819d524facfcc33205d4cd.jpg + # link: https://github.com/TriDiamond + # description: 'Think like an artist, code like an artisan.' + # socials: + # github: https://tridiamond.tech #! --------------------------------------------------------------- #! Menu Configs diff --git a/scripts/lib/generators/index.js b/scripts/lib/generators/index.js index e272331a..55f0a226 100644 --- a/scripts/lib/generators/index.js +++ b/scripts/lib/generators/index.js @@ -92,6 +92,7 @@ module.exports = function (hexo) { apiData = posts.addPaginationPost(apiData) apiData = posts.addArticles(apiData) apiData = posts.addFeatures(apiData) + apiData = posts.addAuthorPost(apiData) const categories = new CategoryGenerator( site.categories, diff --git a/scripts/lib/generators/post.js b/scripts/lib/generators/post.js index 3ef468de..0bc789d4 100644 --- a/scripts/lib/generators/post.js +++ b/scripts/lib/generators/post.js @@ -1,4 +1,5 @@ const { postMapper, postListMapper } = require('../helpers/mapper') +const { formatNumber } = require('../helpers/utils') class PostGenerator { data = [] @@ -6,6 +7,7 @@ class PostGenerator { features = [] configs = {} authors = {} + postByAuthors = new Map() constructor(posts, configs) { this.data = posts @@ -46,6 +48,8 @@ class PostGenerator { featureIndexes.length < featureLimit ) featureIndexes.push(index) + + this.fillAuthorPost(current) }) // Save the feature post data. @@ -73,6 +77,50 @@ class PostGenerator { }) } + /** + * + * @param {*} post + */ + fillAuthorPost(post) { + let authorPostData = {} + + if (!this.postByAuthors.has(post.author.slug)) { + Object.assign(authorPostData, post.author) + // authorPostData = Object.create(post.author) + authorPostData.post_list = [postListMapper(post, this.configs)] + authorPostData.categories = new Set() + authorPostData.tags = new Set() + authorPostData.word_count = 0 + authorPostData.post_count = 0 + } else { + authorPostData = this.postByAuthors.get(post.author.slug) + authorPostData.post_list.push(postListMapper(post, this.configs)) + } + + let wordCount = post.count_time.symbolsCount + + if (String(wordCount).indexOf('k') > -1) { + wordCount = Number(String(wordCount).replace(/[k]+/g, '')) * 1000 + } + + authorPostData.word_count += Number(wordCount) + authorPostData.post_count += 1 + + if (post.categories && post.categories.length > 0) { + post.categories.forEach(function (category) { + authorPostData.categories.add(category.name) + }) + } + + if (post.categories && post.categories.length > 0) { + post.tags.forEach(function (tag) { + authorPostData.tags.add(tag) + }) + } + + this.postByAuthors.set(post.author.slug, authorPostData) + } + /** * Adding post pagination API data * @returns Array @@ -134,6 +182,30 @@ class PostGenerator { return data } + /** + * Creating Authors API data + * @returns Array + */ + addAuthorPost(data) { + if (this.postByAuthors.size <= 0) return data + + const postData = [] + this.postByAuthors.forEach(function (value, key) { + const path = `api/authors/${key}.json` + value.categories = value.categories.size + value.tags = value.tags.size + value.word_count = formatNumber(value.word_count) + + postData.push({ + path: path, + data: JSON.stringify(value) + }) + }) + + data = data.concat(postData) + return data + } + count() { return this.data.length } diff --git a/scripts/lib/helpers/mapper.js b/scripts/lib/helpers/mapper.js index d6bdc39c..a48fbde2 100644 --- a/scripts/lib/helpers/mapper.js +++ b/scripts/lib/helpers/mapper.js @@ -39,15 +39,46 @@ function authorMapper(author, configs) { const configAuthors = configs.theme_config.authors if (typeof author === 'string' && configAuthors[author]) { - return configAuthors[author] + return authorAttributes(configAuthors[author]) } else if (typeof author === 'object') { - return author + return authorAttributes(author) } else { - return { + return authorAttributes({ name: configs.theme_config.site.author, - avatar: configs.theme_config.site.avatar, - link: '' - } + slug: 'blog-author', + avatar: + configs.theme_config.site.avatar || configs.theme_config.site.logo, + link: '', + description: configs.theme_config.site.description, + socials: configs.theme_config.socials + }) + } +} + +function authorAttributes(author) { + return { + name: author.name, + slug: + author.slug || + String(author.name).toLocaleLowerCase().replace(/[\s]+/g, '-'), + avatar: author.avatar || '', + link: author.link || '', + description: author.description || '', + socials: author.socials ? socialMapper(author.socials) : {} + } +} + +function socialMapper(socials) { + return { + github: socials.github || '', + twitter: socials.twitter || '', + stackoverflow: socials.stackoverflow || '', + wechat: socials.wechat || '', + qq: socials.qq || '', + weibo: socials.weibo || '', + zhihu: socials.zhihu || '', + csdn: socials.csdn || '', + juejin: socials.juejin || '' } } diff --git a/scripts/lib/helpers/utils.js b/scripts/lib/helpers/utils.js index 790d7626..14ec279d 100644 --- a/scripts/lib/helpers/utils.js +++ b/scripts/lib/helpers/utils.js @@ -120,3 +120,12 @@ exports.isEmptyObject = function (value) { for (const key in value) return false return true } + +exports.formatNumber = function (value) { + if (value > 9999) { + value = Math.round(value / 1000) + 'k' // > 9999 => 11k + } else if (symbolsResult > 999) { + value = Math.round(value / 100) / 10 + 'k' // > 999 => 1.1k + } // < 999 => 111 + return value +} diff --git a/src/api/index.ts b/src/api/index.ts index 823fb744..edde93ef 100644 --- a/src/api/index.ts +++ b/src/api/index.ts @@ -3,6 +3,7 @@ import request from '@/utils/request' import { HexoConfig } from '@/models/HexoConfig.class' import { AxiosResponse } from 'axios' import { + AuthorPosts, Categories, Post, PostList, @@ -78,3 +79,10 @@ export async function fetchStatistic(): Promise> { export async function fetchSearchIndexes(): Promise> { return request.get('/search.json') } + +// GET /api/authors/author-slug.json +export async function fetchAuthorPost( + slug: string +): Promise> { + return request.get(`/authors/${slug}.json`) +} diff --git a/src/components/PageContainer.vue b/src/components/PageContainer.vue index 699beca3..1e4a84bd 100644 --- a/src/components/PageContainer.vue +++ b/src/components/PageContainer.vue @@ -52,7 +52,7 @@
- +
diff --git a/src/components/Sidebar/src/Profile.vue b/src/components/Sidebar/src/Profile.vue index 16fc3336..853a031f 100644 --- a/src/components/Sidebar/src/Profile.vue +++ b/src/components/Sidebar/src/Profile.vue @@ -11,18 +11,16 @@ >
avatar

- - - - - - - - - - - - - - - - - - - + - + - + - + - + - + - + - css: - - - - + - + - - - - + +plugins: + gitalk: + - + - + valine: + - + - diff --git a/data/en.yml b/data/en.yml index 7401b7bc..959704a3 100644 --- a/data/en.yml +++ b/data/en.yml @@ -1,17 +1,21 @@ scripts: - - - - - - - - - - - - - - - - - - - + - + - + - + - + - + - css: - - - - - - + - + - + - + +plugins: + gitalk: + - + - + valine: + - + - diff --git a/public/index.html b/public/index.html index 179a8b59..b50a0a9b 100644 --- a/public/index.html +++ b/public/index.html @@ -6,46 +6,53 @@ - - - - - - + + + + + - - + + - - - - - - - - -