Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions src/cortex-cli/src/agent_cmd/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@
#[cfg(test)]
mod tests {
use crate::agent_cmd::cli::{CopyArgs, ExportArgs};
use crate::agent_cmd::loader::{
load_builtin_agents, parse_frontmatter, read_file_with_encoding,
};
use crate::agent_cmd::loader::{load_builtin_agents, parse_frontmatter};
use crate::agent_cmd::types::AgentMode;
use crate::utils::file::read_file_with_encoding;

#[test]
fn test_read_file_with_utf8() {
Expand Down
25 changes: 24 additions & 1 deletion src/cortex-cli/src/scrape_cmd/html.rs
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,8 @@ fn process_node_to_markdown(
}
"li" => {
let indent = " ".repeat(list_depth.saturating_sub(1));
output.push_str(&format!("\n{indent}- "));
let marker = list_item_marker(element_ref);
output.push_str(&format!("\n{indent}{marker}"));
process_node_to_markdown(
element_ref,
output,
Expand Down Expand Up @@ -448,6 +449,28 @@ fn process_node_to_markdown(
}
}

fn list_item_marker(element_ref: scraper::ElementRef) -> String {
let Some(parent) = element_ref.parent().and_then(scraper::ElementRef::wrap) else {
return "- ".to_string();
};

if parent.value().name() != "ol" {
return "- ".to_string();
}

let start = parent
.attr("start")
.and_then(|value| value.parse::<usize>().ok())
.unwrap_or(1);
let previous_items = element_ref
.prev_siblings()
.filter_map(scraper::ElementRef::wrap)
.filter(|sibling| sibling.value().name() == "li")
.count();

format!("{}. ", start + previous_items)
}

/// Convert HTML to plain text.
pub fn html_to_text(html: &str) -> String {
let cleaned = remove_unwanted_elements(html);
Expand Down
22 changes: 22 additions & 0 deletions src/cortex-cli/src/scrape_cmd/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,28 @@ mod tests {
assert!(!md_no_images.contains("!["));
}

#[test]
fn test_html_to_markdown_ordered_lists() {
let html = r#"
<ol>
<li>Install Rust</li>
<li>Run cortex</li>
<li>Check output<ul><li>Keep nested unordered item</li></ul></li>
</ol>
<ol start="4">
<li>Continue numbering</li>
</ol>
"#;
let md = html_to_markdown(html, false, false);

assert!(md.contains("1. Install Rust"), "got: {md}");
assert!(md.contains("2. Run cortex"), "got: {md}");
assert!(md.contains("3. Check output"), "got: {md}");
assert!(md.contains(" - Keep nested unordered item"), "got: {md}");
assert!(md.contains("4. Continue numbering"), "got: {md}");
assert!(!md.contains("- Install Rust"), "got: {md}");
}

#[test]
fn test_html_to_text() {
let html = "<h1>Title</h1><p>Hello <strong>world</strong>!</p>";
Expand Down