<% // tp.file.move(“Notes/Templates/TableOfContents.md”) %>
V1 - Working without hash
Table of Contents
<%*
let headers = await tp.file.content
.split(‘\n’) // split file into lines
.filter(t ⇒ t.match(/^[#]+\s+/gi)) // only get headers
.map(h ⇒ {
let header_level = h.split(’ ’)[0].match(/#/g).length;
// get header text without special characters like ’[’ and ’]’
let header_text = h.substring(h.indexOf(’ ’) + 1).replace(/+/g, ”);
let header_link = [[${tp.file.title}#header_text|${header_text}]]
// prepend block-quote (>), indentation and bullet-point (-)
return `>${' '.repeat(header_level - 1) + '- ' + header_link}`;
})
.join('\n')
%><% headers %>
V2 - Not working
<%*
let headers_list = await tp.file.content
.split('\n') // split file into lines
.filter(t => t.match(/^[#]+\s+/gi)) // only get headers
.map(async h => {
let header_level = h.split(' ')[0].match(/#/g).length;
// get header text without special characters like '[' and ']'
let header_text = h.substring(h.indexOf(' ') + 1).replace(/[\[\]]+/g, '');
console.log(1, header_text)
let header_link = `[[${tp.file.title}#header_text|${header_text}]]`;
console.log(2, header_text)
let header_hash = "oooo"; //await sha256Short(header_text, 7); // generate SHA-256 hash
console.log(3, header_text)
// prepend block-quote (>), indentation, bullet-point (-), and append hash
console.log(4, header_text)
return `>${' '.repeat(header_level - 1) + '- ' + header_link +`}`;
});
// Resolve all promises
headers_list = await Promise.all(headers_list);
headers_list = headers_list.join('\n');
%><% headers_list %>
V3
<%*
let headers3 = await tp.file.content
.split(‘\n’) // split file into lines
.filter(t ⇒ t.match(/^[#]+\s+/gi)) // only get headers
.map(h ⇒ {
let header_level = h.split(’ ’)[0].match(/#/g).length;
// get header text without special characters like ’[’ and ’]’
let header_text = h.substring(h.indexOf(’ ’) + 1).replace(/+/g, ”);
let header_link = [[${tp.file.title}#header_text|${header_text}]]
// prepend block-quote (>), indentation and bullet-point (-)
return `>${' '.repeat(header_level - 1) + '- ' + header_link}`;
})
.join('\n')
%><% headers3 %>
Sample File Demo
Header 1 7ad89d
Header 2 1
Header B
Header 5
Footnotes
-
Footnote ↩