This commit is contained in:
2025-07-19 21:09:16 +02:00
parent 802dcdb8ac
commit 391283159d
2 changed files with 27 additions and 25 deletions

View File

@@ -53,34 +53,34 @@ pub fn set_titles(page string, maxnr int) string {
} }
} }
if hash_count > 0 && hash_count <= effective_maxnr { if hash_count > 0 {
// This is a title within the effective_maxnr // This is a title within the effective_maxnr
current_numbers[hash_count - 1]++ // Increment current level mut display_hash_count := hash_count
mut numbering_hash_count := hash_count
if hash_count == 1 { if hash_count == 1 {
has_h1 = true has_h1 = true
} else if !has_h1 {
display_hash_count = hash_count - 1
numbering_hash_count = hash_count - 1
} }
if numbering_hash_count > effective_maxnr {
result_lines << line
continue
}
current_numbers[numbering_hash_count - 1]++ // Increment current level based on numbering hash count
// Reset lower levels // Reset lower levels
for i := hash_count; i < current_numbers.len; i++ { for i := numbering_hash_count; i < current_numbers.len; i++ {
current_numbers[i] = 0 current_numbers[i] = 0
} }
mut new_prefix := "" mut new_prefix := ""
mut actual_hash_count_for_prefix := hash_count for i := 0; i < numbering_hash_count; i++ {
mut first_num_override := false if i > 0 && current_numbers[i] == 0 && current_numbers[i-1] > 0{
current_numbers[i] = 1
if !has_h1 && hash_count > 1 {
// If no H1 has been encountered yet, and this is an H2 or lower,
// the first number should be 1, and the prefix length is hash_count - 1.
first_num_override = true
actual_hash_count_for_prefix = hash_count - 1
}
for i := 0; i < actual_hash_count_for_prefix; i++ {
if i == 0 && first_num_override {
new_prefix += '1.'
} else {
new_prefix += '${current_numbers[i + (if first_num_override {1} else {0}) ]}.'
} }
new_prefix += '${current_numbers[i]}.'
} }
// Extract the original title text (after hashes and spaces) // Extract the original title text (after hashes and spaces)
@@ -104,13 +104,15 @@ pub fn set_titles(page string, maxnr int) string {
// Construct the new line // Construct the new line
mut new_line := "" mut new_line := ""
for _ in 0..hash_count { for _ in 0..display_hash_count {
new_line += '#' new_line += '#'
} }
new_line += " ${new_prefix} ${original_title_text}" new_line += " ${new_prefix} ${original_title_text}"
result_lines << new_line result_lines << new_line
} else { }else {
result_lines << line // Not a title or outside maxnr, keep as is result_lines << line
} }
} }

View File

@@ -123,13 +123,15 @@ Text.
## 1.1. Old Section ## 1.1. Old Section
" "
assert doctreeclient.set_titles(page5, 3) == expected5 assert doctreeclient.set_titles(page5, 3) == expected5
// Test case 6: First heading is H2, should start with 1.
// Test case 6: First heading is H2, should be treated as H1
page6 := " page6 := "
## Core Architectural Principles ## Core Architectural Principles
Some text. Some text.
### Sub-principle 1 ### Sub-principle 1
### Sub-principle 2 ### Sub-principle 2
## Core Architectural Principles 2 ## Core Architectural Principles 2
#### Sub-principle 44
" "
expected6 := " expected6 := "
## 1. Core Architectural Principles ## 1. Core Architectural Principles
@@ -137,9 +139,7 @@ Some text.
### 1.1. Sub-principle 1 ### 1.1. Sub-principle 1
### 1.2. Sub-principle 2 ### 1.2. Sub-principle 2
## 2. Core Architectural Principles 2 ## 2. Core Architectural Principles 2
" #### 2.1.1. Sub-principle 44
assert doctreeclient.set_titles(page6, 3) == expected6
}
" "
assert doctreeclient.set_titles(page6, 3) == expected6 assert doctreeclient.set_titles(page6, 3) == expected6
} }