Autogenerate 'In this section' menus

by timmah

HTML

<h3 id="id_1-1-pegbovigrastim">1.1 Pegbovigrastim</h3>
<h4 id="referred-scheduling-proposal-1">1.1.1 Referred scheduling proposal</h4>
<p>An application was submitted by the Australian Pesticides and Veterinary Medicines Authority (APVMA) to consider whether pegbovigrastim requires scheduling.</p>
<h4 id="scheduling-application-1">1.1.2 Scheduling application</h4>
<p>This was a general application. The applicant's proposed amendments to the Poisons Standard are as follows:</p>
<p><strong>Appendix B - New Entry</strong></p>
<p>PEGBOVIGRASTIM.</p>
<p>The applicant's reasons for the request are:</p>


<h3 id="id_1-1-pegbovigrastim">2.1 Pegbovigrastim</h3>
<h4 id="referred-scheduling-proposal-1">2.1.1 Referred scheduling proposal</h4>
<p>An application was submitted by the Australian Pesticides and Veterinary Medicines Authority (APVMA) to consider whether pegbovigrastim requires scheduling.</p>
<h4 id="scheduling-application-1">2.1.2 Scheduling application</h4>
<p>This was a general application. The applicant's proposed amendments to the Poisons Standard are as follows:</p>
<p><strong>Appendix B - New Entry</strong></p>
<p>PEGBOVIGRASTIM.</p>
<p>The applicant's reasons for the request are:</p>
<h3 id="id_1-1-pegbovigrastim">1.1 Pegbovigrastim</h3>
<h4 id="referred-scheduling-proposal-1">Referred scheduling proposal</h4>
<p>An application was submitted by the Australian Pesticides and Veterinary Medicines Authority (APVMA) to consider whether pegbovigrastim requires scheduling.</p>
<h4 id="scheduling-application-1">Scheduling application</h4>
<p>This was a general application. The applicant's proposed amendments to the Poisons Standard are as follows:</p>
<p><strong>Appendix B - New Entry</strong></p>
<p>PEGBOVIGRASTIM.</p>
<p>The applicant's reasons for the request are:</p>

JavaScript

// Autogenerate 'In this section' pagemenus
// Replace the variables below with the desired target elements, normally heading leves

var sectionHeadingLevel = 'h3',
	nestedHeadingLevel = 'h4';

// For every major heading level
$(sectionHeadingLevel).each(function() {

    var $thisHeading = $(this),
		$pageMenu = '<p class="pagemenu"><strong>In this section:</strong> </p>',
        $items = [];

	// Select every minor heading until another major one is encountered
    $($thisHeading.nextUntil(sectionHeadingLevel, nestedHeadingLevel)).each(function() {

		// Collect the inner text and ID
        var $this = $(this),
            $text = $this.text(),
            $id = $this.attr('id'),
            $template = '<a href=#' + $id + '>' + $text + '</a> | ';

		// Add them to an array for later
        $items.push($template);
    });
	
	// TODO: Replace additional pipe on the end of final array item - not working
	// $items[$items.length-1].replace(/|/g, '');
	
	// Add a section menu after each Major heading
    $($thisHeading).after($pageMenu);
	
	// Insert the array of links from earlier
    $thisHeading.next('.pagemenu').append($items);
	
});