Created
August 24, 2024 12:58
-
-
Save otabekoff/cf6305b28b341239d403f6e1e54cba77 to your computer and use it in GitHub Desktop.
Posts data auto create content loader
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { createContentLoader } from "vitepress"; | |
import "dotenv/config"; | |
const formatDate = (raw) => { | |
const date = new Date(raw); | |
date.setUTCHours(12); | |
return { | |
time: +date, | |
string: date.toLocaleDateString("en-US", { | |
year: "numeric", | |
month: "long", | |
day: "numeric", | |
}), | |
}; | |
}; | |
const base = process.env.BASE_URL; | |
// Fallback to '/' if the environment variable is not set | |
const data = createContentLoader("/blog/posts/*.md", { | |
excerpt: true, | |
transform(raw) { | |
return raw | |
.map(({ url, frontmatter, excerpt }) => ({ | |
title: frontmatter.title, | |
url: base === "/dev-docs/" ? "/dev-docs" + url : url, | |
excerpt, | |
date: formatDate(frontmatter.date), | |
})) | |
.sort((a, b) => b.date.time - a.date.time); | |
}, | |
}); | |
export { data }; | |
export default data; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment