css Audio - Active file-generic CSS - Active Generic - Active HTML - Active JS - Active SVG - Active Text - Active file-generic Video - Active header Love html icon-new-collection icon-person icon-team numbered-list123 pop-out spinner split-screen star tv

Pen Settings

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

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

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

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

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.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              <div id="viewer"></div>
            
          
!
            
              .container {
  font-family: "Microsoft Yahei";
  display: flex;
  flex-wrap: wrap;
  text-align: center;
  h1 {
    width: 100%;
  }
  .editor {
    width: 45%;
    margin: 0 5rem;
  }
  input {
    width: 100%;
  }
  textarea {
    width: 100%;
    height: 30rem;
    padding: 12px 20px;
    box-sizing: border-box;
    border: 2px solid #ccc;
    border-radius: 4px;
    background-color: #f8f8f8;
    resize: vertical;
  }
  .view {
    text-align: left;
  }
  .view:before {
    content: "Select All";
    color: #666;
    border: #eee 1px solid;
    border-radius: 10px;
    padding: 0.2rem 0.5rem;
    font-size: 11px;
    cursor: pointer;
  }
  .paper {
    width: 40%;
    background: #fff;
    padding: 30px;
    position: relative;
  }

  .paper,
  .paper::before,
  .paper::after {
    /* Styles to distinguish sheets from one another */
    box-shadow: 1px 1px 1px rgba(0, 0, 0, 0.25);
    border: 1px solid #bbb;
  }

  .paper::before,
  .paper::after {
    content: "";
    position: absolute;
    height: 95%;
    width: 99%;
    background-color: #eee;
  }

  .paper::before {
    right: 15px;
    top: 0;
    transform: rotate(-1deg);
    z-index: -1;
  }

  .paper::after {
    top: 5px;
    right: -5px;
    transform: rotate(1deg);
    z-index: -2;
  }
  code {
    background-color: #eee;
  }
  blockquote {
    border-left: #9e9e9e 5px solid;
    margin-left: 0;
    padding-left: 1rem;
  }
}

            
          
!
            
              /**
Markdown Previewer
Title
MarkdownInput
MarkdownView
*/
// tutorial1.js
var Title = React.createClass({
  render: function() {
    return (
      <h1>React Markdown Previewer</h1>
    );
  }
});

var UploadInput = React.createClass({
  handleUpload: function(event) {
    this.props.onInputChange(event)
    $('.view').click()
  },
  render: function() {
    return (<p><input onChange={this.handleUpload.bind(this)} name="file" id="markdown_file" type='file'></input></p>)
  }
})

var CopyBtn = React.createClass({
  render: function() {
    return (<button class="btn" data-clipboard-text="Just because you can doesn't mean you should — clipboard.js">
              Copy to clipboard
          </button>)
  }
})

var MarkdownInput = React.createClass({
  handleChange: function(event) {
    this.props.onMarkdownChange(event.target.value);
  },
  render: function() {
    return (<p><textarea value={this.props.message} onChange={this.handleChange}></textarea></p>);
  }
});

var MarkdownView = React.createClass({
  rawMarkup: function() {
    var rawMarkup = marked(this.props.message);
    return {
      __html: rawMarkup
    };
  },
  handleClick: function(event) {
    var sel, range;
    var el = event.target; //get element id
    if (window.getSelection && document.createRange) { //Browser compatibility
      sel = window.getSelection();
      if (sel.toString() == '') { //no text selection
        window.setTimeout(function() {
          range = document.createRange(); //range object
          range.selectNodeContents(el); //sets Range
          sel.removeAllRanges(); //remove all ranges from selection
          sel.addRange(range); //add Range to a Selection.
        }, 1);
      }
    } else if (document.selection) { //older ie
      sel = document.selection.createRange();
      if (sel.text == '') { //no text selection
        range = document.body.createTextRange(); //Creates TextRange object
        range.moveToElementText(el); //sets Range
        range.select(); //make selection.
      }
    }
  },
  render: function() {
    return (
      <div className="paper">
      <div className="view" onClick = {this.handleClick} dangerouslySetInnerHTML={this.rawMarkup()}>
      </div>
        </div>
    );
  }
});

var MarkdownPreviewer = React.createClass({
  getInitialState: function() {
    return {
      message: md
    };
  },
  handleMarkdownChange: function(message) {
    this.setState({
      message: message
    });
  },
  handleUpload: function(event) {
    var that = this;
    var files = event.target.files;

    for (var i = 0, f; f = files[i]; i++) {
      var reader = new FileReader();
      reader.onload = (function(file) {
        return function(event) {
          that.setState({
            message: this.result
          });
        };
      })(f);
      //读取文件内容
      reader.readAsText(f);
    }
  },
  componentDidMount: function() {

  },
  render: function() {
    return (
      <div className="container">
        <Title />
          <div className="editor">
          <UploadInput onInputChange={this.handleUpload} />
          <MarkdownInput message={this.state.message} onMarkdownChange={this.handleMarkdownChange} />
          </div>
          <MarkdownView message={this.state.message} />
        </div>
    )
  }
});

var md = `Heading
=======

Sub-heading
-----------
 
### Another deeper heading
 
> Paragraphs are separated
by a blank line.

Leave 2 spaces at the end of a line to do a  
line break

Text attributes *italic*, **bold**, \`code\`
 ~~strikethrough~~ .

Shopping list:

  * apples
  * oranges
  * pears

Numbered list:

  1. apples
  2. oranges
  3. pears

The rain---not the reign---in
Spain.

 *[Herman Fassett](https://freecodecamp.com/hermanfassett)*`;

ReactDOM.render(
  <MarkdownPreviewer />,
  document.getElementById('viewer')
);
            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.
Loading ..................

Console