Markdown Previewer FCC Project - need help

Hello,
I am facing a problem while doing this project.
I literally have no idea how I am supposed to render the text in the preview as github flavoured markdown.
I searched through this forum and on google. I didn’t find anyone who was having the same problem as me. I imported the library in codepen, but I don’t know how to actually use it.

My code JSX code:

const markdownvar = `# Welcome to my React Markdown Previewer!

## This is a sub-heading...
### And here's some other cool stuff:
  
Heres some code, \`<div></div>\`, between 2 backticks.

\`\`\`
// this is multi-line code:

function anotherExample(firstLine, lastLine) {
  if (firstLine == '\`\`\`' && lastLine == '\`\`\`') {
    return multiLineCode;
  }
}
\`\`\`
  
You can also make text **bold**... whoa!
Or _italic_.
Or... wait for it... **_both!_**
And feel free to go crazy ~~crossing stuff out~~.

There's also [links](https://www.freecodecamp.com), and
> Block Quotes!

And if you want to get really crazy, even tables:

Wild Header | Crazy Header | Another Header?
------------ | ------------- | ------------- 
Your content can | be here, and it | can be here....
And here. | Okay. | I think we get it.

- And of course there are lists.
  - Some are bulleted.
     - With different indentation levels.
        - That look like this.


1. And there are numbererd lists too.
1. Use just 1s if you want! 
1. But the list goes on...
- Even if you use dashes or asterisks.
* And last but not least, let's not forget embedded images:

![React Logo w/ Text](https://goo.gl/Umyytc)
`


class App extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      markdown: markdownvar
    }
    this.handleChange = this.handleChange.bind(this)
  }
  
  handleChange(event) {
    this.setState({
      markdown: event.target.value
    });
  }
  
  render() {
    return (
      <div className="container-fluid">
        <h1 className="text-primary text-center">Markdown Previewer</h1>
        <div className="row">
          <div className="col-xs-6">
            <h4>#editor</h4>
            <div className="well" id="left-well">
              <div id="hello">
                <textarea id="editor" value={this.state.markdown} onChange={this.handleChange.bind(this)}/>
              </div>
            </div>
          </div>
          <div className="col-xs-6">
            <h4>#preview</h4>
            <div className="well" id="right-well">
              <div id="preview">{this.state.markdown}</div>
            </div>
          </div>
        </div>
      </div>
    );
  };
};

ReactDOM.render(<App />, document.getElementById("app"));

I don’t know how to actually use the library I imported, and I didn’t find any guide for it.

Thank you

1 Like

Hello there, its quite simple when using markdown, what you need to do is just below the render(), declare this const markerlib = marked(input, {breaks: true}); this calls the markdown funtion from the imported lib and allows for breaks.

in your preview pass the following within its opening tag as an attribute

 dangerouslySetInnerHTML={{__html: markerlib }}

and then remove your {this.state.markdown} essentially like this
<div className="preview rounded p-2" id="preview" dangerouslySetInnerHTML={{__html: markerlib}} />

also note on your onChange within the editor you can write it like this since you have alreaded binded itonChange={this.handleChange}

3 Likes

hey, i just realised i made a mistake in the dangerouslySetHTML, it should be markerlib not markdown

1 Like

the following ended up not working. nothing was rendered to the dom. i don’t know what was my mistake.

// !! IMPORTANT README:

// You may add additional external JS and CSS as needed to complete the project, however the current external resource MUST remain in place for the tests to work. BABEL must also be left in place. 

/***********
INSTRUCTIONS:
  - Select the project you would 
    like to complete from the dropdown 
    menu.
  - Click the "RUN TESTS" button to
    run the tests against the blank 
    pen.
  - Click the "TESTS" button to see 
    the individual test cases. 
    (should all be failing at first)
  - Start coding! As you fulfill each
    test case, you will see them go   
    from red to green.
  - As you start to build out your 
    project, when tests are failing, 
    you should get helpful errors 
    along the way!
    ************/

// PLEASE NOTE: Adding global style rules using the * selector, or by adding rules to body {..} or html {..}, or to all elements within body or html, i.e. h1 {..}, has the potential to pollute the test suite's CSS. Try adding: * { color: red }, for a quick example!

// Once you have read the above messages, you can delete all comments. 


