-
Notifications
You must be signed in to change notification settings - Fork 0
feat: get latest version spacedf docs #33
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| import fs from "fs" | ||
| import path from "path" | ||
| import matter from "gray-matter" | ||
|
|
||
| type LatestVersion = { | ||
| slug: string | ||
| version: string | ||
| date?: string | ||
| title?: string | ||
| type?: string | ||
| author?: string | ||
| description?: string | ||
| } | ||
|
|
||
| export const getLatestVersion = async (): Promise<LatestVersion | null> => { | ||
| const POSTS_DIR = path.join(process.cwd(), "src/app/blog/(post)") | ||
|
|
||
| if (!fs.existsSync(POSTS_DIR)) return null | ||
|
|
||
| const results: LatestVersion[] = [] | ||
| const folders = fs.readdirSync(POSTS_DIR) | ||
|
|
||
| for (const folder of folders) { | ||
| const fullPath = path.join(POSTS_DIR, folder, "page.mdx") | ||
| if (!fs.existsSync(fullPath)) continue | ||
|
|
||
| const raw = fs.readFileSync(fullPath, "utf-8") | ||
| if (!raw) continue | ||
|
|
||
| const { data } = matter(raw) | ||
|
|
||
| results.push({ | ||
| ...(data as Omit<LatestVersion, "slug" | "version">), | ||
| version: folder, | ||
| slug: `/blog/${folder}`, | ||
| }) | ||
| } | ||
|
|
||
| const latest = | ||
| results.sort( | ||
| (a, b) => new Date(b.date ?? 0).getTime() - new Date(a.date ?? 0).getTime(), | ||
| )[0] ?? null | ||
|
|
||
| return latest | ||
| } | ||
|
Comment on lines
+1
to
+45
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since import fs from "fs/promises"
import path from "path"
import matter from "gray-matter"
type LatestVersion = {
slug: string
version: string
date?: string
title?: string
type?: string
author?: string
description?: string
}
export const getLatestVersion = async (): Promise<LatestVersion | null> => {
const POSTS_DIR = path.join(process.cwd(), "src/app/blog/(post)")
try {
const stats = await fs.stat(POSTS_DIR)
if (!stats.isDirectory()) return null
} catch {
return null
}
const folders = await fs.readdir(POSTS_DIR)
const results: LatestVersion[] = []
for (const folder of folders) {
const fullPath = path.join(POSTS_DIR, folder, "page.mdx")
try {
const raw = await fs.readFile(fullPath, "utf-8")
const { data } = matter(raw)
results.push({
...(data as Omit<LatestVersion, "slug" | "version">),
version: folder,
slug: "/blog/" + folder,
})
} catch {
continue
}
}
return (
results.sort((a, b) => {
const timeA = a.date ? new Date(a.date).getTime() : 0
const timeB = b.date ? new Date(b.date).getTime() : 0
return timeB - timeA
})[0] ?? null
)
} |
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The component should return
nulliflatestVersionor its version property is missing. This prevents rendering an empty styled label and avoids navigating to an invalid URL (e.g.,/blog/undefined).