处理文章数据
This commit is contained in:
parent
3f63dcc3f2
commit
e188ce2b19
|
|
@ -1,13 +0,0 @@
|
||||||
---
|
|
||||||
title: test
|
|
||||||
date: 2020-04-25 12:12:39
|
|
||||||
permalink: /pages/463181
|
|
||||||
categories:
|
|
||||||
- 学习
|
|
||||||
- 更多
|
|
||||||
tags:
|
|
||||||
-
|
|
||||||
---
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -1,12 +0,0 @@
|
||||||
---
|
|
||||||
title: test
|
|
||||||
date: 2020-04-25 12:12:31
|
|
||||||
permalink: /pages/961600
|
|
||||||
categories:
|
|
||||||
- 关于
|
|
||||||
tags:
|
|
||||||
-
|
|
||||||
---
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -1,13 +0,0 @@
|
||||||
---
|
|
||||||
title: test
|
|
||||||
date: 2020-04-25 12:03:59
|
|
||||||
permalink: /pages/4cecec
|
|
||||||
categories:
|
|
||||||
- 收藏夹
|
|
||||||
tags:
|
|
||||||
-
|
|
||||||
---
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -1,6 +1,6 @@
|
||||||
---
|
---
|
||||||
title: test
|
title: test
|
||||||
date: 2020-04-25 11:31:59
|
date: "2020-04-25 11:31:59"
|
||||||
permalink: /pages/ea1541
|
permalink: /pages/ea1541
|
||||||
categories:
|
categories:
|
||||||
- 博文
|
- 博文
|
||||||
|
|
|
||||||
|
|
@ -6,6 +6,7 @@ categories:
|
||||||
- 博文
|
- 博文
|
||||||
tags:
|
tags:
|
||||||
-
|
-
|
||||||
|
sticky: 1
|
||||||
---
|
---
|
||||||
|
|
||||||
# 碎片化文章2
|
# 碎片化文章2
|
||||||
|
|
|
||||||
|
|
@ -66,6 +66,6 @@ $mobileSidebarWidth = $sidebarWidth * 0.82
|
||||||
|
|
||||||
.no-sidebar .footer
|
.no-sidebar .footer
|
||||||
width auto
|
width auto
|
||||||
padding-left 0
|
padding-left 1.5rem
|
||||||
|
|
||||||
</style>
|
</style>
|
||||||
|
|
|
||||||
|
|
@ -104,9 +104,12 @@
|
||||||
import NavLink from "@theme/components/NavLink";
|
import NavLink from "@theme/components/NavLink";
|
||||||
import BScroll from "@better-scroll/core"
|
import BScroll from "@better-scroll/core"
|
||||||
import Slide from "@better-scroll/slide"
|
import Slide from "@better-scroll/slide"
|
||||||
import UpdateArticle from './UpdateArticle'
|
import UpdateArticle from '@theme/components/UpdateArticle'
|
||||||
import PostList from './PostList'
|
import PostList from '@theme/components/PostList'
|
||||||
import Footer from './Footer'
|
import Footer from '@theme/components/Footer'
|
||||||
|
import { filterPosts, sortPosts, getAllCategoriesAndTags } from '../util/postData'
|
||||||
|
|
||||||
|
|
||||||
const MOBILE_DESKTOP_BREAKPOINT = 720 // refer to config.styl
|
const MOBILE_DESKTOP_BREAKPOINT = 720 // refer to config.styl
|
||||||
|
|
||||||
BScroll.use(Slide)
|
BScroll.use(Slide)
|
||||||
|
|
@ -126,7 +129,8 @@ export default {
|
||||||
created() {
|
created() {
|
||||||
this.updateBarConfig = this.$themeConfig.updateBar
|
this.updateBarConfig = this.$themeConfig.updateBar
|
||||||
this.social = this.$themeConfig.social
|
this.social = this.$themeConfig.social
|
||||||
console.log(this.$site.pages)
|
// console.log(filterPosts(this.$site.pages))
|
||||||
|
getAllCategoriesAndTags(this.$site.pages)
|
||||||
},
|
},
|
||||||
beforeMount(){
|
beforeMount(){
|
||||||
this.isMQMobile = window.innerWidth < MOBILE_DESKTOP_BREAKPOINT ? true : false; // vupress在打包时不能在beforeCreate(),created()访问浏览器api(如window)
|
this.isMQMobile = window.innerWidth < MOBILE_DESKTOP_BREAKPOINT ? true : false; // vupress在打包时不能在beforeCreate(),created()访问浏览器api(如window)
|
||||||
|
|
|
||||||
|
|
@ -50,7 +50,6 @@ export default {
|
||||||
props: ['sidebarItems'],
|
props: ['sidebarItems'],
|
||||||
components: { PageEdit, PageNav, ArticleInfo, Catalogue, UpdateArticle, Timeline, Footer, RightMenu},
|
components: { PageEdit, PageNav, ArticleInfo, Catalogue, UpdateArticle, Timeline, Footer, RightMenu},
|
||||||
created() {
|
created() {
|
||||||
console.log(this.$themeConfig)
|
|
||||||
this.updateBarConfig = this.$themeConfig.updateBar
|
this.updateBarConfig = this.$themeConfig.updateBar
|
||||||
},
|
},
|
||||||
computed: {
|
computed: {
|
||||||
|
|
@ -74,7 +73,6 @@ export default {
|
||||||
|
|
||||||
<style lang="stylus">
|
<style lang="stylus">
|
||||||
@require '../styles/wrapper.styl'
|
@require '../styles/wrapper.styl'
|
||||||
@require '../styles/variable.styl'
|
|
||||||
|
|
||||||
.page
|
.page
|
||||||
padding-bottom 2rem
|
padding-bottom 2rem
|
||||||
|
|
|
||||||
|
|
@ -44,8 +44,7 @@ export default {
|
||||||
</script>
|
</script>
|
||||||
|
|
||||||
<style lang='stylus'>
|
<style lang='stylus'>
|
||||||
@require '../styles/variable.styl'
|
|
||||||
|
|
||||||
.right-menu-wrapper
|
.right-menu-wrapper
|
||||||
width $rightMenuWidth
|
width $rightMenuWidth
|
||||||
float right
|
float right
|
||||||
|
|
|
||||||
|
|
@ -115,12 +115,12 @@ export default {
|
||||||
dd
|
dd
|
||||||
font-size 1.1rem
|
font-size 1.1rem
|
||||||
color #F17229
|
color #F17229
|
||||||
width 50px
|
width 45px
|
||||||
margin-left 22px
|
margin-left 22px
|
||||||
font-weight bold
|
font-weight bold
|
||||||
line-height: 50px;
|
line-height: 45px;
|
||||||
@media (max-width: $MQNarrow)
|
// @media (max-width: $MQNarrow)
|
||||||
width 50px
|
// width 45px
|
||||||
dt
|
dt
|
||||||
flex 1
|
flex 1
|
||||||
display flex
|
display flex
|
||||||
|
|
@ -142,12 +142,10 @@ export default {
|
||||||
&.more
|
&.more
|
||||||
color $accentColor
|
color $accentColor
|
||||||
span
|
span
|
||||||
width 100px
|
width 50px
|
||||||
margin-right 15px
|
margin-right 15px
|
||||||
color #999
|
color #999
|
||||||
text-align right
|
text-align right
|
||||||
font-size .9rem
|
font-size .9rem
|
||||||
line-height: 50px;
|
line-height: 50px;
|
||||||
@media (max-width: $MQNarrow)
|
|
||||||
width 95px
|
|
||||||
</style>
|
</style>
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,27 @@ export default ({
|
||||||
router, // 当前应用的路由实例
|
router, // 当前应用的路由实例
|
||||||
siteData // 站点元数据
|
siteData // 站点元数据
|
||||||
}) => {
|
}) => {
|
||||||
// siteData.test = '123'
|
|
||||||
// console.log(siteData)
|
// 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')
|
||||||
}
|
}
|
||||||
|
|
@ -1,6 +0,0 @@
|
||||||
|
|
||||||
// // vdoing主题新增变量
|
|
||||||
// $rightMenuWidth = 280px // 右侧菜单
|
|
||||||
|
|
||||||
// :root
|
|
||||||
// --homePageWidth: 1080px // 首页内容宽
|
|
||||||
|
|
@ -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) {
|
export function dateFormat(date) {
|
||||||
if (!(date instanceof Date)) {
|
if (!(date instanceof Date)) {
|
||||||
date = new Date(date)
|
date = new Date(date)
|
||||||
|
|
@ -255,6 +260,16 @@ export function dateFormat(date) {
|
||||||
}
|
}
|
||||||
|
|
||||||
// 小于10补0
|
// 小于10补0
|
||||||
function zero(d){
|
export function zero(d){
|
||||||
return d.toString().padStart(2,'0')
|
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)
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
@ -11,4 +11,4 @@ delete:
|
||||||
|
|
||||||
# 要添加、修改front matter的数据 (front matter中没有的数据则添加,已有的数据则覆盖)
|
# 要添加、修改front matter的数据 (front matter中没有的数据则添加,已有的数据则覆盖)
|
||||||
data:
|
data:
|
||||||
author: 阮一峰
|
# author: 阮一峰
|
||||||
Loading…
Reference in New Issue