More actions
No edit summary |
No edit summary |
||
Line 1: | Line 1: | ||
local p = {} | local p = {} | ||
function p._convert(markdown) | |||
if not markdown or markdown == "" then return "(No content)" end | |||
end | |||
-- | -- 1. Decode JSON-style escapes | ||
markdown = markdown:gsub("\\u003C", "<") | |||
:gsub("\\u003E", ">") | |||
:gsub("\\n", "\n") | |||
:gsub("\\r", "") | |||
:gsub("\\\"", '"') | |||
:gsub("\\/", "/") | |||
:gsub("\\%", "%%") -- preserve percent signs | |||
-- 2. Strip all inline HTML tags | |||
markdown = markdown:gsub("<iframe.-</iframe>", "") | |||
:gsub("<.->", "") | |||
-- 3. Normalize blank-lines around headers | |||
markdown = markdown:gsub("\n#+ ", "\n%1%s") -- ensure line breaks before headings | |||
-- 4. Convert headings | |||
for i = 6,1,-1 do | |||
local hashes = string.rep("#", i) | |||
local equals = string.rep("=", i) | |||
markdown = markdown:gsub("\n"..hashes.." (.-)\n", "\n"..equals.." %1 "..equals.."\n") | |||
end | |||
-- 5. Bold & Italics | |||
markdown = markdown:gsub("%*%*%*(.-)%*%*%*", "'''''%1'''''") | |||
:gsub("%*%*(.-)%*%*", "'''%1'''") | |||
:gsub("%*(.-)%*", "''%1''") | |||
-- 6. Nested lists from spaces | |||
markdown = markdown:gsub("(\n[ ]*)%- ", function(spaces) | |||
local level = math.floor(#spaces / 2) + 1 | |||
return "\n" .. string.rep("*", level) .. " " | |||
end) | |||
-- 7. Links | |||
markdown = markdown:gsub("%[(.-)%]%((.-)%)", "[%2 %1]") | |||
-- 8. Clean up extra blank lines | |||
markdown = markdown:gsub("\r", "") | |||
:gsub("\n\n\n+", "\n\n") | |||
:gsub("^%s+", ""):gsub("%s+$", "") | |||
return markdown | |||
end | end | ||
function p.convertMarkdownToWikitext(frame) | function p.convertMarkdownToWikitext(frame) | ||
local md = frame.args[1] | |||
if not md then return "(No input received)" end | |||
return p._convert(md) | |||
end | end | ||
return p | return p |
Revision as of 17:09, 24 July 2025
Documentation for this module may be created at Module:MarkdownToWikitext/doc
local p = {}
function p._convert(markdown)
if not markdown or markdown == "" then return "(No content)" end
-- 1. Decode JSON-style escapes
markdown = markdown:gsub("\\u003C", "<")
:gsub("\\u003E", ">")
:gsub("\\n", "\n")
:gsub("\\r", "")
:gsub("\\\"", '"')
:gsub("\\/", "/")
:gsub("\\%", "%%") -- preserve percent signs
-- 2. Strip all inline HTML tags
markdown = markdown:gsub("<iframe.-</iframe>", "")
:gsub("<.->", "")
-- 3. Normalize blank-lines around headers
markdown = markdown:gsub("\n#+ ", "\n%1%s") -- ensure line breaks before headings
-- 4. Convert headings
for i = 6,1,-1 do
local hashes = string.rep("#", i)
local equals = string.rep("=", i)
markdown = markdown:gsub("\n"..hashes.." (.-)\n", "\n"..equals.." %1 "..equals.."\n")
end
-- 5. Bold & Italics
markdown = markdown:gsub("%*%*%*(.-)%*%*%*", "'''''%1'''''")
:gsub("%*%*(.-)%*%*", "'''%1'''")
:gsub("%*(.-)%*", "''%1''")
-- 6. Nested lists from spaces
markdown = markdown:gsub("(\n[ ]*)%- ", function(spaces)
local level = math.floor(#spaces / 2) + 1
return "\n" .. string.rep("*", level) .. " "
end)
-- 7. Links
markdown = markdown:gsub("%[(.-)%]%((.-)%)", "[%2 %1]")
-- 8. Clean up extra blank lines
markdown = markdown:gsub("\r", "")
:gsub("\n\n\n+", "\n\n")
:gsub("^%s+", ""):gsub("%s+$", "")
return markdown
end
function p.convertMarkdownToWikitext(frame)
local md = frame.args[1]
if not md then return "(No input received)" end
return p._convert(md)
end
return p