Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

180
Views
How to get post content in javascript from markdown file

I am building a blog site with sveltekit and markdown. I import the .md file using dynamic import but i can only acces the metadata and not the post content written in markdown. Please help

//[slug.js]
export async function get({ params }) {
    const post = await import(`../../posts/${params.slug}.md`)
    console.log(post.default)
    console.log(post.metadata)

    return {
        body: {
            postContent: post.default,
            meta: post.metadata
        }
    }
}

//[slug.svelte]
<script>
    export let postContent, meta
    const { title, date, thumbnail } = meta
</script>

<h1>{title}</h1>
<p>{date}</p>
<img src={thumbnail} alt={title} />
<p>{postContent}</p>

Using post.metadata I can get the post title, date and thumbnail but post.default returns this:

{ render: [Function: render], '$$render': [Function: $$render] }

How can I get the post content?

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

The solution is to use post.default on the endpoint [slug].js and use <svelte:component this={postContent}/> on the [slug].svelte file.

Here are the files for the solution:

//[slug].js
export async function get({ params }) {
    const post = await import(`../../posts/${params.slug}.md`)

    return {
        body: {
            postContent: post.default,
            meta: post.metadata
        }
    }
}
//[slug].svelte
<script>
    export let postContent, meta
    const { title, date, thumbnail } = meta
</script>

<h1>{title}</h1>
<p>{date}</p>
<img src={thumbnail} alt={title} />

<svelte:component this={postContent} />

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!