pengzhanbo 1bf2c82d01 fix: 优化皮肤主题交互
1. 修复caniuse插件在hash更新时重新渲染的问题;
2. 优化皮肤主题样式;
3.修复归档排序问题
2022-05-14 20:31:54 +08:00

88 lines
2.4 KiB
TypeScript

import type { App, Page } from '@vuepress/core'
import * as chokidar from 'chokidar'
import type {
PlumeThemeLocaleOptions,
PlumeThemePageData,
PlumeThemePostFrontmatter,
PostIndex,
PostItem,
} from '../../shared'
import { getCreateTime } from '../utils'
const HMR_CODE = `
if (import.meta.webpackHot) {
import.meta.webpackHot.accept()
if (__VUE_HMR_RUNTIME__.updatePostIndex) {
__VUE_HMR_RUNTIME__.updatePostIndex(postIndex)
}
}
if (import.meta.hot) {
import.meta.hot.accept(({ postIndex }) => {
__VUE_HMR_RUNTIME__.updatePostIndex(postIndex)
})
}
`
export const preparedPostIndex = (
app: App,
localeOption: PlumeThemeLocaleOptions
): void => {
const postIndex: PostIndex = (app.pages as Page<PlumeThemePageData>[])
.filter((page) => {
return (
!!page.pathInferred &&
!!page.filePath &&
page.path !== '/' &&
page.path !== '/404.html'
)
})
.sort((left, right) => {
const leftMatter = left.frontmatter as PlumeThemePostFrontmatter
const rightMatter = right.frontmatter as PlumeThemePostFrontmatter
const leftTime = getCreateTime(leftMatter.createTime || '')
const rightTime = getCreateTime(rightMatter.createTime || '')
return leftTime < rightTime ? 1 : -1
})
.map((page: Page<PlumeThemePageData>) => {
const frontmatter = page.frontmatter as PlumeThemePostFrontmatter
return {
title: page.title,
path: page.path,
excerpt: page.excerpt,
tags: frontmatter.tags || [],
createTime: page.data.createTime,
author: frontmatter.author,
sticky: frontmatter.sticky,
article: frontmatter.article,
category: page.data.category,
isNote: page.data.isNote,
banner: frontmatter.banner,
} as PostItem
})
let content = `
export const postIndex = ${JSON.stringify(postIndex, null, 2)}
`
if (app.env.isDev) {
content += HMR_CODE
}
app.writeTemp('internal/postIndex.js', content)
}
export const watchPostIndex = (
app: App,
watchers: any[],
localeOption: PlumeThemeLocaleOptions
): void => {
const watcher = chokidar.watch('pages/**/*', {
cwd: app.dir.temp(),
ignoreInitial: true,
})
watcher.on('add', () => preparedPostIndex(app, localeOption))
watcher.on('change', () => preparedPostIndex(app, localeOption))
watcher.on('unlink', () => preparedPostIndex(app, localeOption))
watchers.push(watcher)
}