2

So, I'm trying to display pure markdown in my NextJS Typescript page, I tried it:

import React, { useState, useEffect } from "react";
import markdown from "./assets/1.md";

const Post1 = () => {
  return <>{markdown}</>;
};

export default Post1;

But I got it:

./pages/blog/assets/1.md
Module parse failed: Unexpected character ' ' (1:1)
You may need an appropriate loader to handle this file type, currently no loaders are configured to process this file. See https://webpack.js.org/concepts#loaders
> # Apresentations
| 
| Hi, my name is Vitor Koch

I already created the markdown.d.ts file

Obs.: I don't want to convert md to jsx, just want to display it, I already did it in this gist

Vitor Koch
  • 23
  • 4

2 Answers2

1

Oh now I get it. You could try this then:

return <>{JSON.stringify(markdown, 2, null)}</>;
Sovari
  • 141
  • 5
0

My approach:

  1. Install markdown parsing library (react-markdown or other, I will use marked)

  2. Place your markdown_file.md file in /public folder

  3. Read file on the server /pages/your_page.tsx:

import React from 'react';
import { marked } from 'marked';

export default function YourPage(props: { html: string }) {
    const { html } = props;
    return (
        <div>
            <span dangerouslySetInnerHTML={{ __html: html }} />
        </div>
    );
}

const filePath = join(process.cwd(), 'public', 'markdown_file.md');

export async function getStaticProps() {
    const fileContent = fs.readFileSync(filePath, 'utf8');
    return {
        props: {
            html: marked.parse(fileContent),
        },
    };
}
Ilya Iksent
  • 1,425
  • 17
  • 15