lib/tpl/md-plugin.js
function addContents($, contents) {
    console.log('addContents', contents);
    var body = document.getElementsByTagName('BODY');
    if (!body) return;

    var $body = $(body[0]),
      contentsStyle = [
        'position:fixed;right:1em;top:1em;',
        'padding:0.5em;min-width:120px;',
        'font-size:90%;line-height:18px;',
        'border:1px solid #aaa;background: #F9F9F9;'
      ].join(''),
      html = [],
      order = [],
      hash = [];

    for (var i = 0; i < contents.length; ++i) {
      order[i] = 0;
      hash[i] = '';
    }

    function indexOf(tag) {
      for (var i = 0; i < contents.length && contents[i].toLowerCase() !== tag; ++i);
      return i;
    }

    $(contents.join(',')).each(function (i, obj) {
      var index = indexOf(obj.tagName.toLowerCase());
      order[index]++;
      hash[index] = $(obj).text();
      for (var j = index + 1; j < contents.length; ++j) {
        // Clear low level order
        order[j] = 0;
        hash[j] = '';
      }
      var anchor = hash.slice(0, index + 1).join('-');
      //anchor = '__id_' + tag + Math.floor(9999999 * Math.random());

      // Add anchor
      $(obj).append(fm('<a name="{0}" style="color:#333;"></a>', anchor));
      // Add contents item
      html.push(fm('<div style="padding-left:{0}em;"><a href="#{2}" style="text-decoration:none;">{1}</a></div>',
        index * 1.5, order.slice(0, index + 1).join('.') + ' ' + hash[index], anchor));
    });

    var $contentsWrap = $(fm(['<div style="{0}">',
        '<div style="text-align: center;height:22px;line-height:22px;">',
        '<b>Contents</b> <a href="javascript:;">hide</a>',
        '</div>',
        '<div>{1}</div>',
        '</div>'].join(''), contentsStyle, html.join(''))).prependTo($body),
      $toggle = $contentsWrap.find('> :first').find('> :last'),
      $contents = $contentsWrap.find('> :last');
    console.log($contentsWrap, $toggle, $contents);

    $toggle.click(function () {
      $contents.slideToggle();
      $toggle.html($toggle.html() === 'show' ? 'hide' : 'show');
    });
  }
function addContents($, contents) {
    console.log('addContents', contents);
    var body = document.getElementsByTagName('BODY');
    if (!body) return;

    var $body = $(body[0]),
      contentsStyle = [
        'position:fixed;right:1em;top:1em;',
        'padding:0.5em;min-width:120px;',
        'font-size:90%;line-height:18px;',
        'border:1px solid #aaa;background: #F9F9F9;'
      ].join(''),
      html = [],
      order = [],
      hash = [];

    for (var i = 0; i < contents.length; ++i) {
      order[i] = 0;
      hash[i] = '';
    }

    function indexOf(tag) {
      for (var i = 0; i < contents.length && contents[i].toLowerCase() !== tag; ++i);
      return i;
    }

    $(contents.join(',')).each(function (i, obj) {
      var index = indexOf(obj.tagName.toLowerCase());
      order[index]++;
      hash[index] = $(obj).text();
      for (var j = index + 1; j < contents.length; ++j) {
        // Clear low level order
        order[j] = 0;
        hash[j] = '';
      }
      var anchor = hash.slice(0, index + 1).join('-');
      //anchor = '__id_' + tag + Math.floor(9999999 * Math.random());

      // Add anchor
      $(obj).append(fm('<a name="{0}" style="color:#333;"></a>', anchor));
      // Add contents item
      html.push(fm('<div style="padding-left:{0}em;"><a href="#{2}" style="text-decoration:none;">{1}</a></div>',
        index * 1.5, order.slice(0, index + 1).join('.') + ' ' + hash[index], anchor));
    });

    var $contentsWrap = $(fm(['<div style="{0}">',
        '<div style="text-align: center;height:22px;line-height:22px;">',
        '<b>Contents</b> <a href="javascript:;">hide</a>',
        '</div>',
        '<div>{1}</div>',
        '</div>'].join(''), contentsStyle, html.join(''))).prependTo($body),
      $toggle = $contentsWrap.find('> :first').find('> :last'),
      $contents = $contentsWrap.find('> :last');
    console.log($contentsWrap, $toggle, $contents);

    $toggle.click(function () {
      $contents.slideToggle();
      $toggle.html($toggle.html() === 'show' ? 'hide' : 'show');
    });
  }
function addContents ( $ , contents ) { console . log ( 'addContents' , contents ) ; var body = document . getElementsByTagName ( 'BODY' ) ; if ( ! body ) return ; var $body = $ ( body [ 0 ] ) , contentsStyle = [ 'position:fixed;right:1em;top:1em;' , 'padding:0.5em;min-width:120px;' , 'font-size:90%;line-height:18px;' , 'border:1px solid #aaa;background: #F9F9F9;' ] . join ( '' ) , html = [ ] , order = [ ] , hash = [ ] ; for ( var i = 0 ; i < contents . length ; ++ i ) { order [ i ] = 0 ; hash [ i ] = '' ; } function indexOf ( tag ) { for ( var i = 0 ; i < contents . length && contents [ i ] . toLowerCase ( ) !== tag ; ++ i ) ; return i ; } $ ( contents . join ( ',' ) ) . each ( function ( i , obj ) { var index = indexOf ( obj . tagName . toLowerCase ( ) ) ; order [ index ] ++ ; hash [ index ] = $ ( obj ) . text ( ) ; for ( var j = index + 1 ; j < contents . length ; ++ j ) { // Clear low level order order [ j ] = 0 ; hash [ j ] = '' ; } var anchor = hash . slice ( 0 , index + 1 ) . join ( '-' ) ; //anchor = '__id_' + tag + Math.floor(9999999 * Math.random()); // Add anchor $ ( obj ) . append ( fm ( '<a name="{0}" style="color:#333;"></a>' , anchor ) ) ; // Add contents item html . push ( fm ( '<div style="padding-left:{0}em;"><a href="#{2}" style="text-decoration:none;">{1}</a></div>' , index * 1.5 , order . slice ( 0 , index + 1 ) . join ( '.' ) + ' ' + hash [ index ] , anchor ) ) ; } ) ; var $contentsWrap = $ ( fm ( [ '<div style="{0}">' , '<div style="text-align: center;height:22px;line-height:22px;">' , '<b>Contents</b> <a href="javascript:;">hide</a>' , '</div>' , '<div>{1}</div>' , '</div>' ] . join ( '' ) , contentsStyle , html . join ( '' ) ) ) . prependTo ( $body ) , $toggle = $contentsWrap . find ( '> :first' ) . find ( '> :last' ) , $contents = $contentsWrap . find ( '> :last' ) ; console . log ( $contentsWrap , $toggle , $contents ) ; $toggle . click ( function ( ) { $contents . slideToggle ( ) ; $toggle . html ( $toggle . html ( ) === 'show' ? 'hide' : 'show' ) ; } ) ; }
Add Contents for page
Add Contents for page