// ==================================
// Generator of Table of content
//
// Description:
// creates an ordered list from headings
// container in an element, then inserts it
// somewhere else.
// ==================================
var tocBuilder = {
	_sourceElement : null,
	_destinationElement : null,
	_headingSelectors : "h2,h3,h4,h5,h6",
	_minimumSections : 2,
	_headings : [],
	_isHooked : false,
	_ticking : false,
	_scrollY : null,
	_activeHeading : null,
	_activeClassName : null,
	_tocClass : "toc-container",
	_tocListClass : "toc-list",
	_tocListTag: "ol",

	Init : function(sourceElementID, destinationElementID, tocListClass)
	{
		this._setValue(this._tocListClass, tocListClass, "string");

		this._sourceElement = document.getElementById(sourceElementID);
		this._destinationElement = document.getElementById(destinationElementID);

		if (this._sourceElement == null || this._destinationElement == null)
			return false;
		return true;
	},
	InsertTOC : function()
	{
		var tocListElement = this.BuildTOC( this._sourceElement );
		if (tocListElement !== null)
		{
			this._destinationElement.appendChild(tocListElement);
			return true;
		}
		return false;
	},
	BuildTOC : function(headingsContainerElement)
	{
		let mainHeading = headingsContainerElement.querySelector("h1");
		let subHeadings = headingsContainerElement.querySelectorAll(this._headingSelectors);
		let heading;
		let headingNbrStr;
		let headingNbr;
		let listTag = this._tocListTag;
		let indexNbr = 0;

		if(subHeadings.length >= this._minimumSections)
		{
			let rootNode = document.createElement("div");
			let titleNode = this.BuildTOCItem(mainHeading, indexNbr, true);
			let listNode = document.createElement(listTag);

			rootNode.setAttribute("class", "toc-container");
			listNode.setAttribute("class", "toc-list");

			rootNode.appendChild(titleNode);
			rootNode.appendChild(listNode);

			let currentParent = listNode;
			let currentNode = null;
			let previousNode = null;
			let previousNbr = 2;
			indexNbr = 1;

			// Create items
			for(let i = 0; i < subHeadings.length; ++i)
			{
				heading = subHeadings[i];
				headingNbrStr = heading.nodeName;
				headingNbr = Number.parseInt(headingNbrStr[1]);
				
				if (previousNbr < headingNbr)
				{
					currentParent = document.createElement(listTag);
					previousNode.appendChild(currentParent);
				}
				else
				{	
					for(let i = headingNbr; i < previousNbr; ++i) 
						currentParent = currentParent.parentNode.parentNode;
				}
				currentNode = this.BuildTOCItem(heading, indexNbr);
				currentParent.appendChild(currentNode);
				previousNode = currentNode;
				previousNbr = headingNbr;
				indexNbr += 1;
			}
			return rootNode;
		}
		return null;
	},
	BuildTOCItem : function(heading, index, isTitle = false)
	{
		var headingID = heading.id;
		var headingLabel = heading.textContent;
		var classPrefix = "toc-";
		var className = classPrefix + heading.tagName.toLowerCase();

		var itemTag = isTitle ? "div" : "li";
		var anchorTag = "a";

		var item = document.createElement(itemTag);
		var anchor = document.createElement(anchorTag);

		anchor.setAttribute("href", "#" + headingID.toLowerCase());
		anchor.textContent = headingLabel;

		item.classList.add("toc-item");
		item.classList.add("toc-item-" + index);
		item.classList.add(className);
		item.appendChild(anchor);
		return item;
	},
	// =========================================
	_setValue: function(member, parameter, type)
	{
		if (parameter !== null && typeof parameter === type)
			member = parameter;
	}
};



@@ -1223,74 +1223,8 @@
 %0A%09%7B%0A
-%09%09let mainHeading = headingsContainerElement.querySelector(%22h1%22);%0A
 %09%09le
@@ -1312,60 +1312,61 @@
 ;%0A%09%09
-let heading;%0A%09%09let headingNbrStr;%0A%09%09let headingNbr;%0A
+if(subHeadings.length %3E= this._minimumSections)%0A%09%09%7B%0A%09
 %09%09le
@@ -1397,16 +1397,17 @@
 tTag;%0A%09%09
+%09
 let inde
@@ -1423,59 +1423,129 @@
 %0A%0A%09%09
-if(subHeadings.length %3E= this._minimumSections)%0A%09%09%7B
+%09// Create%0A%09%09%09// .. root%0A%09%09%09// .. title%0A%09%09%09// .. list %0A%09%09%09let mainHeading = headingsContainerElement.querySelector(%22h1%22);
 %0A%09%09%09
@@ -1650,20 +1650,37 @@
 dexNbr, 
-true
+%22div%22, %22Introduction%22
 );%0A%09%09%09le
@@ -1898,16 +1898,73 @@
 Node);%0A%0A
+%09%09%09let heading;%0A%09%09%09let headingNbrStr;%0A%09%09%09let headingNbr;%0A
 %09%09%09let c
@@ -2835,22 +2835,33 @@
 x, i
-sTitle = false
+temTag = %22li%22, label = %22%22
 )%0A%09%7B
@@ -2911,16 +2911,38 @@
 gLabel =
+ label != %22%22 ? label :
  heading
@@ -3050,73 +3050,8 @@
 ();%0A
-%0A%09%09var itemTag = isTitle ? %22div%22 : %22li%22;%0A%09%09var anchorTag = %22a%22;%0A%0A
 %09%09va
@@ -3134,17 +3134,11 @@
 ent(
-anchorTag
+%22a%22
 );%0A%0A

Change toc title to constant "introduction"