Skip to content

Commit 89c3d6d

Browse files
authored
Fix table of contents generation for release notes (#1000)
- [x] fixed typo: `table-of-content` in the backreference - [x] missing sanitization of `_`,`+`,`.`,`?` etc. - [x] handling the confusion of h1 `#` and comment `#`
1 parent ba13f5c commit 89c3d6d

File tree

1 file changed

+34
-6
lines changed

1 file changed

+34
-6
lines changed

make_release/release-note/notes.nu

Lines changed: 34 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -44,10 +44,18 @@ export def pr-table [] {
4444
| to md
4545
}
4646

47-
const toc = '[[toc](#table-of-content)]'
47+
const toc = '[[toc](#table-of-contents)]'
4848

4949
# Generate and write the table of contents to a release notes file
5050
export def write-toc [file: path] {
51+
let known_h1s = [
52+
"# Highlights and themes of this release",
53+
"# Changes",
54+
"# Notes for plugin developers",
55+
"# Hall of fame",
56+
"# Full changelog",
57+
]
58+
5159
let lines = open $file | lines | each { str trim -r }
5260

5361
let content_start = 2 + (
@@ -65,11 +73,32 @@ export def write-toc [file: path] {
6573
| insert level {
6674
get line | split chars | take while { $in == '#' } | length
6775
}
76+
| insert nocomment {
77+
# We assume that comments only have one `#`
78+
if ($in.level != 1) {
79+
return true
80+
}
81+
let line = $in.line
82+
83+
# Try to use the whitelist first
84+
if ($known_h1s | any {|| $line =~ $in}) {
85+
return true
86+
}
87+
88+
# We don't know so let's ask
89+
let user = ([Ignore Accept] |
90+
input list $"Is this a code comment or a markdown h1 heading:(char nl)(ansi blue)($line)(ansi reset)(char nl)Choose if we include it in the TOC!")
91+
match $user {
92+
"Accept" => {true}
93+
"Ignore" => {false}
94+
}
95+
96+
}
6897
)
6998

7099
let table_of_contents = (
71100
$data
72-
| where level in 1..=3
101+
| where level in 1..=3 and nocomment == true
73102
| each {|header|
74103
let indent = '- ' | fill -w ($header.level * 2) -a right
75104

@@ -82,17 +111,16 @@ export def write-toc [file: path] {
82111

83112
let link = (
84113
$text
85-
| str replace -a '`' ''
86-
| str replace -a ' ' '-'
87-
| str replace -a -r '--+' '-'
114+
| str downcase
115+
| str kebab-case
88116
)
89117

90118
$"($indent)[_($text)_]\(#($link)-toc\)"
91119
}
92120
)
93121

94122
let content = $data | each {
95-
if $in.level in 1..=3 and not ($in.line ends-with $toc) {
123+
if $in.level in 1..=3 and not ($in.line ends-with $toc) and $in.nocomment {
96124
$'($in.line) ($toc)'
97125
} else {
98126
$in.line

0 commit comments

Comments
 (0)