0

I am buiding a simple markdown previewer in JS

I have:

  • Installed the library by running npm i marked in the app folder (I double checked the nodes_package folder and a marked folder was indeed created)

  • Added import marked from "marked" at the top of my App.js file

  • As per suggested in the Marked documentation’s “advanced” section, used the marked.parse(string) function

When I try to run the app, I get:

TypeError: marked.parse is not a function

1
  • I had some help - the key was to add the script to the index.html file, and to set the /* global marked */flag at the top of the App.js - looks like something was off when importing Commented Nov 9, 2021 at 15:36

2 Answers 2

0

you can use marked just like this:

Import marked

import marked from 'marked';

Then call the function in your component:

marked('# Markdown');

Here is the example:

// app.jsx
import marked from "marked"

const html = marked('# Marked in React\n\nRendered by **marked**.');

const App = () => {
    return <p dangerouslySetInnerHTML={{ __html: html }} />
}

Though it generate the result in html format, so we need to parse it by using dangerouslySetInnerHTML of react.

Sign up to request clarification or add additional context in comments.

Comments

0

I had some help - the key was to add the script to the index.html file, and to set the /* global marked */ flag at the top of the App.js - looks like something was off when importing

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.