mirror of
https://github.com/sbrow/thor.git
synced 2026-08-26 11:23:32 -04:00
refactor: Moved markdown code to a separate packge.
This commit is contained in:
@@ -0,0 +1,44 @@
|
||||
package markdown
|
||||
|
||||
import "core:strings"
|
||||
|
||||
wrap_sections :: proc(html: string) -> string {
|
||||
H2 :: "<h2"
|
||||
|
||||
sb := strings.builder_make()
|
||||
defer strings.builder_destroy(&sb)
|
||||
|
||||
pos := 0
|
||||
search_pos := 0
|
||||
found := false
|
||||
|
||||
for {
|
||||
rel := strings.index(html[search_pos:], H2)
|
||||
if rel < 0 {
|
||||
break
|
||||
}
|
||||
idx := search_pos + rel
|
||||
found = true
|
||||
|
||||
if idx > pos {
|
||||
strings.write_string(&sb, "<section>")
|
||||
strings.write_string(&sb, html[pos:idx])
|
||||
strings.write_string(&sb, "</section>")
|
||||
}
|
||||
|
||||
pos = idx
|
||||
search_pos = idx + len(H2)
|
||||
}
|
||||
|
||||
if pos < len(html) {
|
||||
strings.write_string(&sb, "<section>")
|
||||
strings.write_string(&sb, html[pos:])
|
||||
strings.write_string(&sb, "</section>")
|
||||
found = true
|
||||
}
|
||||
|
||||
if !found {
|
||||
return html
|
||||
}
|
||||
return strings.to_string(sb)
|
||||
}
|
||||
Reference in New Issue
Block a user