处理文章数据

This commit is contained in:
xugaoyi 2020-04-26 18:03:46 +08:00
parent 3f63dcc3f2
commit e188ce2b19
15 changed files with 145 additions and 66 deletions

View File

@ -1,13 +0,0 @@
---
title: test
date: 2020-04-25 12:12:39
permalink: /pages/463181
categories:
- 学习
- 更多
tags:
-
---

View File

@ -1,12 +0,0 @@
---
title: test
date: 2020-04-25 12:12:31
permalink: /pages/961600
categories:
- 关于
tags:
-
---

View File

@ -1,13 +0,0 @@
---
title: test
date: 2020-04-25 12:03:59
permalink: /pages/4cecec
categories:
- 收藏夹
tags:
-
---

View File

@ -1,6 +1,6 @@
---
title: test
date: 2020-04-25 11:31:59
date: "2020-04-25 11:31:59"
permalink: /pages/ea1541
categories:
- 博文

View File

@ -6,6 +6,7 @@ categories:
- 博文
tags:
-
sticky: 1
---
# 碎片化文章2

View File

@ -66,6 +66,6 @@ $mobileSidebarWidth = $sidebarWidth * 0.82
.no-sidebar .footer
width auto
padding-left 0
padding-left 1.5rem
</style>

View File

