fix(theme): fix post category sort error (#727)

This commit is contained in:
pengzhanbo 2025-10-15 14:00:28 +08:00 committed by GitHub
parent 4abc1eeb58
commit 707d534b95
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 9 additions and 7 deletions

View File

@ -1,6 +1,7 @@
import type { Page } from 'vuepress/core'
import type { PostsCategoryItem, ThemePageData } from '../../shared/index.js'
import { ensureEndingSlash, ensureLeadingSlash, removeLeadingSlash } from '@vuepress/helper'
import { ensureEndingSlash, ensureLeadingSlash } from '@vuepress/helper'
import { path } from 'vuepress/utils'
import { findCollection } from '../collections/index.js'
import { hash } from '../utils/index.js'
@ -21,11 +22,11 @@ export function autoCategory(page: Page<ThemePageData>): void {
if (page.data.type || !pagePath || collection.categories === false)
return
const collectionDir = ensureEndingSlash(path.join(page.pathLocale, collection.dir))
const list = ensureLeadingSlash(pagePath)
.slice(page.pathLocale.length + ensureEndingSlash(removeLeadingSlash(collection.dir)).length)
.slice(collectionDir.length)
.split('/')
.slice(0, -1)
const categoryList: PostsCategoryItem[] = list
.map((category, index) => {
const match = category.match(RE_CATEGORY) || []

View File

@ -6,9 +6,9 @@ import type {
ThemePosts,
ThemePostsItem,
} from '../../shared/index.js'
import fs from 'node:fs'
import { removeLeadingSlash } from '@vuepress/helper'
import dayjs from 'dayjs'
import { fs, path } from 'vuepress/utils'
import { getThemeConfig } from '../loadConfig/index.js'
import { createMatcher, logger, perf, resolveContent, withBase, writeTemp } from '../utils/index.js'
import { isEncryptPage } from './prepareEncrypt.js'
@ -97,10 +97,11 @@ export async function preparedPostsData(app: App): Promise<void> {
continue
for (const { include, exclude, dir } of collections.filter(item => item.type === 'post')) {
const source = app.dir.source(removeLeadingSlash(withBase(dir, locale)))
const isMatched = createMatcher(include, exclude, source)
const isMatched = createMatcher(include, exclude)
postsData[withBase(dir, locale)] = pages
.filter(({ filePath }) => filePath?.startsWith(source) && isMatched(filePath))
.filter(({ filePath }) => {
return filePath?.startsWith(source) && isMatched(path.relative(source, filePath!))
})
.sort(sortPage)
.map(page => processPostData(page, isBuild, encrypt))
}