add api endpoint to preview babycode

This commit is contained in:
2025-06-01 00:45:11 +03:00
parent 72709226c0
commit 8a00500387
3 changed files with 53 additions and 0 deletions

View File

@ -1,8 +1,13 @@
local app = require("lapis").Application()
local json_params = require("lapis.application").json_params
local sse = require("lib.sse")
local db = require("lapis.db")
local html_escape = require("lapis.html").escape
local babycode = require("lib.babycode")
local util = require("util")
app:get("sse_thread_updates", "/thread-updates/:thread_id", function(self)
@ -33,4 +38,20 @@ app:get("sse_thread_updates", "/thread-updates/:thread_id", function(self)
return {skip_render = true}
end)
app:post("babycode_preview", "/babycode-preview", json_params(function(self)
local user = util.get_logged_in_user(self)
if not user then
return {json = {error = "not authorized"}, status = 401}
end
if not util.rate_limit_allowed(user.id, "babycode_preview", 5) then
return {json = {error = "too many requests"}, status = 429}
end
local markup = self.params.markup
if not markup or type(markup) ~= "string" then
return {json = {error = "markup field missing or invalid type"}, status = 400}
end
local rendered = babycode.to_html(markup, html_escape)
return {json = {html = rendered}}
end))
return app