Table of Contents
The Problem with Building a Table of Contents in TDX
Need to build a table of contents for a KB Article?
It can be quite cumbersome to add anchors and hyperlinks between the table of contents and your headers.
This Script performs the hard work for you.
The script takes a list of headings, like this:
the problem with building a table of contents in tdx
the script
How to Run the Script to Generate a Table of Contents
And generates HTML code that can be pasted in an Article Source to create a Table of Contents and section Headings like this:
Article Source (HTML Code) Example
<div><strong>Table of Contents<a id="TOC" name="TOC"></a></strong></div>
<ul>
<li><a href="#theproblemwithbuilding2">The Problem with Building a Table of Contents in TDX</a></li>
<li><a href="#thescript3">The Script</a></li>
<li><a href="#HowtoRunthe4">How to Run the Script to Generate a Table of Contents</a></li>
</ul>
<p> </p><p> </p><p> </p>
<h2>The Problem with Building a Table of Contents in TDX<a id="theproblemwithbuilding2" name="theproblemwithbuilding2"></a></p>
<p> </p><p> </p><p> </p>
<p><a href="#TOC">Return to the Article Table of Contents above</a></h2>
<p> </p><h2>The Script<a id="thescript3" name="thescript3"></a></p>
<p> </p><p> </p><p> </p>
<p><a href="#TOC">Return to the Article Table of Contents above</a></h2>
<p> </p><h2>How to Run the Script to Generate a Table of Contents<a id="HowtoRunthe4" name="HowtoRunthe4"></a></p>
<p> </p><p> </p><p> </p>
<p><a href="#TOC">Return to the Article Table of Contents above</a></h2>
<p> </p>
Return to the Article Table of Contents above
The Script
Note: These scripts were written by SVC Employees. Be wary of running scripts found on the Internet Not written/reviewed by IT. If in doubt, Do Not Run Internet Scripts: have someone in IT who is familiar with the script language check it before you run it.
- Attached Files:
- Attached files typically show in the Top Right of the KB article.
- Smaller browser windows will move these to the very bottom of the article.
- You can either View the file to copy it's contents directly from your web browser, or click the file name to download and open the file itself to copy it's contents.
Simplest Method for One Offs:
Use it frequently? Create a file on your desktop:
Save as a Batch File
- Download and open, or View, the Save As Batch File.txt file (Attached to this KB article)
- Copy it's contents:
- Control + A on Keyboard to select all
- Control + C on Keyboard to Copy
- Close the file or Browser Tab
- Open Notepad
- Paste the code copied in Step 1.1 in to the Notepad window.
- Control + V on Keyboard to Paste.
- Navigate to File > Save As from the top menu
- Change the Save type: to All Files
- Name the file "Convert to HTML Table of Contents.bat" or something similar with the .bat or .cmd file extension.
- Save the file to your desktop.
Creating the Encoded Command
$encodedcommand = [Convert]::ToBase64String([Text.Encoding]::Unicode.GetBytes( {
# Copy the code from Copy and Paste.txt (attached to this KB article) and Paste it's contents below
# Copy the code from Copy and Paste.txt (attached to this KB article) and Paste it's contents above
})) # End encoded command
$EncodedCommand | Set-Clipboard # Copy to your clipboard. Paste between the pair of double quotation marks after -encodedcommand in the Save as Batch File.txt
Or Save it to your PowerShell Profile
Save as a Function
You can save functions within your Windows PowerShell Profile. They will be available within all Windows PowerShell consoles.
- Open the Save to PS Profile.txt file (Attached to this KB article)
- Copy it's contents:
- Control + A on Keyboard to select all
- Control + C on Keyboard to Copy
- Close the file
- Open Windows PowerShell:
- Right click the Start button, select Windows PowerShell from the context menu
- Type the following command into the PowerShell window:
Notepad.exe $Profile
- Click Yes if asked to create the file.
- Paste the code copied from step 1.1 above into the Notepad window.
- Save and close Notepad.
- Close and reopen PowerShell
- You now have a custom PowerShell profile that performs the following:
- The ConvertTo-TitleCase and ConvertTo-HTMLTableOfContents functions built in to subsequently opened PowerShell consoles.
- Custom Prompt to show:
- PowerShell Host: Hard Coded to PS5:
- Which is what would start when opening powershell as we did in step 2.1.
- If you are running a different version of PowerShell, change the code in the Prompt function accordingly.
- What User opened the PowerShell Console: USER:thor
- Curent Working Directory: c:\Users\thor in Prompt and Window Title
- Time Stamp indicating when the previous command completed: Mon 03:34:NN PM
- Time taken to run previous command: Green 0.nn sec]
Return to the Article Table of Contents above
How to Run the Script to Generate a Table of Contents
- Run the script via your preferred method
- See above at The Script ​​​​
- Copy and Paste Method: Paste Code directly into PowerShell
- Batch File Method: Double Click the batch file on your desktop
- Function Method: Open PowerShell and type ConvertTo-HTMLTableOfContents
- You can tab twice after HTML to complete the command
- Confirm if the copied text is what you want to process.
- If not:
- Copy your list to your computer clipboard
- Type No in the PowerShell prompt and press enter on your keyboard to recheck your clipboard.
- Once confirmed that you have the correct list copied, type Yes in the PowerShell prompt and press enter.
- The HTML is automatically copied to your clipboard.
- Click the Source button to toggle Source View ON in the TDX KB article
- Paste the HTML Code into the Code editor.
- Click the Source button again to toggle Source View Off and return to the article body.
- Indent any Table of Contents as required.
Pro Tip: You can use this to Add to a Table of Contents
- Run the script with only the header(s) you want to add copied to your clipboard.
- Paste the HTML code in like normal, adding it at the very top of the KB article source.
- Back in the article Body (source toggled off), Remove the just added second "Table of Contents" header.
- Move the just added Table of Contents Bullet and Header to your desired locations.
Uber-Pro Tip: As you become more familiar with the KB Source view, you can just copy those HTML lines you want out of the PowerShell window and paste them directly into the Source view exactly where you want them.
Pro Tip: Sub-Table of Contents within Section Headings
Sometimes you have complicated articles that require a sub table of contents. i.e., a separate Table of Contents under another heading.
- Run the script like normal, including all headings within your list that you want to include in the sub table of contents.
- Paste into the article like normal.
- Indent the sub headings of the Table of Contents to indicate that they are a sub headings under a primary heading.
- Copy the indented sub headings out of the table of contents and paste them under the primary heading for which they are a sub table of contents
- Copy the primary heading out of the Table of Contents and paste it under the "Return to Table of Contents Above" "footer" at the end of one of the Sub Headings.
- Edit the hyperlink to show a display text of "Return to [Primary Heading Here]".
- Copy and paste this under each sub heading, right below or above the "Return to the Article Table of Contents above link "footer".
Return to the Article Table of Contents above