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 is required to process package imports. If you need a different preprocessor remove all packages first.

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

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

              
                <h1>Basic Markdown Editor</h1>
<p class="lede">Lightweight markdown editor with a live preview underneath. Next steps is to include a simple method for inserting placeholders for images that allow drag and drop upload.</p>
<p>Also includes some basic features for keyboard shortcuts, try with cmd+i (ctrl+i) and cmd+b (ctrl+b) either on a text selection or while in the editor.</p>
<p><b>Experimental</b> - I've had a play with a very basic implementation of undo and redo - access via (ctrl+z) and (ctrl+r) even on a mac
<h2>Editor</h2>
<textarea id="content">
Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo.
  
Pellentesque habitant morbi tristique senectus et netus et malesuada fames ac turpis egestas. Vestibulum tortor quam, feugiat vitae, ultricies eget, tempor sit amet, ante. Donec eu libero sit amet quam egestas semper. Aenean ultricies mi vitae est. Mauris placerat eleifend leo.  
</textarea>
<h2>Preview</h2>  
<div id="preview"></div>
              
            
!

CSS

              
                body {
  padding: 1em;
  font-size: 0.875em;
  line-height: 1.5;
  color: #5b5b5b;
}

.lede {
  font-size: 1rem;
}

#content {
  width: 100%;
  height: 40vh;
  padding: 1.5em;
  margin: 1em 0;
  border: none;
  background-color: #f8f8f8;
  box-sizing: border-box;
  resize: none;
  
  &:focus {
    outline: none;
    background-color: #f3f3f3;
  }
}

#preview {
 width: 100%;
  height: 40vh;
}

h1 { 
  padding-bottom: 0.2em;
  margin-bottom: 0.6em;
  border-bottom: 1px solid #f0f0f0;
}

h2 {
  margin-bottom: 0.2em; 
}
              
            
!

JS

              
                marked.setOptions({
  renderer: new marked.Renderer(),
   gfm: true,
  tables: true,
  breaks: false,
  pedantic: false,
  sanitize: true,
  smartLists: true,
  smartypants: false
})
let undoStack = [];
let redoStack = [];
let undoTimeout;
let editor = document.querySelector('#content');
let preview = document.querySelector('#preview');

preview.innerHTML = marked(editor.value);

function updateStack(text, immediate) {
  if (undoTimeout) {
    clearTimeout(undoTimeout);
  }
  
  undoTimeout = setTimeout(() => {
    undoStack.push(text);

    if (undoStack.length > 50) {
      undoStack.shift();
    }
  }, (immediate ? 0 : 200));
}

function undo() {
  if (undoStack.length > 1) {
    redoStack.push(undoStack.pop()); 
    editor.value = undoStack.pop();
    preview.innerHTML = marked(editor.value);
  }
}

function redo() {
  if (redoStack.length) {
    updateStack(editor.value, true); 
    editor.value = redoStack.pop();
    preview.innerHTML = marked(editor.value);
  }
}

editor.addEventListener('input', (e) => {
  updateStack(editor.value);
  preview.innerHTML = marked(editor.value); 
  redoStack = [];
});

editor.addEventListener('keypress', (e) => {
  if (e.metaKey || e.ctrlKey) {
    let text = editor.value;
    let start = editor.selectionStart;
    let end = editor.selectionEnd;
    let selection = text.slice(start, end);
    let replacement = selection;
    let keyCode = e.key || e.keyCode || e.charCode || e.char;  
    
    switch (keyCode) {
      case 105:
        replacement = `*${selection}*`;
        break;
      case 98:
        replacement = `**${selection}**`;
        break;
        
      case 26:
        undo();
        return;
      
      case 18:
        redo();
        return;
    }
    
    let diff = (replacement.length - selection.length);
    if (diff === 0) {
    } else {
      updateStack(text);
      redoStack = [];
    }
    
    text = text.slice(0, start) + replacement + text.slice(end);
    editor.value = text;
    preview.innerHTML = marked(editor.value);
    if (selection.length) {
      editor.setSelectionRange(start, end+diff);
    } else {
      let sel = start + ( diff / 2 );
      editor.setSelectionRange(sel, sel);
    }
  }
})
              
            
!
999px

Console