const markdownvar = `# Welcome to my React Markdown Previewer!

## This is a sub-heading...
### And here's some other cool stuff:
  
Heres some code, \`<div></div>\`, between 2 backticks.

\`\`\`
// this is multi-line code:

function anotherExample(firstLine, lastLine) {
  if (firstLine == '\`\`\`' && lastLine == '\`\`\`') {
    return multiLineCode;
  }
}
\`\`\`
  
You can also make text **bold**... whoa!
Or _italic_.
Or... wait for it... **_both!_**
And feel free to go crazy ~~crossing stuff out~~.

There's also [links](https://www.freecodecamp.com), and
> Block Quotes!

And if you want to get really crazy, even tables:

Wild Header | Crazy Header | Another Header?
------------ | ------------- | ------------- 
Your content can | be here, and it | can be here....
And here. | Okay. | I think we get it.

- And of course there are lists.
  - Some are bulleted.
     - With different indentation levels.
        - That look like this.


1. And there are numbererd lists too.
1. Use just 1s if you want! 
1. But the list goes on...
- Even if you use dashes or asterisks.
* And last but not least, let's not forget embedded images:

![React Logo w/ Text](https://goo.gl/Umyytc)
`


class App extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      markdown: markdownvar
    }
    this.handleChange = this.handleChange.bind(this)
  }
  
  handleChange(event) {
    this.setState({
      markdown: event.target.value
    });
  }
  
  render() {
    const markerlib = marked(input, {breaks: true});
    return (
      <div className="container-fluid">
        <h1 className="text-primary text-center">Markdown Previewer</h1>
        <div className="row">
          <div className="col-xs-6">
            <h4>#editor</h4>
            <div className="well" id="left-well">
              <div id="hello">
                <textarea id="editor" value={this.state.markdown} onChange={this.handleChange}/>
              </div>
            </div>
          </div>
          <div className="col-xs-6">
            <h4>#preview</h4>
            <div className="well" id="right-well">
              <div id="preview" dangerouslySetInnerHTML={{__html: markerlib}} />
            </div>
          </div>
        </div>
      </div>
    );
  };
};

ReactDOM.render(<App />, document.getElementById("app"));

i found this method and i copy-pasted it to be honest:

// !! IMPORTANT README:

// You may add additional external JS and CSS as needed to complete the project, however the current external resource MUST remain in place for the tests to work. BABEL must also be left in place. 

/***********
INSTRUCTIONS:
  - Select the project you would 
    like to complete from the dropdown 
    menu.
  - Click the "RUN TESTS" button to
    run the tests against the blank 
    pen.
  - Click the "TESTS" button to see 
    the individual test cases. 
    (should all be failing at first)
  - Start coding! As you fulfill each
    test case, you will see them go   
    from red to green.
  - As you start to build out your 
    project, when tests are failing, 
    you should get helpful errors 
    along the way!
    ************/

// PLEASE NOTE: Adding global style rules using the * selector, or by adding rules to body {..} or html {..}, or to all elements within body or html, i.e. h1 {..}, has the potential to pollute the test suite's CSS. Try adding: * { color: red }, for a quick example!

// Once you have read the above messages, you can delete all comments. 


const markdownvar = `# Welcome to my React Markdown Previewer!

## This is a sub-heading...
### And here's some other cool stuff:
  
Heres some code, \`<div></div>\`, between 2 backticks.

\`\`\`
// this is multi-line code:

function anotherExample(firstLine, lastLine) {
  if (firstLine == '\`\`\`' && lastLine == '\`\`\`') {
    return multiLineCode;
  }
}
\`\`\`
  
You can also make text **bold**... whoa!
Or _italic_.
Or... wait for it... **_both!_**
And feel free to go crazy ~~crossing stuff out~~.

There's also [links](https://www.freecodecamp.com), and
> Block Quotes!

And if you want to get really crazy, even tables:

Wild Header | Crazy Header | Another Header?
------------ | ------------- | ------------- 
Your content can | be here, and it | can be here....
And here. | Okay. | I think we get it.

- And of course there are lists.
  - Some are bulleted.
     - With different indentation levels.
        - That look like this.


1. And there are numbererd lists too.
1. Use just 1s if you want! 
1. But the list goes on...
- Even if you use dashes or asterisks.
* And last but not least, let's not forget embedded images:

![React Logo w/ Text](https://goo.gl/Umyytc)
`


class App extends React.Component {
  constructor(props) {
    super(props);
    this.state = {
      markdown: markdownvar
    }
    this.handleChange = this.handleChange.bind(this)
  }
  
  handleChange(event) {
    this.setState({
      markdown: event.target.value
    });
  }
  
  render() {
    return (
      <div className="container-fluid">
        <h1 className="text-primary text-center">Markdown Previewer</h1>
        <div className="row">
          <div className="col-xs-6">
            <h4>#editor</h4>
            <div className="well" id="left-well">
              <div id="hello">
                <textarea id="editor" value={this.state.markdown} onChange={this.handleChange}/>
              </div>
            </div>
          </div>
          <div className="col-xs-6">
            <h4>#preview</h4>
            <div className="well" id="right-well">
              <div id="preview" dangerouslySetInnerHTML={{__html: marked(this.state.markdown)}} />
            </div>
          </div>
        </div>
      </div>
    );
  };
};

ReactDOM.render(<App />, document.getElementById("app"));