Sharepoint 365 - Creating a Table of Contents for the Wiki Application

Here's the (unabashedly stolen) code that I use. It keys on the header styles H2 and lower. I found it a long time ago as one of the comments under your This link.

Save as a .txt file and include it as the Content Link in a Content Editor web part on your page.

<!-- SharePoint WIKI Table of Contents. Sunnyape, August -->

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>

<script type="text/javascript">
    function hideTOC() 
   {
        document.getElementById("theTOC").style.display = 'none';
       document.getElementById("show").style.display = 'block';
        document.getElementById("hide").style.display = 'none';
    }
</script>

<script type="text/javascript">
    function showTOC() 
   {
        document.getElementById("theTOC").style.display = 'block';
       document.getElementById("show").style.display = 'none';
        document.getElementById("hide").style.display = 'block';
    }
</script>

<script type="text/javascript">//alert("1")</script>

<div id="wikiTOC">
    <table style="padding: 5px;">
        <tr>
            <td><div class="TOCheader">Table of Contents</div></td>
           <td>
                <div class="closehide" id='hide'>[<a href='#' title='Click to hide' onClick="hideTOC()">hide</a>]</div>
               <div class="closehide" id='show'>[<a href='#' title='Click to show' onClick="showTOC()">show</a>]</div>
            </td>
        </tr>
    </table>

    <div id='theTOC'>
        <script type="text/javascript">
            $(document).ready
           (
                function()
               {
                    var L2=0, L3=0, L4=0;
                   $("h2, h3, h4").each(
                        function(i)
                       {
                            theLevel=$(this).get(0).tagName;
                           if (theLevel=="H2") 
                            {
                                L2=L2+1;
                               L3=0;
                                L4=0;
                               theLevelString=""+L2;
                            }
                           else if (theLevel=="H3") 
                            {
                                L3=L3+1;
                               L4=0;
                                theLevelString=""+L2+"."+L3;
                            }
                           else 
                            {
                                L4=L4+1;
                               theLevelString=""+L2+"."+L3+"."+L4;
                            }
                        $(this).attr("id", "heading_" + i);
                       $("#theTOC").append("<a href='#heading_" + i + "' title='" + theLevel + "'>" + theLevelString + " " + $(this).text() + "</a><br />");
                        }
                    );
                }
            );

            showTOC();
        </script>
    </div>
</div>

<style>
    #wikiTOC {border: 1px black dashed; background-color: whitesmoke; float: left; padding: 10px; padding-top: 0px; margin-bottom: 10px;}
   #wikiTOC .TOCheader {font-size: 1.5em; font-weight: normal; text-align: center; padding: 5px;}
    #wikiTOC .closehide {font-size: 11px; font-weight: normal;}
   #wikiTOC a[title=H2] {font-size:15px; font-weight: normal;}
    #wikiTOC a[title=H3] {font-size:14px; font-weight: normal; margin-left: 20px;}
   #wikiTOC a[title=H4] {font-size:14px; font-weight: normal; margin-left: 40px;}
</style>
/r/sharepoint Thread