...
This commit is contained in:
@@ -29,6 +29,7 @@ pub fn extract_title(page string) string {
|
|||||||
pub fn set_titles(page string, maxnr int) string {
|
pub fn set_titles(page string, maxnr int) string {
|
||||||
mut result_lines := []string{}
|
mut result_lines := []string{}
|
||||||
mut current_numbers := []int{len: 6, init: 0} // Support up to H6, initialize with 0s
|
mut current_numbers := []int{len: 6, init: 0} // Support up to H6, initialize with 0s
|
||||||
|
mut has_h1 := false
|
||||||
|
|
||||||
mut effective_maxnr := maxnr
|
mut effective_maxnr := maxnr
|
||||||
if effective_maxnr == 0 {
|
if effective_maxnr == 0 {
|
||||||
@@ -55,14 +56,31 @@ pub fn set_titles(page string, maxnr int) string {
|
|||||||
if hash_count > 0 && hash_count <= effective_maxnr {
|
if hash_count > 0 && hash_count <= effective_maxnr {
|
||||||
// This is a title within the effective_maxnr
|
// This is a title within the effective_maxnr
|
||||||
current_numbers[hash_count - 1]++ // Increment current level
|
current_numbers[hash_count - 1]++ // Increment current level
|
||||||
|
if hash_count == 1 {
|
||||||
|
has_h1 = true
|
||||||
|
}
|
||||||
// Reset lower levels
|
// Reset lower levels
|
||||||
for i := hash_count; i < current_numbers.len; i++ {
|
for i := hash_count; i < current_numbers.len; i++ {
|
||||||
current_numbers[i] = 0
|
current_numbers[i] = 0
|
||||||
}
|
}
|
||||||
|
|
||||||
mut new_prefix := ""
|
mut new_prefix := ""
|
||||||
for i := 0; i < hash_count; i++ {
|
mut actual_hash_count_for_prefix := hash_count
|
||||||
new_prefix += '${current_numbers[i]}.'
|
mut first_num_override := false
|
||||||
|
|
||||||
|
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}) ]}.'
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Extract the original title text (after hashes and spaces)
|
// Extract the original title text (after hashes and spaces)
|
||||||
|
|||||||
@@ -123,4 +123,23 @@ 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.
|
||||||
|
page6 := "
|
||||||
|
## Core Architectural Principles
|
||||||
|
Some text.
|
||||||
|
### Sub-principle 1
|
||||||
|
### Sub-principle 2
|
||||||
|
## Core Architectural Principles 2
|
||||||
|
"
|
||||||
|
expected6 := "
|
||||||
|
## 1. Core Architectural Principles
|
||||||
|
Some text.
|
||||||
|
### 1.1. Sub-principle 1
|
||||||
|
### 1.2. Sub-principle 2
|
||||||
|
## 2. Core Architectural Principles 2
|
||||||
|
"
|
||||||
|
assert doctreeclient.set_titles(page6, 3) == expected6
|
||||||
|
}
|
||||||
|
"
|
||||||
|
assert doctreeclient.set_titles(page6, 3) == expected6
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user