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

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

CSS

              
                .toc ul {
  list-style: none;
  padding-left:40px;
}

.toc li {
  margin-bottom:5px;
}
              
            
!

JS

              
                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

Console