Pen Settings

HTML

CSS

CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource

JavaScript

Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource

Packages

Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.

Behavior

Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.

HTML

              
                <div id="root"></div>

<script crossorigin src="https://unpkg.com/react@17/umd/react.development.js"></script>
<script crossorigin src="https://unpkg.com/react-dom@17/umd/react-dom.development.js"></script>
              
            
!

CSS

              
                @import url("https://fonts.googleapis.com/css2?family=Noto+Sans+Mono:wght@400;700&display=swap");

* {
  box-sizing: border-box;
}

body {
  margin: 1rem;
}

img {
  max-width: 95%;
  margin: 1rem;
}

textarea {
  width: 100%;
  height: 120vh;
  border: none;
  resize: none;
}

textarea:focus,
select:focus {
  outline: none;
}

.container {
  &--compact-vertical {
    height: 1.2rem;
  }

  &--large {
    display: block;
    margin-bottom: 0;
    height: 120vh;
    overflow-x: hidden;
    overflow-y: scroll;
    overflow-wrap: break-word;
  }

  &--column {
    display: flex;
    flex-direction: column;
    align-items: center;
  }

  &--no-overflow {
    overflow: hidden;
  }

  &--row {
    display: flex;
    flex-direction: row;
    flex-wrap: wrap;
  }
}

.tab {
  display: block;
  height: 1.2rem;
  width: 10vw;
  min-width: 120px;
  margin-top: 1rem;
  border-radius: 0 5px 0px 0px;
  font-weight: 700;
  position: absolute;
  top: 0;
  left: 0;
}

.readable {
  background: #f4f4f4;
  font-family: "Noto Sans Mono", monospace;
  padding: 1rem 1rem 1.5rem 1rem;
}

.wrapper {
  position: relative;
  min-width: 100%;
}

#preview {
  display: block;
  border-left: 1px solid black;
  padding-top: 0.2rem;
  position: absolute;
  top: 3.3rem;
}

#editor {
  display: block;
  position: absolute;
  border-left: 1px solid black;
  top: 3.3rem;
}

@media screen and (min-width: 1024px) {
  .wrapper {
    position: relative;
    min-width: 50%;
  }
}

              
            
!

JS

              
                import dompurify from "https://cdn.skypack.dev/dompurify@2.3.3";

// Marked configuration
marked.setOptions({
  highlight: function (code) {
    return Prism.highlight(code, Prism.languages.javascript, "javascript");
  },
  gfm: true,
  breaks: true
});

// INSERTS target="_blank" INTO HREF TAGS (required for Codepen links)
const renderer = new marked.Renderer();
renderer.link = function (href, title, text) {
  return `<a target="_blank" href="${href}">${text}</a>`;
};

const App = (props) => {
  const [rawText, setRawText] = React.useState(placeholder);
  const handleChange = (e) => {
    setRawText(e.target.value);
  };
  return (
    <main className="container container--row">
      <Editor rawText={rawText} handleChange={handleChange} />
      <Preview rawText={rawText} />
    </main>
  );
};

const Preview = (props) => {
  // Data sanitized via dom purified before returning it from component
  const dirtyData = marked(props.rawText, { renderer: renderer });
  const safeData = dompurify.sanitize(dirtyData);

  return (
    <div className="container container--large  container--no-overflow wrapper">
      <Tab>Preview</Tab>
      <div
        id="preview"
        dangerouslySetInnerHTML={{ __html: safeData }}
        className="container container--large readable"
      />
    </div>
  );
};

const Editor = (props) => {
  // useEffect added as a way to test some of the react hooks and make sure test script was loaded after all components were mounted.
  React.useEffect(() => {
    const script = document.createElement("script");
    script.src =
      "https://cdn.freecodecamp.org/testable-projects-fcc/v1/bundle.js";
    script.async = true;
    document.body.appendChild(script);
    return () => {
      document.body.removeChild(script);
    };
  }, []);

  return (
    <div className="container container--large wrapper">
      <Tab>Editor</Tab>
      <textarea
        id="editor"
        className="container container--no-overflow readable "
        type="text"
        value={props.rawText}
        onChange={props.handleChange}
      />
    </div>
  );
};

const Tab = (props) => {
  return <div className="tab readable">{props.children}</div>;
};

const placeholder = `# 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.org), 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 numbered lists too.
1. Use just 1s if you want!
1. And last but not least, let's not forget embedded images:

![freeCodeCamp Logo](https://cdn.freecodecamp.org/testable-projects-fcc/images/fcc_secondary.svg)
`;

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

              
            
!
999px

Console