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.

Expose an API to retrieve the frontmatter and outline of any page

See original GitHub issue

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

We’d like to generate a FAQ index page that lists questions from subpages.

For instance, let’s say we have 8 topics, and each topic has 5-15 questions and answers. Each topic would be a dedicated page with the topic name as the title in its frontmatter, and the questions as H2 headings.

We’d like to generate an index page where the content looks like:

1. [Topic A](./faq/topic-a)
    - [How do I do X?](./faq/topic-a#how-do-i-do-x)
    - [What about Y?](./faq/topic-a#what-about-y)
    - …
2. [Topic B](./faq/topic-b)
    - [Is it any good?](./faq/topic-b#is-it-any-good)
    - [Can I cancel at any time?](./faq/topic-b#can-i-cancel-at-any-time)
    - …

etc.

(Shown as Markdown for brevity, but it could be a JS structure that we pass to a Vue component to render lists of links.)

Describe the solution you’d like

Ideally importing any .md file would return an object with enriched metadata such as the frontmatter, body in Markdown and HTML, and ideally outline data too.

This might not be desirable, so maybe an API provided by VitePress that takes a Markdown file path or the raw contents of a Markdown file and returns that data would be preferable? Or methods to extract specific data only (frontmatter, raw body, outline…).

Describe alternatives you’ve considered

We can hard-code the list of links, but we’ll have to manually maintain it and make sure that it is always in sync with the actual content. Any slight change to how a question is worded would make the link in the index broken.

We already do that for the main navigation, which is already a pain. We’d like to avoid extra pain. 😁

Additional context

No response

Validations

Issue Analytics

  • State:open
  • Created a year ago
  • Comments:6 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
brc-ddcommented, Oct 31, 2022

We actually have that, it isn’t exactly exposed (documented) though.

Some stuff like this can give you another page’s data:

import { __pageData as foo } from './foo.md'

console.log(foo)

For ts you’ll need to do this:

declare module '*.md' {
  import type { Component } from 'vue'
  import type { PageData } from 'vitepress'

  const component: Component
  const __pageData: PageData

  export default component
  export { __pageData }
}
1reaction
fvschcommented, Nov 24, 2022

Thanks for the answer!

It would be good to have it documented indeed, though there’s always the downside of documenting a private API and turning it into a de-facto public API. ⚖️

Read more comments on GitHub >

github_iconTop Results From Across the Web

expose a configurable outline config in frontmatter #954 - GitHub
So I think: configuration of page outline of frontmatter should be documented. The outline now only has one available type 'deep' , but...
Read more >
Toward using a Headless CMS with Hugo: Building Pages ...
How to build pages from a remote API using Hugo and Hugo alone? In this article we uncover how Hugo can fetch data...
Read more >
Use Gatsby's File System Route API to Generate Pages for ...
In the pages directory, create the {Mdx.slug}.js file. Using curly braces, you can denote a file system route API that operates on all...
Read more >
Static site generators | Documenting APIs
Static site generators build all the files for your website, pushing Markdown files into the layouts you define, running scripts to automate ...
Read more >
(Gatsby) Displaying frontmatter data in MDX component
Extending Hasan's answer: First of all, here you wrote a page query. Page query only works in pages. Make sure your component is...
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