Skip to content

How would you get a list of posts including metadata? #220

Answered by alexiglesias93
didier asked this question in Q&A
Discussion options

You must be logged in to vote

SvelteKit approach to fetch the posts data:

const glob_import = import.meta.glob<{ default: SvelteComponent; metadata: Record<string, any> }>(
	'./writing/*.md',
	{
		eager: true
	}
);
const writings = Object.entries(glob_import);
console.log({ writings });

This will return the posts data like this:

type Path = string; // The file path
type Module = {
  default: SvelteComponent; // The Svelte Component
  metadata: Record<string, any> // An object containing the frontmatter
}
type Writings = [Path, Module][];
// Writings
[
    [
        "./writing/test.md",
        {
            default: SvelteComponent,
            metadata: {
                layout: "writing",
                title: "Pos…

Replies: 3 comments 3 replies

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
2 replies
@winston0410
Comment options

@bwklein
Comment options

Comment options

You must be logged in to vote
1 reply
@didier
Comment options

Answer selected by didier
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
6 participants
Converted from issue

This discussion was converted from issue #158 on March 27, 2021 22:34.