@ -104,9 +104,12 @@
import NavLink from "@theme/components/NavLink";
import BScroll from "@better-scroll/core"
import Slide from "@better-scroll/slide"
import UpdateArticle from './UpdateArticle'
import PostList from './PostList'
import Footer from './Footer'
import UpdateArticle from '@theme/components/UpdateArticle'
import PostList from '@theme/components/PostList'
import Footer from '@theme/components/Footer'
import { filterPosts, sortPosts, getAllCategoriesAndTags } from '../util/postData'
const MOBILE_DESKTOP_BREAKPOINT = 720 // refer to config.styl
BScroll.use(Slide)
@ -126,7 +129,8 @@ export default {
created() {
this.updateBarConfig = this.$themeConfig.updateBar
this.social = this.$themeConfig.social
console.log(this.$site.pages)
// console.log(filterPosts(this.$site.pages))
getAllCategoriesAndTags(this.$site.pages)
},
beforeMount(){
this.isMQMobile = window.innerWidth < MOBILE_DESKTOP_BREAKPOINT ? true : false; // vupressbeforeCreate(),created()访apiwindow

View File

@ -50,7 +50,6 @@ export default {
props: ['sidebarItems'],
components: { PageEdit, PageNav, ArticleInfo, Catalogue, UpdateArticle, Timeline, Footer, RightMenu},
created() {
console.log(this.$themeConfig)
this.updateBarConfig = this.$themeConfig.updateBar
},
computed: {
@ -74,7 +73,6 @@ export default {
<style lang="stylus">
@require '../styles/wrapper.styl'
@require '../styles/variable.styl'
.page
padding-bottom 2rem

View File

@ -44,8 +44,7 @@ export default {
</script>
<style lang='stylus'>
@require '../styles/variable.styl'
.right-menu-wrapper
width $rightMenuWidth
float right

View File

@ -115,12 +115,12 @@ export default {
dd
font-size 1.1rem
color #F17229
width 50px
width 45px
margin-left 22px
font-weight bold
line-height: 50px;
@media (max-width: $MQNarrow)
width 50px
line-height: 45px;
// @media (max-width: $MQNarrow)
// width 45px
dt
flex 1
display flex
@ -142,12 +142,10 @@ export default {
&.more
color $accentColor
span
width 100px
width 50px
margin-right 15px
color #999
text-align right
font-size .9rem
line-height: 50px;
@media (max-width: $MQNarrow)
width 95px
</style>

View File

@ -4,6 +4,27 @@ export default ({
router, // 当前应用的路由实例
siteData // 站点元数据
}) => {
// siteData.test = '123'
// console.log(siteData)
// 修复ISO8601时间格式为普通时间格式
siteData.pages.map(item => {
const { frontmatter: { date } } = item
if (typeof date === 'string' && date.charAt(date.length-1) === 'Z') {
item.frontmatter.date = repairUTCDate(date)
}
})
}
// 修复ISO8601时间格式为普通时间格式
function repairUTCDate(date) {
if (!(date instanceof Date)) {
date = new Date(date)
}
return `${date.getUTCFullYear()}-${zero(date.getUTCMonth()+1)}-${zero(date.getUTCDate())} ${zero(date.getUTCHours())}:${zero(date.getUTCMinutes())}:${zero(date.getUTCSeconds())}`;
}
// 小于10补0
function zero(d){
return d.toString().padStart(2,'0')
}

View File

@ -1,6 +0,0 @@
// // vdoing
// $rightMenuWidth = 280px //
// :root
// --homePageWidth: 1080px //

View File

@ -245,8 +245,13 @@ function resolveItem (item, pages, base, groupDepth = 1) {
}
// 类型判断
export function type(o){
const s = Object.prototype.toString.call(o)
return s.match(/\[object (.*?)\]/)[1].toLowerCase()
}
// 日期格式化
// 日期格式化(只获取年月日)
export function dateFormat(date) {
if (!(date instanceof Date)) {
date = new Date(date)
@ -255,6 +260,16 @@ export function dateFormat(date) {
}
// 小于10补0
function zero(d){
export function zero(d){
return d.toString().padStart(2,'0')
}
}
// 获取时间的时间戳
export function getTimeNum (post) {
return new Date(post.frontmatter.date || timeline.lastUpdated).getTime()
}
// 比对时间
export function compareDate (a, b) {
return getTimeNum(b) - getTimeNum(a)
}

View File

@ -0,0 +1,87 @@
// 处理文章数据
/**
* 思路过滤非文章页按时间排序置顶靠前 按分类标签对数据分组解析出所有分类数量取分组post的长度标签
*/
import { type, compareDate } from './index'
const log = console.log
// 过滤非文章页
export function filterPosts (posts) {
posts = posts.filter(item => {
const { frontmatter: { pageComponent, article, home }} = item
return !(pageComponent || article === false || home === true) // 存在页面组件、article字段为false以及首页
})
return posts
}
// 按时间排序(并且置顶靠前)
export function sortPosts (posts, isSticky = true) {
posts = filterPosts(posts) // 过滤非文章页
posts.sort((prev, next) => {
if (isSticky) { // 是否需要置顶靠前
const prevSticky = prev.frontmatter.sticky
const nextSticky = next.frontmatter.sticky
if (prevSticky && nextSticky) {
return prevSticky == nextSticky ? compareDate(prev, next) : (prevSticky - nextSticky)
} else if (prevSticky && !nextSticky) {
return -1
} else if (!prevSticky && nextSticky) {
return 1
}
}
return compareDate(prev, next)
})
return posts
}
// 按分类和标签分组
export function groupByCategoriesAndTags(posts) {
const categoriesArr = []
const tagsArr = []
posts = sortPosts(posts, false)
for (let i = 0, postsL = posts.length; i < postsL; i++) {
}
return {
categories: categoriesArr,
tags: tagsArr
}
}
// 获取所有分类和标签
export function getAllCategoriesAndTags(posts) {
const categoriesArr = []
const tagsArr = []
// posts = sortPosts(posts, false)
// for (let i = 0, postsL = posts.length; i < postsL; i++) {
// const { frontmatter: { categories, tags }} = posts[i]
// if (type(categories) === 'array') {
// categories.forEach(item => {
// item && categoriesArr.indexOf(item) === -1 && categoriesArr.push(item)
// })
// }
// if (type(tags) === 'array') {
// tags.forEach(item => {
// item && tagsArr.indexOf(item) === -1 && tagsArr.push(item)
// })
// }
// }
return {
categories: categoriesArr,
tags: tagsArr
}
}

View File

@ -11,4 +11,4 @@ delete:
# 要添加、修改front matter的数据 front matter中没有的数据则添加已有的数据则覆盖
data:
author: 阮一峰
# author: 阮一峰