🦠 The Definitive Gemini Protocol Toolkit
gemini gemini-protocol gemtext parser zero-dependency toolkit ast converter html markdown cli networking
0
fork

Configure Feed

Select the types of activity you want to include in your feed.

fix(ast): Improve UTF-8 handling

Fuwn 28581ff4 b4515608

+48 -5
+8 -4
src/ast/container.rs
··· 224 224 225 225 nodes.push(Node::Heading { 226 226 level, 227 - // Here, we are `get`ing the `&str` starting at the `level`-th 228 - // index, then trimming the start. These operations 229 - // effectively off the line identifier. 230 - text: line.get(level..).unwrap_or("").trim_start().to_string(), 227 + // Here, the text after the heading markers is safely extracted. 228 + // `chars().skip()` is used to safely handle UTF-8 boundaries. 229 + text: line 230 + .chars() 231 + .skip(level) 232 + .collect::<String>() 233 + .trim_start() 234 + .to_string(), 231 235 }); 232 236 233 237 break;
+40 -1
tests/ast.rs
··· 95 95 assert_eq!(to, ""); 96 96 assert_eq!(text, &None); 97 97 } else { 98 - panic!("Expected Link node"); 98 + panic!("Expected link node"); 99 + } 100 + } 101 + 102 + #[test] 103 + fn build_heading_with_unicode_and_edge_cases() { 104 + // Unicode characters 105 + let ast = Ast::from_string("# Hello, 世界!"); 106 + 107 + assert_eq!(ast.inner().len(), 1); 108 + 109 + if let Node::Heading { level, text } = ast.inner().first().unwrap() { 110 + assert_eq!(level, &1); 111 + assert_eq!(text, "Hello, 世界!"); 112 + } else { 113 + panic!("Expected heading node"); 114 + } 115 + 116 + // Only hashes 117 + let ast = Ast::from_string("###"); 118 + 119 + assert_eq!(ast.inner().len(), 1); 120 + 121 + if let Node::Heading { level, text } = ast.inner().first().unwrap() { 122 + assert_eq!(level, &3); 123 + assert_eq!(text, ""); 124 + } else { 125 + panic!("Expected heading node"); 126 + } 127 + 128 + // Many hashes 129 + let ast = Ast::from_string("########## Very Deep Heading"); 130 + 131 + assert_eq!(ast.inner().len(), 1); 132 + 133 + if let Node::Heading { level, text } = ast.inner().first().unwrap() { 134 + assert_eq!(level, &10); 135 + assert_eq!(text, "Very Deep Heading"); 136 + } else { 137 + panic!("Expected heading node"); 99 138 } 100 139 } 101 140 }