Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fetch all content from markdown files #294

Closed
RiccardoPP opened this issue Aug 25, 2021 · 4 comments
Closed

Fetch all content from markdown files #294

RiccardoPP opened this issue Aug 25, 2021 · 4 comments

Comments

@RiccardoPP
Copy link

Hi,
I'm a newbie. I'm tring to build an infinite scrolling homepage for my blog with SvelteKit and MDsveX. I need to fetch all the content from the .svx/.md files, not only the metadata. I've tried some methods (for example this one or other similar with import.meta.glob but with I only fetch the metadata and not the whole html.

May you help me?

Thanks

@RiccardoPP RiccardoPP changed the title Fetch all content from markdown file (not only metadata) in homepage Fetch all content from markdown file Aug 25, 2021
@RiccardoPP RiccardoPP changed the title Fetch all content from markdown file Fetch all content from markdown files Aug 25, 2021
@michaeloliverx
Copy link

Here is a simple setup that will get you what you want

// src/routes/blog/_posts.ts
import { basename, dirname } from "path";

export type Post = {
  slug: string;
  title: string;
  excerpt: string;
  isPublished: boolean;
  publishedOn: string;
  updatedOn: string;
};

const modules = import.meta.globEager("/content/posts/**/*.svelte.md");

export const posts: Post[] = Object.entries(modules).map(([filepath, module]) => {
  const slug = basename(dirname(filepath));
  const { metadata } = module;
  const { html } = module.default.render();
  return {
    slug,
    html,
    ...metadata,
  };
});
// src/routes/blog/index.json.ts
import type { EndpointOutput } from "@sveltejs/kit";

import { posts } from "./_posts";

export async function get(): Promise<EndpointOutput> {
  return {
    body: JSON.stringify(posts),
  };
}

This sets up an endpoint /blog.json which contains a list of all your posts metadata and html.

@RiccardoPP
Copy link
Author

It works. Thanks!

@deep-research
Copy link

Hi Michael. I have a question about your example if you don't mind me asking. I am trying to understand how you got the following line to work in your page data file:

const { html } = module.default.render();

It works for me within a svelte component with a reactive declaration, but when I try to place it in my page data file, I get the following error messages on the client side:

render is not a function and
<Root> was created without expected prop 'form'

The data does appear in my server console though.

@MorganConrad
Copy link

MorganConrad commented Nov 22, 2023

I get the same intermittent error as @deep-research. Will try moving the code as suggested.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants