Skip to content

Commit

Permalink
feat: improve the matching of multilingual pagination dir (#26)
Browse files Browse the repository at this point in the history
  • Loading branch information
tolking authored Oct 15, 2024
1 parent 09d5ae4 commit 3f2e3e1
Showing 1 changed file with 25 additions and 14 deletions.
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`
}

0 comments on commit 3f2e3e1

Please sign in to comment.