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.

            
              <section class="article-info"></section>
<div class="article-body">
<h1 id="title">Title</h1>
<h1 id="second-title">Second Titles</h1>
<h2>Subtitle</h2>
<h2>Subtitle2</h2>
<h3>Sub Sub</h3>
  <p>hfewhfewfhew</p>
<h1>Third Title</h1>
<h2>The sub</h2>
</div>
            
          
!
            
              .toc ul {
  list-style: none;
  padding-left:40px;
}

.toc li {
  margin-bottom:5px;
}
            
          
!
            
              var $headers = $('.article-body h1');

if ($headers.length > 1) {
  var $toc = $('<div class="toc">');
  var $firstUl = $('<ul>');
  var $currentUl = $firstUl;
  var previous_level = 1;

  $firstUl.appendTo($toc);
  $toc.prependTo('section.article-info');

  // start with first H1
  insertHeading($headers[0]);
}

function insertHeading(heading) {
  var $heading = $(heading);
  // what level heading are we on?
  var current_level = headingLevel(heading);

  // if it's an H1, add it to the original list
  if (current_level === 1) {
    newLi($heading, $firstUl);
    $currentUl = $firstUl;
  }

  // if it's the same as the one before it, add it to the current list
  else if (current_level === previous_level) {
    newLi($heading, $currentUl);
  }

  // if it's one level higher than the one before it... time to make a new nested list
  else if (current_level > previous_level) {
    nestUl();
    newLi($heading, $currentUl);
  }

  previous_level = current_level;

  var $nextHeading = $heading.nextAll("h1, h2, h3, h4, h5, h6").first()[0];
  if ($nextHeading) insertHeading($nextHeading);
}

function nestUl() {
  var $newUl = $('<ul>');
  $newUl.appendTo($currentUl);
  $currentUl = $newUl;
}

function headingLevel(heading) {
  switch (heading.nodeName) {
    case 'H1':
      return 1;
      break;
    case 'H2':
      return 2;
      break;
    case 'H3':
      return 3;
      break;
    case 'H4':
      return 4;
      break;
    case 'H5':
      return 1;
      break;
    case 'H6':
      return 6;
      break;
    default:
      return 0;
  }
}

function newLi(heading, $list) {
  var $heading = $(heading);
  var $wrapper = $('<li></li>');
  var $link = $('<a>').prop('href', '#' + $heading.prop('id'));
  $link.html('<span class="index"></span> ' + $heading.text());

  $link.appendTo($wrapper);
  $wrapper.appendTo($list);

  var place_in_parent = $list.children('li').length;

  if ($list.parent()[0].nodeName === 'DIV') {
    $link.find('.index').text(place_in_parent)
  } else {
    $link.find('.index').text($wrapper.parent().prev('li').find('.index').text() + '.' + place_in_parent)
  }
  $heading.text($link.find('.index').text() + ' ' + $heading.text())
}
            
          
!
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