Skip to content
Snippets Groups Projects
Commit c0c707d9 authored by 最萌小汐's avatar 最萌小汐
Browse files

cleanup markdown

parent 907bbbe0
Branches
No related tags found
No related merge requests found
---@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
if type(text) == 'table' then
self[#self+1] = {
type = 'markdown',
markdown = text,
}
else
text = tostring(text)
if #text == 0 then
return
end
self[#self+1] = {
type = 'text',
language = language,
text = text,
}
end
end
function mt:splitLine()
self[#self+1] = {
type = 'splitline',
}
end
checkSplitLine(self)
function mt:string()
local lines = {}
local language = 'md'
if language == 'md' then
if self._last == 'md' then
self[#self+1] = ''
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
self[#self+1] = text
elseif obj.type == 'markdown' then
concat(obj.markdown)
else
if #self > 0 then
self[#self+1] = ''
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
self[#self+1] = ('```%s\n%s\n```'):format(language, text)
end
self._last = language
end
function mt:string()
return table.concat(self, '\n')
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
function mt:splitLine()
self._splitLine = true
return table.concat(lines, '\n')
end
return function ()
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment