question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Metadata for collections of pages (blogs etc.)

See original GitHub issue

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

I would like to be able to display a collection of pages (e.g. my latest blog posts or an alphabetized list of API docs). VuePress has $site.pages, but VitePress does not ship metadata about all pages, which is great except when I need that data for certain pages.

Describe the solution you’d like

I would like to specify collections of pages that I need metadata for in config.js, for example:

module.exports = {
  title: "My Tech Blog",
  collections: [
    {
      name: 'blog',
      directory: '_posts/',
      layout: 'BlogPostLayout',
    },
    {
      name: 'api',
      directory: 'guide/api/',
      layout: 'TechDocLayout',
    }
  ],
};

This would produce an array of metadata for pages inside the _posts directory could then be accessed via $site.collections.blog

I’ve also included a layout option that could be used to define a default layout for pages in that collection. That’s a separate idea, but the point is that collections could have additional benefits.

You could possibly specify which metadata you need - e.g. you may or may not need the frontmatter for every page in the collection.

I’ve borrowed the term “collections” from NetlifyCMS, which I use with VuePress currently.

Describe alternatives you’ve considered

Alternatively, you could simply have a config option to ship metadata for all pages, but that would be all or nothing.

Additional context

Here’s an example of how I’ve implemented collections in a VuePress theme: themeConfig.js, PostList.vue, GlobalLayout.vue

Issue Analytics

  • State:open
  • Created 3 years ago
  • Reactions:8
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

6reactions
brc-ddcommented, Aug 3, 2022

I just tested writing a blog with VitePress. It was quite easy actually. I was able to write a short script that generated a json file for all articles:

import fs from 'node:fs/promises'
import matter from 'gray-matter'
import removeMd from 'remove-markdown'

const articles = await fs.readdir('./blog/')

const data = await Promise.all(
  articles.map(async (article) => {
    const file = matter.read(`./blog/${article}`, {
      excerpt: true,
      excerpt_separator: '<!-- more -->'
    })

    const { data, excerpt, path } = file
    const contents = removeMd(excerpt).trim().split(/\r\n|\n|\r/)

    return {
      ...data,
      title: contents[0].replace(/\s{2,}/g, '').trim(),
      path: path.replace(/\.md$/, '.html'),
      excerpt: contents.slice(1).join('').replace(/\s{2,}/g, '').trim()
    }
  })
)

await fs.writeFile('./data.json', JSON.stringify(data), 'utf-8')

Here is the complete repo: https://github.com/brc-dd/vitepress-blog-demo

Final result (didn’t do any styling):

image

I guess this feature is less likely to be supported officially. It will be better if someone can write a Vite plugin to auto-generate that data before build.

Something like this can also be done: https://github.com/vuejs/blog/blob/master/.vitepress/posts.data.js

3reactions
jivane-perichcommented, Jun 4, 2022

Seems possible with a customMetadata (as shown here in vuejs blog : https://github.com/vuejs/blog/blob/master/.vitepress/config.js )

Read more comments on GitHub >

github_iconTop Results From Across the Web

Metadata and the Web - Getty Center
Metadata provides a means of indexing, accessing, preserving, and discovering digital resources. The volume of digital information available over electronic ...
Read more >
How Do You Build Effective Search? (Hint: Metadata and ...
Metadata is data about the collection, such as author, date, title, section, topic, era, etc. Tagging is a technical means to mark up...
Read more >
What is metadata and how does it work? - TechTarget
Metadata is data that describes other data, providing a structured reference that helps to sort and identify attributes of the information it describes....
Read more >
Metadata Standards and Resources - ALA Store |
A Companion to Metadata for Digital Collections by Steven J. Miller. ... Planet Cataloging [Compilation from various blogs on cataloging and metadata].
Read more >
The Basics of Book Metadata and Keywords - IngramSpark
Your book metadata will consist of basic things such as your title, author name, author bio, book description, publication date, etc.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found