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:
parent
ba13f5ca60
commit
89c3d6d92b
1 changed files with 34 additions and 6 deletions
|
@ -44,10 +44,18 @@ export def pr-table [] {
|
|||
| 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
|
||||
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 content_start = 2 + (
|
||||
|
@ -65,11 +73,32 @@ export def write-toc [file: path] {
|
|||
| insert level {
|
||||
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 = (
|
||||
$data
|
||||
| where level in 1..=3
|
||||
| where level in 1..=3 and nocomment == true
|
||||
| each {|header|
|
||||
let indent = '- ' | fill -w ($header.level * 2) -a right
|
||||
|
||||
|
@ -82,9 +111,8 @@ export def write-toc [file: path] {
|
|||
|
||||
let link = (
|
||||
$text
|
||||
| str replace -a '`' ''
|
||||
| str replace -a ' ' '-'
|
||||
| str replace -a -r '--+' '-'
|
||||
| str downcase
|
||||
| str kebab-case
|
||||
)
|
||||
|
||||
$"($indent)[_($text)_]\(#($link)-toc\)"
|
||||
|
@ -92,7 +120,7 @@ export def write-toc [file: path] {
|
|||
)
|
||||
|
||||
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)'
|
||||
} else {
|
||||
$in.line
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue