Skip to content

Commit 3f2e3e1

Browse files
authored
feat: improve the matching of multilingual pagination dir (#26)
1 parent 09d5ae4 commit 3f2e3e1

File tree

1 file changed

+25
-14
lines changed

1 file changed

+25
-14
lines changed

src/posts.data.ts

+25-14
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,10 @@ export { data }
99
type GlobalThis = typeof globalThis & { VITEPRESS_CONFIG: SiteConfig<Theme> }
1010

1111
const config = (globalThis as GlobalThis).VITEPRESS_CONFIG
12-
const themeConfig = config.site.themeConfig
13-
const pagination = themeConfig.pagination && toArray(themeConfig.pagination)
14-
const postsDir = pagination?.reduce((all, item) => {
15-
if (Array.isArray(item.dir)) {
16-
all = all.concat(item.dir)
17-
} else if (item.dir) {
18-
all.push(item.dir)
19-
}
20-
return all
21-
}, [] as string[])
22-
const pattern = postsDir?.length
23-
? postsDir.map((item) => `${item}/*.md`)
24-
: `${config.userConfig.srcDir || '**'}/*.md`
12+
const pattern = getPattern()
2513

2614
export default createContentLoader(pattern, {
27-
excerpt: themeConfig.excerpt ?? true,
15+
excerpt: config.site.themeConfig.excerpt ?? true,
2816
transform(raw): PostsItem[] {
2917
const posts: PostsItem[] = []
3018

@@ -53,3 +41,26 @@ export default createContentLoader(pattern, {
5341
return posts
5442
},
5543
})
44+
45+
function getPattern() {
46+
const dirs = new Set<string>()
47+
48+
if (config.site.themeConfig.pagination) {
49+
toArray(config.site.themeConfig.pagination).forEach((item) => {
50+
item.dir && toArray(item.dir).forEach((item) => dirs.add(item))
51+
})
52+
}
53+
if (config.site.locales.length) {
54+
Object.values(config.site.locales).forEach((locale) => {
55+
if (locale.themeConfig?.pagination) {
56+
toArray(locale.themeConfig.pagination).forEach((item) => {
57+
item.dir && toArray(item.dir).forEach((item) => dirs.add(item))
58+
})
59+
}
60+
})
61+
}
62+
63+
return dirs.size > 0
64+
? [...dirs].map((item) => `${item}/*.md`)
65+
: `${config.userConfig.srcDir || '**'}/*.md`
66+
}

0 commit comments

Comments
 (0)