add img to babycode

This commit is contained in:
2025-05-27 17:19:06 +03:00
parent c79cc5797a
commit 8cd4695794
4 changed files with 25 additions and 0 deletions

View File

@ -96,6 +96,13 @@ function babycode.to_html(s, escape_html)
return "<ol>" .. get_list_items(list_body, escape_html) .. "</ol>"
end)
-- images
local images = {}
text = text:gsub("%[img=(.-)%](.-)%[/img%]", function (img, alt)
table.insert(images, {img = img, alt = alt})
return "\1IMG:"..#images.."\1"
end)
-- normalize newlines, attempt #4
text = text:gsub(" +%s*\r?\n", "<br>")
text = text:gsub("(%S)(\r?\n\r?\n)\r?\n*", "%1<br><br>")
@ -131,6 +138,11 @@ function babycode.to_html(s, escape_html)
-- rule
text = text:gsub("\n+%-%-%-", "<hr>")
-- <div class=\"post-img-container\"><img src=%1 alt=%2></div>
text = text:gsub("\1IMG:(%d+)\1", function (n)
local img = images[tonumber(n)]
return ("<div class=\"block-img-container\"><img class=\"block-img\" src=\"%s\" alt=\"%s\"></div>"):format(img.img, img.alt)
end)
-- replace code block placeholders back with their original contents
text = text:gsub("\1CODE:(%d+)\1", function(n)
local code = code_blocks[tonumber(n)]