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

133
Visualizações
Next.js markdown-blog page code not working
import { Blogs } from "../../components/Data"
import Image from "next/image";
import Link from "next/link";
import fs from 'fs'
import path from 'path'
import matter from 'gray-matter'

export default function index({ posts }) {
    // const Short_Blog = Blogs.map(item =>
    //     <div className="BLOGS_Projects" key={item}>
    //         <div className="BLOGS_Projects_Image">
    //             <Image
    //                 className='BLOGS_Projects_image'
    //                 src={item['img-1']}
    //                 layout='fill'
    //             // objectFit='contain'
    //             />
    //         </div>
    //         {/* if someone clicks on this link i want them to go to [project].js and send This item to [projcet].js */}
    //         <Link href={'/blogs/' + Blogs.indexOf(item)}>
    //             <a>{item['title']}</a>
    //         </Link>
    //         <p>{item['desc']}</p>
    //     </div>
    // );

    return (
        <div className="BLOGS_Container">
            <div className="BLOGS_Sub_Container">

                <div className="BLOGS_New">

                    <h1 style={{ marginLeft: 25 + 'px' }}>Cyber-Security Blogs</h1>

                    <div className="BLOGS_Present">

                        {posts.map( post =>{
                            <h1 style={{zIndex: '10000'}}>{post}</h1>
                        })}

                    </div>

                </div>

            </div>
        </div>
    )
}

export async function getStaticProps() {
    const files = fs.readdirSync(path.join('posts'))

    const posts = files.map((filename) => {
        const slug = filename.replace('.md', '')

        const markdownWithMeta = fs.readFileSync(path.join('posts', filename), 'utf-8')

        const { data: frontmatter } = matter(markdownWithMeta)

        return {
            slug,
            frontmatter
        }
    })

    return {
        props: {
            posts,
        },
    }
}

The posts object does exist but when I try to use it in the HTML, it doesn't show up anyone got any idea why this is happening? the posts.map that I used is not giving any errors but it also doesn't show any h1 html in actual page, the actual page seems blank.

enter image description here

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

You need to return the element to render inside the callback of map function.

1st way

{posts.map((post) => {
  return <h1 style={{ zIndex: "10000" }}>{post}</h1>;
})}

2nd way

{posts.map((post) => (
  <h1 style={{ zIndex: "10000" }}>{post}</h1>
))}
about 4 years ago · Juan Pablo Isaza Relatório

0

React will not render plain html content as a string. You need to use the dangerouslySetInnerHTML prop, like so:

{
  posts.map((post) => {
    return <div dangerouslySetInnerHTML={{__html: post}}></div>
  })
}

You can read more about it here:

https://reactjs.org/docs/dom-elements.html#dangerouslysetinnerhtml

Alternatively you can use something like next-mdx-remote, which you can learn about here:

https://github.com/hashicorp/next-mdx-remote

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