Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions languages/markdown/templates/sections/private-events.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
## Private Events
<details id="private-events-details">
<summary>View</summary>

${event.list}
</details>
${event.list}
5 changes: 1 addition & 4 deletions languages/markdown/templates/sections/private-methods.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
## Private Methods
<details id="private-methods-details">
<summary>View</summary>

${method.list}
</details>
${method.list}
36 changes: 21 additions & 15 deletions src/macrofier/engine.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -732,36 +732,42 @@ function insertTableofContents(content) {
let toc = ''
const count = {}
const slugger = title => title.toLowerCase().replace(/ /g, '-').replace(/-+/g, '-').replace(/[^a-zA-Z-]/g, '')
let collapsedContentLevel = null
let inPrivateSection = false

content.split('\n').filter(line => line.match(/^\#/)).map(line => {
const match = line.match(/^(\#+) (.*)/)
if (match) {

if (!match) { return }

// level 2 == section title (Usage, Overview, Methods, etc)
// level 3 == method/event names; we'll only list public methods/events in the ToC
// level 4 == examples; we'll ignore these in the ToC
const level = match[1].length
if (level > 1 && level < 4) {
if (collapsedContentLevel === level) {
// we are back to the level we started the collapsed content, end the collapse
toc += ' ' + ' '.repeat(collapsedContentLevel) + '</details>\n'
collapsedContentLevel = null
}
const title = match[2]
const slug = slugger(title)

if (count.hasOwnProperty(slug)) {
count[slug] += 1
}
else {
count[slug] = 0
}
const link = '#' + slug + (count[slug] ? `-${count[slug]}` : '')
toc += ' ' + ' '.repeat(level - 1) + `- [${title}](${link})`

if (level == 2 || level == 3) {
if (inPrivateSection) {
// we're currently in a private section and don't want to output private methods/events in ToC
if (level == 3) { return }

// we've dropped out of a private section, output methods as normal
inPrivateSection = false
}

if (title === 'Private Methods' || title === 'Private Events') {
let anchor = title === 'Private Methods' ? 'private-methods-details' : 'private-events-details'
toc += '<details ontoggle="document.getElementById(\'' + anchor + '\').open=this.open"><summary>Show</summary>\n'
collapsedContentLevel = level
} else {
toc += '\n'
}
inPrivateSection = true
}

toc += ' '.repeat(level - 2) + `- [${title}](${link})\n`
}
}).join('\n')

Expand Down