Skip to content

feat: improve the matching of multilingual pagination dir #26

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 15, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 25 additions & 14 deletions src/posts.data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,10 @@ export { data }
type GlobalThis = typeof globalThis & { VITEPRESS_CONFIG: SiteConfig<Theme> }

const config = (globalThis as GlobalThis).VITEPRESS_CONFIG
const themeConfig = config.site.themeConfig
const pagination = themeConfig.pagination && toArray(themeConfig.pagination)
const postsDir = pagination?.reduce((all, item) => {
if (Array.isArray(item.dir)) {
all = all.concat(item.dir)
} else if (item.dir) {
all.push(item.dir)
}
return all
}, [] as string[])
const pattern = postsDir?.length
? postsDir.map((item) => `${item}/*.md`)
: `${config.userConfig.srcDir || '**'}/*.md`
const pattern = getPattern()

export default createContentLoader(pattern, {
excerpt: themeConfig.excerpt ?? true,
excerpt: config.site.themeConfig.excerpt ?? true,
transform(raw): PostsItem[] {
const posts: PostsItem[] = []

Expand Down Expand Up @@ -53,3 +41,26 @@ export default createContentLoader(pattern, {
return posts
},
})

function getPattern() {
const dirs = new Set<string>()

if (config.site.themeConfig.pagination) {
toArray(config.site.themeConfig.pagination).forEach((item) => {
item.dir && toArray(item.dir).forEach((item) => dirs.add(item))
})
}
if (config.site.locales.length) {
Object.values(config.site.locales).forEach((locale) => {
if (locale.themeConfig?.pagination) {
toArray(locale.themeConfig.pagination).forEach((item) => {
item.dir && toArray(item.dir).forEach((item) => dirs.add(item))
})
}
})
}

return dirs.size > 0
? [...dirs].map((item) => `${item}/*.md`)
: `${config.userConfig.srcDir || '**'}/*.md`
}