vuejs / vitepress

Vite & Vue powered static site generator.
https://vitepress.dev
MIT License
11.48k stars 1.86k forks source link

Feature Request: Add Support for Retrieving Frontmatter Data from Multiple Markdown Files #3710

Closed otabekoff closed 1 month ago

otabekoff commented 1 month ago

Is your feature request related to a problem? Please describe.

Currently, Vitepress provides functions like usePage or frontmatter to retrieve frontmatter data of the rendered page. However, there is no built-in functionality to retrieve frontmatter data of multiple Markdown files within a directory.

Imagine I'm crafting a custom layout, possibly for a portfolio or blog page. Within my project directory, I have folders like project/blog/ or project/portfolio alongside the main index.md. Inside these directories lie numerous Markdown files, each containing frontmatter data similar to this:

---
title: Blog Post Title
date: 2024-03-27
author: John Doe
tags: [blog, update]
layout: blog
---

# Blog Post Title

This is the content of your blog post, written in Markdown format.

Lorem ipsum dolor sit amet, consectetur adipiscing elit. Sed eget turpis nec leo euismod fringilla vitae ut quam.

However, my challenge arises when attempting to access the frontmatter data of all these files. While Vitepress offers functions like usePage or frontmatter to retrieve individual page data, there isn't an equivalent function like usePages to access data from multiple files simultaneously.

Describe the solution you'd like

I would like to propose the addition of a new function or hook, such as usePages, that allows developers to retrieve frontmatter data of all Markdown files within a specified directory. This would provide more flexibility and enable the creation of custom layouts for portfolio or blog-related pages.

Describe alternatives you've considered

As an alternative, developers currently have to manually parse Markdown files using Node.js scripts before building the Vitepress project. However, this approach is less convenient and may not integrate seamlessly with the Vitepress development workflow.

Additional context

By adding support for retrieving frontmatter data of multiple Markdown files, developers can streamline the development process and create more dynamic and customizable layouts for their Vitepress projects. This feature would enhance the flexibility and usability of Vitepress, making it even more appealing for building static websites and blogs.

Validations

brc-dd commented 1 month ago

https://vitepress.dev/guide/data-loading#createcontentloader in a data loader file is the recommended way for that. However you can also do something like #1450