refactor(docs): added content loader
This commit is contained in:
@@ -0,0 +1,45 @@
|
||||
import type { SiteConfig } from 'vitepress'
|
||||
|
||||
import { createContentLoader } from 'vitepress'
|
||||
|
||||
import { formatDate } from './utils'
|
||||
|
||||
const config: SiteConfig = (globalThis as any).VITEPRESS_CONFIG
|
||||
|
||||
interface Document {
|
||||
title: string
|
||||
url: string
|
||||
urlWithoutLang: string
|
||||
lang: string
|
||||
date: ReturnType<typeof formatDate>
|
||||
frontmatter?: Record<string, any>
|
||||
}
|
||||
|
||||
declare const data: Document[]
|
||||
export { data }
|
||||
|
||||
export default createContentLoader('**/*.md', {
|
||||
transform(raw): Document[] {
|
||||
return raw
|
||||
.map(({ url, frontmatter }) => {
|
||||
const foundLanguage = Object.values(config.userConfig.locales!).find((locale) => {
|
||||
let normalizedLanguagePrefix = locale.lang || 'en'
|
||||
if (!normalizedLanguagePrefix.startsWith('/')) {
|
||||
normalizedLanguagePrefix = `/${normalizedLanguagePrefix}`
|
||||
}
|
||||
|
||||
return url.startsWith(normalizedLanguagePrefix)
|
||||
})
|
||||
|
||||
return {
|
||||
title: frontmatter.title,
|
||||
url,
|
||||
urlWithoutLang: url.replace(`/${foundLanguage?.lang || 'en'}`, ''),
|
||||
date: formatDate(frontmatter.date),
|
||||
lang: foundLanguage?.lang || 'en',
|
||||
frontmatter,
|
||||
}
|
||||
})
|
||||
.sort((a, b) => b.date.time - a.date.time)
|
||||
},
|
||||
})
|
||||
@@ -2,6 +2,8 @@ import type { SiteConfig } from 'vitepress'
|
||||
|
||||
import { createContentLoader } from 'vitepress'
|
||||
|
||||
import { formatDate } from './utils'
|
||||
|
||||
const config: SiteConfig = (globalThis as any).VITEPRESS_CONFIG
|
||||
|
||||
interface Post {
|
||||
@@ -9,10 +11,7 @@ interface Post {
|
||||
url: string
|
||||
urlWithoutLang: string
|
||||
lang: string
|
||||
date: {
|
||||
time: number
|
||||
string: string
|
||||
}
|
||||
date: ReturnType<typeof formatDate>
|
||||
excerpt: string | undefined
|
||||
frontmatter?: Record<string, any>
|
||||
}
|
||||
@@ -49,17 +48,3 @@ export default createContentLoader('**/blog/**/*.md', {
|
||||
.sort((a, b) => b.date.time - a.date.time)
|
||||
},
|
||||
})
|
||||
|
||||
function formatDate(raw: string): Post['date'] {
|
||||
const date = new Date(raw)
|
||||
date.setUTCHours(12)
|
||||
|
||||
return {
|
||||
time: +date,
|
||||
string: date.toLocaleDateString('en-US', {
|
||||
year: 'numeric',
|
||||
month: 'long',
|
||||
day: 'numeric',
|
||||
}),
|
||||
}
|
||||
}
|
||||
+3
-18
@@ -2,6 +2,8 @@ import type { SiteConfig } from 'vitepress'
|
||||
|
||||
import { createContentLoader } from 'vitepress'
|
||||
|
||||
import { formatDate } from './utils'
|
||||
|
||||
const config: SiteConfig = (globalThis as any).VITEPRESS_CONFIG
|
||||
|
||||
interface ChronicleEntry {
|
||||
@@ -9,10 +11,7 @@ interface ChronicleEntry {
|
||||
url: string
|
||||
urlWithoutLang: string
|
||||
lang: string
|
||||
date: {
|
||||
time: number
|
||||
string: string
|
||||
}
|
||||
date: ReturnType<typeof formatDate>
|
||||
excerpt: string | undefined
|
||||
frontmatter?: Record<string, any>
|
||||
}
|
||||
@@ -49,17 +48,3 @@ export default createContentLoader('**/chronicles/**/*.md', {
|
||||
.sort((a, b) => b.date.time - a.date.time)
|
||||
},
|
||||
})
|
||||
|
||||
function formatDate(raw: string): ChronicleEntry['date'] {
|
||||
const date = new Date(raw)
|
||||
date.setUTCHours(12)
|
||||
|
||||
return {
|
||||
time: +date,
|
||||
string: date.toLocaleDateString('en-US', {
|
||||
year: 'numeric',
|
||||
month: 'long',
|
||||
day: 'numeric',
|
||||
}),
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
export function formatDate(raw: string): {
|
||||
time: number
|
||||
string: string
|
||||
} {
|
||||
const date = new Date(raw)
|
||||
date.setUTCHours(12)
|
||||
|
||||
return {
|
||||
time: +date,
|
||||
string: date.toLocaleDateString('en-US', {
|
||||
year: 'numeric',
|
||||
month: 'long',
|
||||
day: 'numeric',
|
||||
}),
|
||||
}
|
||||
}
|
||||
@@ -5,7 +5,7 @@ community: false
|
||||
---
|
||||
|
||||
<script setup>
|
||||
import { data } from '../../../.vitepress/theme/blog.data'
|
||||
import { data } from '../../../.vitepress/functions/blog.data'
|
||||
import BlogPosts from '../../../.vitepress/components/BlogPosts.vue'
|
||||
</script>
|
||||
|
||||
|
||||
@@ -5,7 +5,7 @@ community: false
|
||||
---
|
||||
|
||||
<script setup>
|
||||
import { data } from '../../../.vitepress/theme/blog.data'
|
||||
import { data } from '../../../.vitepress/functions/blog.data'
|
||||
import BlogPosts from '../../../.vitepress/components/BlogPosts.vue'
|
||||
</script>
|
||||
|
||||
|
||||
Reference in New Issue
Block a user