Is there any syntax that when wrapped around content, automatically builds a table of contents towards the top of the wiki, similarly to how the Media Wiki works? I am using SharePoint 2013.
3 Answers
Recently I updated my Add navigation table to wiki page solution to support multiple levels and SharePoint 2013.
The new solution is a configurable jquery plugin and is documented in the blog post Revised: Table of Contents for Wiki Pages
There is no OOTB capability in SP 2013 that will let you have a table of contents from a selected portion of content, however there are some nifty workarounds available mentioned below you can try it out.
I've got a simpler solution that requires no deployment at all: just add a UserCustomAction with a ScriptLink by running the following in the browser developer tools console:
var sp = SP.ClientContext.get_current();
var uca = sp.get_web().get_userCustomActions().add();
uca.set_name('AddWikiPageToC');
uca.set_location('ScriptLink');
uca.set_scriptBlock('{ \
function addWikiToC() {\
var contentContainer = $(\'#contentBox div.edit-mode-border\'), \
tocContainer = $(\'.ewiki-slink\');\
\
tocContainer.append(\'<div id="pageTOC" style="margin-top:1.1em; width:250px;"><strong>Table of Contents</strong><div id="pageTOC-UL"></div></div>\');\
\
contentContainer.find(\':header\')\
.each(function () {\
$(\'#pageTOC-UL\').append(\'<div style="padding-left:\'+this.tagName.replace(/h/ig,\'\')+\'em"><a onclick="javascript:$(\\\'#s4-workspace\\\').animate({scrollTop: \'+($(this).offset().top-80)+\'}, 1000)">\'+$(this).text()+\'</a></div>\');\
});\
}\
_spBodyOnLoadFunctionNames.push(\'addWikiToC\')\
}');
uca.update();
sp.executeQueryAsync();
In this rudimentary form it only works on all enterprise wiki pages with the social rating box and builds a sequence of indented div
elements below it, but it's very easy improve by changing selectors for contentContainer
and styles for entries below pageTOC-UL
.