pengzhanbo e04e2a1973 chore: update vuepress dependencies
更新 vuepress 相关依赖至 2.0.0.beta.41,暂时下线hope相关插件

BREAKING CHANGE: 跟随vuepress@2.0.0.beta.41的重大变更
2022-04-26 21:25:36 +08:00

61 lines
1.8 KiB
TypeScript

import type { Page } from '@vuepress/core'
import { path } from '@vuepress/utils'
import type {
CategoryData,
PlumeThemeLocaleOptions,
PlumeThemePageData,
PlumeThemePostFrontmatter,
} from '../shared'
import { formatDate } from './utils'
function normalizePath(filepath: string): string {
return filepath.replace(/^\//, '')
}
let uuid = 10000
const cache: Record<string, number> = {}
export const extendsPage = (
page: Page<Partial<PlumeThemePageData>>,
localeOption: PlumeThemeLocaleOptions
): void => {
const pagePath = page.filePathRelative || ''
if (page.frontmatter.pageType) return
const { notes } = localeOption
const frontmatter = page.frontmatter as PlumeThemePostFrontmatter
let category: CategoryData = []
if (notes && notes.dir && pagePath.startsWith(normalizePath(notes.dir))) {
page.data.isNote = true
const filepath = path.relative(notes.dir, pagePath)
if (filepath) {
const note = filepath.split('/')[0]
const currentNote = notes.notes.find(({ link }) =>
normalizePath(link).startsWith(note)
)
if (currentNote) {
!cache[currentNote.text] && (cache[currentNote.text] = uuid++)
category.push({
type: cache[currentNote.text],
name: currentNote.text,
})
}
}
} else {
category = pagePath
.split('/')
.slice(0, -1)
.map((category) => {
const match = category.match(/^(\d+)?(?:\.?)([^]+)$/) || []
!cache[match[2]] && !match[1] && (cache[match[2]] = uuid++)
return {
type: Number(match[1] || cache[match[2]]),
name: match[2],
}
})
}
page.data.category = category || []
page.data.createTime = formatDate(frontmatter.createTime)
page.data.tags = frontmatter.tags || []
page.data.path = page.path
page.data.author = frontmatter.author || ''
}