Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

181
Visualizações
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 Respostas
Responde à pergunta

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda