1
Fork 0
mirror of https://github.com/RGBCube/nu_scripts synced 2025-08-01 06:37:46 +00:00

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 `#`
This commit is contained in:
Stefan Holderbach 2024-12-22 22:48:55 +01:00 committed by GitHub
parent ba13f5ca60
commit 89c3d6d92b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -44,10 +44,18 @@ export def pr-table [] {
| to md | to md
} }
const toc = '[[toc](#table-of-content)]' const toc = '[[toc](#table-of-contents)]'
# Generate and write the table of contents to a release notes file # Generate and write the table of contents to a release notes file
export def write-toc [file: path] { export def write-toc [file: path] {
let known_h1s = [
"# Highlights and themes of this release",
"# Changes",
"# Notes for plugin developers",
"# Hall of fame",
"# Full changelog",
]
let lines = open $file | lines | each { str trim -r } let lines = open $file | lines | each { str trim -r }
let content_start = 2 + ( let content_start = 2 + (
@ -65,11 +73,32 @@ export def write-toc [file: path] {
| insert level { | insert level {
get line | split chars | take while { $in == '#' } | length get line | split chars | take while { $in == '#' } | length
} }
| insert nocomment {
# We assume that comments only have one `#`
if ($in.level != 1) {
return true
}
let line = $in.line
# Try to use the whitelist first
if ($known_h1s | any {|| $line =~ $in}) {
return true
}
# We don't know so let's ask
let user = ([Ignore Accept] |
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!")
match $user {
"Accept" => {true}
"Ignore" => {false}
}
}
) )
let table_of_contents = ( let table_of_contents = (
$data $data
| where level in 1..=3 | where level in 1..=3 and nocomment == true
| each {|header| | each {|header|
let indent = '- ' | fill -w ($header.level * 2) -a right let indent = '- ' | fill -w ($header.level * 2) -a right
@ -82,9 +111,8 @@ export def write-toc [file: path] {
let link = ( let link = (
$text $text
| str replace -a '`' '' | str downcase
| str replace -a ' ' '-' | str kebab-case
| str replace -a -r '--+' '-'
) )
$"($indent)[_($text)_]\(#($link)-toc\)" $"($indent)[_($text)_]\(#($link)-toc\)"
@ -92,7 +120,7 @@ export def write-toc [file: path] {
) )
let content = $data | each { let content = $data | each {
if $in.level in 1..=3 and not ($in.line ends-with $toc) { if $in.level in 1..=3 and not ($in.line ends-with $toc) and $in.nocomment {
$'($in.line) ($toc)' $'($in.line) ($toc)'
} else { } else {
$in.line $in.line