···183183 "#" => {
184184 // If the Gemtext line starts with an "#", it is a heading, so let's
185185 // find out how deep it goes.
186186- let level = line.get(0..3).map_or(0, |root| {
187187- if root.contains("###") {
188188- 3
189189- } else if root.contains("##") {
190190- 2
191191- } else {
192192- // Converting the boolean response of `contains` to an integer
193193- usize::from(root.contains('#'))
194194- }
195195- });
186186+ let level =
187187+ line.trim_start().chars().take_while(|&c| c == '#').count();
188188+ let level = if level > 0
189189+ && line.chars().nth(level).map_or(true, char::is_whitespace)
190190+ {
191191+ level
192192+ } else {
193193+ 0
194194+ };
196195197196 nodes.push(Node::Heading {
198197 level,