Until now, comments were stored in filenames with an incrementing counter. As a result, conflicts are easy; two people commenting on a post around the same time would create the same filename. Then they'd run into conflicts when merging.
Now comment filenames include a random slug.
A secondary consideration is filtering out comments from the file-picker screen. It's only intended for top-level posts. I had a hacky way to do this. My ideal approach would be a directory per post, but Lua+LÖVE doesn't have a portable way to create directories.
Now you now have to manually create a single directory called
comments/
in the directory of articles before using pothi.love.
TQ3N2E6PWHTSCYJ2P7Z73THPSKNPS7UD2XS3NDCF6D7VRMLQ7Q4AC
local num_replies = #load_metadata(id).replies
local corename = id:gsub('%.md$', '')
return ('%s-%d.md'):format(corename, num_replies)
end
next_comment_id = function(id)
comment_path = function(id)
return full_path('comments/'..id)
end
random_string = function(n)
-- generate a random string of length n
local result = {}
local nchars = #Random_string_chars
for i=1,n do
local idx = math.random(1, nchars)
table.insert(result, Random_string_chars:sub(idx, idx))
end
return table.concat(result)
end
Random_string_chars = 'abcdefghijklmnopqrstuvwxyz0123456789'
new_comment_id = function(id)
local corename = id:gsub('%.md$', '')
-- there'll be a collision on avarage every #Random_string_chars^(n/2) = 1296 comments
local n = 4
local result = ('%s-%s.md'):format(corename, random_string(4))
if file_exists(comment_path(result)) then
-- retry
-- This will infinite loop every #Random_string_chars^n = 1.6M, but it'll start taking a long time by halfway as long.
return new_comment_id(id)
end
return result
end