diff options
Diffstat (limited to 'script/provider/markdown.lua')
-rw-r--r-- | script/provider/markdown.lua | 103 |
1 files changed, 71 insertions, 32 deletions
diff --git a/script/provider/markdown.lua b/script/provider/markdown.lua index 5e564a40..7b94157e 100644 --- a/script/provider/markdown.lua +++ b/script/provider/markdown.lua @@ -1,3 +1,4 @@ +---@class markdown local mt = {} mt.__index = mt mt.__name = 'markdown' @@ -8,50 +9,88 @@ function mt:__tostring() return self:string() end -local function checkSplitLine(self) - if not self._splitLine then - return - end - self._splitLine = nil - if #self == 0 then - return - end - - self[#self+1] = '\n---' -end - +---@param language string +---@param text string|markdown function mt:add(language, text) if not text then return end - text = tostring(text) - if #text == 0 then - return - end - - checkSplitLine(self) - - if language == 'md' then - if self._last == 'md' then - self[#self+1] = '' - end - self[#self+1] = text + if type(text) == 'table' then + self[#self+1] = { + type = 'markdown', + markdown = text, + } else - if #self > 0 then - self[#self+1] = '' + text = tostring(text) + if #text == 0 then + return end - self[#self+1] = ('```%s\n%s\n```'):format(language, text) + self[#self+1] = { + type = 'text', + language = language, + text = text, + } end +end - self._last = language +function mt:splitLine() + self[#self+1] = { + type = 'splitline', + } end function mt:string() - return table.concat(self, '\n') -end + local lines = {} + local language = 'md' -function mt:splitLine() - self._splitLine = true + local function concat(markdown) + for _, obj in ipairs(markdown) do + if obj.type == 'splitline' then + if language ~= 'md' then + lines[#lines+1] = '```' + language = 'md' + end + if #lines > 0 + and lines[#lines] ~= '---' then + lines[#lines+1] = '' + lines[#lines+1] = '---' + end + elseif obj.type == 'markdown' then + concat(obj.markdown) + else + if obj.language == language then + lines[#lines+1] = obj.text + else + if language ~= 'md' then + lines[#lines+1] = '```' + end + if #lines > 0 then + lines[#lines+1] = '' + end + if obj.language ~= 'md' then + lines[#lines+1] = '```' .. obj.language + end + lines[#lines+1] = obj.text + language = obj.language + end + end + end + end + + concat(self) + if language ~= 'md' then + lines[#lines+1] = '```' + end + while true do + if lines[#lines] == '---' + or lines[#lines] == '' then + lines[#lines] = nil + else + break + end + end + + return table.concat(lines, '\n') end return function () |