fix(alerts): Fixed leaks

This commit is contained in:
Spencer Brower
2026-07-17 16:42:56 -04:00
parent ebb4ef691a
commit 66b5c5a934
+17 -14
View File
@@ -1,7 +1,6 @@
#+feature dynamic-literals #+feature dynamic-literals
package markdown package markdown
import "core:fmt"
import "core:strings" import "core:strings"
ALERT_EMOJIS: map[string]string = { ALERT_EMOJIS: map[string]string = {
@@ -35,7 +34,7 @@ inject_alerts :: proc(html: string) -> string {
bq := remaining[bq_start:bq_end] bq := remaining[bq_start:bq_end]
strings.write_string(&sb, remaining[:bq_start]) strings.write_string(&sb, remaining[:bq_start])
strings.write_string(&sb, transform_alert(bq)) transform_alert(&sb, bq)
remaining = remaining[bq_end:] remaining = remaining[bq_end:]
} }
@@ -43,7 +42,7 @@ inject_alerts :: proc(html: string) -> string {
return strings.to_string(sb) return strings.to_string(sb)
} }
transform_alert :: proc(bq: string) -> string { transform_alert :: proc(sb: ^strings.Builder, bq: string) {
pos := len("<blockquote>") pos := len("<blockquote>")
for pos < len(bq) && for pos < len(bq) &&
(bq[pos] == '\n' || bq[pos] == '\r' || bq[pos] == ' ' || bq[pos] == '\t') { (bq[pos] == '\n' || bq[pos] == '\r' || bq[pos] == ' ' || bq[pos] == '\t') {
@@ -56,20 +55,23 @@ transform_alert :: proc(bq: string) -> string {
bq[pos + 2] != '>' || bq[pos + 2] != '>' ||
bq[pos + 3] != '[' || bq[pos + 3] != '[' ||
bq[pos + 4] != '!' { bq[pos + 4] != '!' {
return bq strings.write_string(sb, bq)
return
} }
close := strings.index(bq[pos + 5:], "]") close := strings.index(bq[pos + 5:], "]")
if close < 0 { if close < 0 {
return bq strings.write_string(sb, bq)
return
} }
type_raw := bq[pos + 5:pos + 5 + close] type_raw := bq[pos + 5:pos + 5 + close]
type_lower := strings.to_lower(type_raw) type_lower := strings.to_lower(type_raw, context.temp_allocator)
emoji, found := ALERT_EMOJIS[type_lower] emoji, found := ALERT_EMOJIS[type_lower]
if !found { if !found {
return bq strings.write_string(sb, bq)
return
} }
after_type := pos + 5 + close + 1 after_type := pos + 5 + close + 1
@@ -84,12 +86,13 @@ transform_alert :: proc(bq: string) -> string {
rest := bq[content_start:] rest := bq[content_start:]
return fmt.aprintf( strings.write_string(sb, `<blockquote class="alert alert-`)
`<blockquote class="alert alert-%s"> strings.write_string(sb, type_lower)
<p class="alert-title">%s %s`, strings.write_string(sb, `">`)
type_lower, strings.write_string(sb, "\n")
emoji, strings.write_string(sb, `<p class="alert-title">`)
rest, strings.write_string(sb, emoji)
) strings.write_string(sb, " ")
strings.write_string(sb, rest)
} }