fix: scan ahead panic

This commit is contained in:
trisua 2025-07-24 15:10:59 -04:00
parent 9fe5735127
commit 2cc9ed7445
3 changed files with 127 additions and 13 deletions

View file

@ -27,13 +27,29 @@ pub fn render_markdown(input: &str) -> String {
.replace("</span>:temp_style", "</style>")
}
pub(crate) fn is_numeric(value: &str) -> bool {
let mut is_numeric = false;
for char in value.chars() {
is_numeric = char.is_numeric();
}
is_numeric
}
pub(crate) fn slice(x: &str, range: core::ops::RangeFrom<usize>) -> String {
(&x.chars().collect::<Vec<char>>()[range])
.iter()
.collect::<String>()
}
fn parse_text_color_line(output: &mut String, buffer: &mut String, line: &str) {
let mut in_color_buffer = false;
let mut in_main_buffer = false;
let mut color_buffer = String::new();
let mut close_1 = false;
for char in line.chars() {
for (i, char) in line.chars().enumerate() {
if close_1 && char != '%' {
// we expected to see another percentage to close the main buffer,
// not getting that means this wasn't meant to be a color
@ -60,7 +76,7 @@ fn parse_text_color_line(output: &mut String, buffer: &mut String, line: &str) {
// by this point, we have: !
// %color_buffer%main_buffer%%
output.push_str(&format!(
"<span style=\"color: {color_buffer}\">{buffer}</span>\n"
"<span style=\"color: {color_buffer}\">{buffer}</span>"
));
color_buffer.clear();
@ -73,6 +89,13 @@ fn parse_text_color_line(output: &mut String, buffer: &mut String, line: &str) {
}
// start
// scan ahead
let ahead = slice(line, i..);
if !ahead.contains("%%") {
// no closing sequence, we're done
continue;
}
// flush buffer
output.push_str(&buffer);
buffer.clear();
@ -336,7 +359,17 @@ fn parse_image_size_line(output: &mut String, buffer: &mut String, line: &str) {
if in_size && in_size_rhs {
// end
output.push_str(&format!(
"<span style=\"width: {size_lhs}; height: {size_rhs}\" class=\"img_sizer\">![{buffer}</span>"
"<span style=\"width: {}; height: {}\" class=\"img_sizer\">![{buffer}</span>",
if is_numeric(&size_lhs) {
format!("{size_lhs}px")
} else {
size_lhs
},
if is_numeric(&size_rhs) {
format!("{size_rhs}px")
} else {
size_rhs
}
));
size_lhs = String::new();
@ -487,8 +520,7 @@ fn parse_link_line(output: &mut String, buffer: &mut String, line: &str) {
buffer.clear();
// scan for closing, otherwise quit
let haystack = &line[i..];
let haystack = slice(line, i..);
if !haystack.contains("]") {
output.push('[');
continue;