Skip to content

Commit 296a066

Browse files
fix: Omit private methods from doc ToC (#275)
1 parent b1ed692 commit 296a066

File tree

3 files changed

+23
-23
lines changed

3 files changed

+23
-23
lines changed
Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
11
## Private Events
2-
<details id="private-events-details">
3-
<summary>View</summary>
42

5-
${event.list}
6-
</details>
3+
${event.list}
Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
11
## Private Methods
2-
<details id="private-methods-details">
3-
<summary>View</summary>
42

5-
${method.list}
6-
</details>
3+
${method.list}

src/macrofier/engine.mjs

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -732,36 +732,42 @@ function insertTableofContents(content) {
732732
let toc = ''
733733
const count = {}
734734
const slugger = title => title.toLowerCase().replace(/ /g, '-').replace(/-+/g, '-').replace(/[^a-zA-Z-]/g, '')
735-
let collapsedContentLevel = null
735+
let inPrivateSection = false
736736

737737
content.split('\n').filter(line => line.match(/^\#/)).map(line => {
738738
const match = line.match(/^(\#+) (.*)/)
739-
if (match) {
739+
740+
if (!match) { return }
741+
742+
// level 2 == section title (Usage, Overview, Methods, etc)
743+
// level 3 == method/event names; we'll only list public methods/events in the ToC
744+
// level 4 == examples; we'll ignore these in the ToC
740745
const level = match[1].length
741-
if (level > 1 && level < 4) {
742-
if (collapsedContentLevel === level) {
743-
// we are back to the level we started the collapsed content, end the collapse
744-
toc += ' ' + ' '.repeat(collapsedContentLevel) + '</details>\n'
745-
collapsedContentLevel = null
746-
}
747746
const title = match[2]
748747
const slug = slugger(title)
748+
749749
if (count.hasOwnProperty(slug)) {
750750
count[slug] += 1
751751
}
752752
else {
753753
count[slug] = 0
754754
}
755755
const link = '#' + slug + (count[slug] ? `-${count[slug]}` : '')
756-
toc += ' ' + ' '.repeat(level - 1) + `- [${title}](${link})`
756+
757+
if (level == 2 || level == 3) {
758+
if (inPrivateSection) {
759+
// we're currently in a private section and don't want to output private methods/events in ToC
760+
if (level == 3) { return }
761+
762+
// we've dropped out of a private section, output methods as normal
763+
inPrivateSection = false
764+
}
765+
757766
if (title === 'Private Methods' || title === 'Private Events') {
758-
let anchor = title === 'Private Methods' ? 'private-methods-details' : 'private-events-details'
759-
toc += '<details ontoggle="document.getElementById(\'' + anchor + '\').open=this.open"><summary>Show</summary>\n'
760-
collapsedContentLevel = level
761-
} else {
762-
toc += '\n'
763-
}
767+
inPrivateSection = true
764768
}
769+
770+
toc += ' '.repeat(level - 2) + `- [${title}](${link})\n`
765771
}
766772
}).join('\n')
767773

0 commit comments

Comments
 (0)