This commit is contained in:
2025-07-19 21:48:11 +02:00
parent 6092143ed8
commit 721352489d
4 changed files with 109 additions and 23 deletions

View File

@@ -1,8 +1,7 @@
module markdownparser
module tools
import freeflowuniverse.herolib.ui.console
pub fn markdown_min_header(text string, minlevel int) string {
pub fn min_header(text string, minlevel int) string {
mut nrhash := 100
mut out := []string{}
@@ -24,12 +23,13 @@ pub fn markdown_min_header(text string, minlevel int) string {
for _ in 0 .. (minlevel - nrhash) {
addhashstring += '#'
}
for mut line2 in text.split('\n') {
if line2.starts_with('#') {
line2 = addhashstring + '${line2}'
}
out << line2
}
for mut line2 in text.split('\n') {
if line2.starts_with('#') && minlevel > nrhash {
line2 = addhashstring + '${line2}'
}
out << line2
}
return out.join('\n')

View File

@@ -0,0 +1,86 @@
module tools
import os
import freeflowuniverse.herolib.ui.console
fn test_markdown_min_header_basic() {
txt := "
# Header 1
## Header 2
### Header 3
"
expected := "
## Header 1
### Header 2
#### Header 3
"
result := min_header(txt, 2)
assert result == expected
}
fn test_markdown_min_header_no_change() {
txt := "
## Header 2
### Header 3
"
expected := "
## Header 2
### Header 3
"
result := min_header(txt, 2)
assert result == expected
}
fn test_markdown_min_header_multiple_levels() {
txt := "
# Title
Some txt here.
## Subtitle
More txt.
### Sub-subtitle
"
expected := "
### Title
Some txt here.
#### Subtitle
More txt.
##### Sub-subtitle
"
result := min_header(txt, 3)
assert result == expected
}
fn test_markdown_min_header_no_headers() {
txt := "
This is some plain txt.
No headers here.
"
expected := "
This is some plain txt.
No headers here.
"
result := min_header(txt, 1)
assert result == expected
}
fn test_markdown_min_header_empty_input() {
txt := ""
expected := ""
result := min_header(txt, 1)
assert result == expected
}
fn test_markdown_min_header_with_txt_before_header() {
txt := "
Some intro txt
# Header 1
## Header 2
"
expected := "
Some intro txt
## Header 1
### Header 2
"
result := min_header(txt, 2)
assert result == expected
}

View File

@@ -1,4 +1,4 @@
module doctreeclient
module tools
// extract_title extracts the highest level markdown title from a page string.
// It returns the first one found, or an empty string if none are present.

View File

@@ -1,10 +1,10 @@
module doctreeclient
module tools
fn test_extract_title() {
// Test case 1: Single H1 title
page1 := "# My Awesome Document"
assert doctreeclient.extract_title(page1) == "My Awesome Document"
assert extract_title(page1) == "My Awesome Document"
// Test case 2: Multiple titles, H1 first
page2 := "
@@ -14,26 +14,26 @@ Some text here.
More text.
### Sub-subtitle 1.1
"
assert doctreeclient.extract_title(page2) == "Main Title"
assert extract_title(page2) == "Main Title"
// Test case 3: No titles
page3 := "
This is a plain document.
No markdown titles here.
"
assert doctreeclient.extract_title(page3) == ""
assert extract_title(page3) == ""
// Test case 4: Title with leading/trailing spaces
page4 := " # Another Title "
assert doctreeclient.extract_title(page4) == "Another Title"
assert extract_title(page4) == "Another Title"
// Test case 5: Title with only hashes and spaces
page5 := "### "
assert doctreeclient.extract_title(page5) == ""
assert extract_title(page5) == ""
// Test case 6: Title with content immediately after hashes
page6 := "##TitleWithoutSpace"
assert doctreeclient.extract_title(page6) == "TitleWithoutSpace"
assert extract_title(page6) == "TitleWithoutSpace"
}
fn test_set_titles() {
@@ -60,7 +60,7 @@ Even more content.
### 1.2.1. Sub-subsection B.1
#### Sub-subsection B.1.1 (should not be numbered)
"
assert doctreeclient.set_titles(page1, 3) == expected1
assert set_titles(page1, 3) == expected1
// Test case 2: maxnr = 2
page2 := "
@@ -73,7 +73,7 @@ Even more content.
## 1.1. Second Level
### Third Level (should not be numbered)
"
assert doctreeclient.set_titles(page2, 2) == expected2
assert set_titles(page2, 2) == expected2
// Test case 3: No titles
page3 := "
@@ -84,7 +84,7 @@ No titles to renumber.
Plain text document.
No titles to renumber.
"
assert doctreeclient.set_titles(page3, 3) == expected3
assert set_titles(page3, 3) == expected3
// Test case 4: Mixed content and reset of numbering
page4 := "
@@ -111,7 +111,7 @@ Text.
## 2.1. Section 2.1
Text.
"
assert doctreeclient.set_titles(page4, 3) == expected4
assert set_titles(page4, 3) == expected4
// Test case 5: Titles with existing numbers (should be overwritten)
page5 := "
@@ -122,7 +122,7 @@ Text.
# 1. Old Chapter
## 1.1. Old Section
"
assert doctreeclient.set_titles(page5, 3) == expected5
assert set_titles(page5, 3) == expected5
// Test case 6: First heading is H2, should be treated as H1
page6 := "
@@ -141,7 +141,7 @@ Some text.
# 2. Core Architectural Principles 2
### 2.1.1. Sub-principle 44
"
assert doctreeclient.set_titles(page6, 3) == expected6
assert set_titles(page6, 3) == expected6
// Test case 7: maxnr = 0, no numbering but still shift headings
page7 := "
@@ -160,5 +160,5 @@ Some text.
# Core Architectural Principles 2
### Sub-principle 44
"
assert doctreeclient.set_titles(page7, 0) == expected7
assert set_titles(page7, 0) == expected7
}