Creating Automatic Templates -> Title and TOC

I’m trying to create a template that will do two things when creating a new note:

First: Add the title of the note based on the the file name when it’s created using:


<% tp.file.title %>.


Second: I’m trying to add a Table of Contents based on headings within the note itself, using the following:


<%*
let headers = await tp.file.content
.split(‘\n’) // Split file into lines
.filter(t => t.match(/^#+\s+/)) // Only get headers
.map(h => {
let header_level = h.split(’ ‘)[0].match(/#/g).length; // Get header level
let header_text = h.substring(h.indexOf(’ ‘) + 1).trim(); // Get header text
let header_link = [[${tp.file.title}#${header_text}|${header_text}]]; // Create link
return ${' '.repeat(header_level - 1)}- ${header_link}; // Format link
})
.join(’\n’); // Join all links
%>

Table of Contents

<% headers %>


All of the above is in the actual template note, but none of it actually works. I’m new to this whole concept of templater and the automation it can do for you, so perhaps I don’t fully understand how to incorporate this into a template note.

I saw both the automated title and the TOC on a YouTube video, but no real indication of the code behind it.

Any assistance would be greatly appreciated.

thanks!!

This has been resolved as I found the answers I needed: Table of Contents (Headers) · SilentVoid13/Templater · Discussion #888 · GitHub