123

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 class="container">
  <div class="form form-stacked">
    <div class="form-block">
      <label class="label" for="board_content">Content</label>
      <div class="form-controls">
        <span class="button ion-code" title="Code block <pre><code>" data-button-type="addCode"></span>
        <span class="button ion-code-working" title="Inline code <code>" data-button-type="addInlineCode"></span>
        <span class="button" title="Strong <strong>" data-button-type="addStrong"><strong>b</strong></span>
        <span class="button" title="Emphasis <em>" data-button-type="addEmphasis"><strong><em>i</em></strong></span>
        <span class="button ion-link" title="Link <a>" data-button-type="addLink"></span>
        <span class="button ion-social-codepen-outline" title="Embed CodePen pen" data-button-type="embedCodePen"></span>
      </div>
      <textarea placeholder="Ask a programming question or share a tip with other developers. Markdown is supported." class="textarea-tall" id="board_content"></textarea>
    </div>
  </div>
</div>

<div class="note">This was extracted from <a href="https://www.codehive.io" target="_blank">CodeHive's</a> markdown WYSIWYG toolbar. In sharing it on CodePen, we hope for others to learn how we've implemented it. Feel free to fork it and make it awesomer.</div>
            
          
!
            
              @import "bourbon";

$light-gray:  #F5F5F5;
$blue:        #338EDA;
$white:       #FFF;
$mid-gray:    #D8D8D8;
$spacing:     1.5rem;
$ui-height:   2rem;

@mixin border-radius($value){
  @include border-top-radius($value);
  @include border-bottom-radius($value);
}

*{
  -webkit-box-sizing: border-box;
  -moz-box-sizing: border-box;
  box-sizing: border-box;
}

body{
  line-height: 1.5;
}

.container{
  width: 600px;
  margin: 0 auto;
}

.form{
	font-size: 1rem;
	input[type="text"], input[type="email"], input[type="password"],
	textarea{
		background: $light-gray;
		border-color: $mid-gray;
		border-style: solid;
		@include border-radius(4px);
		border-width: 1px;

		box-shadow: none;
		display: inline-block;
		max-width: 100%;
		padding: $spacing/2;
		
		-webkit-appearance: none;
		&:focus{
			background: $white;
			border-color: $blue;
			outline: 0;
		}
	}
	textarea, .textarea{
		min-height: 150px;
	}
	input[type="submit"]:focus{
		border: none;
		outline: 0;
	}
	.textarea-tall{
		min-height: 300px;
	}
}

.form-stacked{
	input[type="text"], input[type="email"], input[type="password"],
	textarea{
		display: block;
		margin-bottom: $spacing;
		width: 100%;
	}
}

.form-block{
	position: relative;
}

.form textarea{
	@include border-top-radius(0);
}

.form-controls{
	padding: 0 $spacing/2;
	border-top: 1px solid $mid-gray;
	border-left: 1px solid $mid-gray;
	border-right: 1px solid $mid-gray;
	border-top-left-radius: 4px;
	border-top-right-radius: 4px;
	.button{
		padding: 0 $spacing/1.5;
		font-size: 1.125rem;
		&:hover{
			color: $blue;
		}
	}
}

.button{
  @include border-left-radius(4px);
  @include border-right-radius(4px);
  border: none;
  cursor: pointer;
  display: inline-block;
  min-height:   $ui-height;
  line-height:  $ui-height;

  font-size: em(14);
  font-family:      inherit;
  text-decoration:  none;

  @include appearance(none);

  padding: 0 $spacing/2;

  white-space: nowrap;
  vertical-align: middle;
}

.label{
	display: block;
  font-weight: 600;
  text-transform: uppercase;
  margin-bottom: $spacing/4;
  font-size: 0.875rem;
}

.note{
  background: $mid-gray;
  font-size: 0.825rem;
  width: 250px;
  padding: $spacing;
  @include border-left-radius(2px);
	@include border-right-radius(2px);
  @include position(fixed, null null $spacing/2 $spacing/2);
  z-index: 100;
  a{
    color: $blue;
  }
}
            
          
!
            
              buttonTypes = 
  addCode: "Enter code here"
  addInlineCode: "Enter inline code here"
  addStrong: "Strong text"
  addEmphasis: "Emphasized text"
  addLink: "https://www.codehive.io"

buttonFunctions =
  addCode: "```\n#{buttonTypes.addCode}\n```\n\n"
  addInlineCode: "`#{buttonTypes.addInlineCode}` "
  addStrong: "**#{buttonTypes.addStrong}** "
  addEmphasis: "*#{buttonTypes.addEmphasis}* "
  addLink: "[Link title](#{buttonTypes.addLink}) "

matchString = (target, textAreaElement, limit) ->
  textArea = document.getElementById(textAreaElement.attr('id'))
  highlight = textArea.value.lastIndexOf(target, limit)
  if highlight >= 0
    textArea.focus()
    textArea.selectionStart = highlight
    textArea.selectionEnd = highlight + target.length

generatePenEmbed = (link) ->
  nameBeg = ///.*codepen.io\////
  nameEnd = ///\/pen.*///
  penBeg = ///.*/pen\////
  penEnd = ///\////
  name = link.replace nameBeg, ""
  name = name.replace nameEnd, ""
  pen = link.replace penBeg, ""
  pen = pen.replace penEnd, ""
  embed = "<p data-height='350' data-theme-id='0' data-slug-hash='#{pen}' data-default-tab='result' data-user='#{name}' class='codepen'>See the <a href='https://codepen.io/#{name}/pen/#{pen}/'>Pen</a> by <a href='https://codepen.io/#{name}'>@#{name}</a> on <a href='https://codepen.io'>CodePen</a>.</p>"
  return embed
  
    
addMarkdown = (buttonType, textArea) ->
  text = textArea.val()
  caretPosition = document.getElementById(textArea.attr('id')).selectionStart
  if buttonType == "embedCodePen"
    penLink = prompt("Link to Pen")
    #TODO: Add some validation for CodePen link
    if penLink
      generatePenEmbed(penLink)
      textArea.val(text.substring(0, caretPosition) + generatePenEmbed(penLink) + text.substring(caretPosition, text.length - 1))
  if buttonType of buttonTypes
    textArea.val(text.substring(0, caretPosition) + buttonFunctions[buttonType] + text.substring(caretPosition, text.length - 1))
    matchString(buttonTypes[buttonType], textArea, caretPosition + buttonTypes[buttonType].length - 1)
    
$('.form-controls .button').on 'click', () ->
  buttonType = $(this).data('button-type')
  textArea = $(this).parent().parent().find('textarea')
  addMarkdown(buttonType, textArea)

            
          
!
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.

Console