Toggle menu
Toggle preferences menu
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.

Module:MarkdownToWikitext: Difference between revisions

From Vault Hunters Official Wiki
mNo edit summary
No edit summary
Line 1: Line 1:
local p = {}
local p = {}


-- Utility: unescape common HTML entities
-- Utility: unescape common HTML entities and escaped unicode sequences
local function htmlUnescape(s)
local function htmlUnescape(s)
s = s:gsub("\\u003C", "<")
s = s:gsub("\\u003C", "<")
Line 13: Line 13:
end
end


-- Utility: strip inline HTML tags (like iframe, br, strong, etc)
-- Utility: strip inline HTML (iframes, br, etc)
local function stripHTML(s)
local function stripHTML(s)
-- Remove <iframe ...>...</iframe>
-- Strip iframe blocks
s = s:gsub("<iframe.-</iframe>", "")
s = s:gsub("<iframe.-</iframe>", "")
-- Remove all remaining HTML tags
-- Strip all HTML tags
s = s:gsub("<.->", "")
s = s:gsub("<.->", "")
return s
return s
end
end


-- Main converter
-- Main logic for Markdown → Wikitext conversion
function p.convertMarkdownToWikitext(markdown)
function p._convert(markdown)
if not markdown then return "(No content)" end
if not markdown or markdown == "" then return "(No content)" end


-- Decode escapes
-- Unescape sequences
markdown = htmlUnescape(markdown)
markdown = htmlUnescape(markdown)
markdown = markdown:gsub("\\n", "\n")    -- convert escaped newlines
markdown = markdown:gsub("\\n", "\n")    -- newline
markdown = markdown:gsub("\\%-", "–")    -- hyphen escape
markdown = markdown:gsub("\\%-", "–")    -- dash
markdown = markdown:gsub("\\!", "!")      -- unescape exclamation
markdown = markdown:gsub("\\!", "!")      -- exclamation
markdown = markdown:gsub("\\_", "_")      -- keep underscores
markdown = markdown:gsub("\\_", "_")      -- underscores
markdown = markdown:gsub("\\+", "+")      -- unescape pluses
markdown = markdown:gsub("\\+", "+")      -- plus signs
markdown = markdown:gsub("\\*", "*")      -- unescape asterisks
markdown = markdown:gsub("\\*", "*")      -- asterisks
markdown = markdown:gsub("\\\\", "\\")    -- backslashes
markdown = markdown:gsub("\\\\", "\\")    -- backslash


-- Strip inline HTML
-- Strip inline HTML
Line 47: Line 47:
markdown = markdown:gsub("\n# (.-)\n", "\n= %1 =\n")
markdown = markdown:gsub("\n# (.-)\n", "\n= %1 =\n")


-- Bold/Italic
-- Bold and Italic
markdown = markdown:gsub("%*%*%*(.-)%*%*%*", "'''''%1'''''")
markdown = markdown:gsub("%*%*%*(.-)%*%*%*", "'''''%1'''''")
markdown = markdown:gsub("%*%*(.-)%*%*", "'''%1'''")
markdown = markdown:gsub("%*%*(.-)%*%*", "'''%1'''")
markdown = markdown:gsub("%*(.-)%*", "''%1''")
markdown = markdown:gsub("%*(.-)%*", "''%1''")


-- Nested list conversion: convert '- ' with leading spaces into nested '*'s
-- Nested lists: convert space-prefixed - to * with correct level
    markdown = markdown:gsub('\n([ \t]*)%- ', function(indent)
markdown = markdown:gsub("\n([ ]*)%- ", function(indent)
local level = math.floor(#indent / 2) + 1 -- every 2 spaces = one level
local level = math.floor(#indent / 2) + 1
return '\n' .. string.rep('*', level) .. ' '
return "\n" .. string.rep("*", level) .. " "
    end)
end)


 
-- Links: [text](url) → [url text]
-- Links: [text](url)
markdown = markdown:gsub("%[(.-)%]%((.-)%)", "[%2 %1]")
markdown = markdown:gsub("%[(.-)%]%((.-)%)", "[%2 %1]")


-- Clean up excess line breaks
-- Normalize line endings
markdown = markdown:gsub("\r", "")
markdown = markdown:gsub("\r", "")
markdown = markdown:gsub("\n\n\n+", "\n\n")
markdown = markdown:gsub("\n\n\n+", "\n\n") -- Collapse extra breaks
markdown = markdown:gsub("^%s+", "") -- Trim leading space
markdown = markdown:gsub("%s+$", "") -- Trim trailing space


return markdown
return markdown
end
end


-- Pull markdown from API via ExternalData
-- Entry point: used from #invoke with raw markdown string
function p.renderFromAPI(frame)
function p.convertMarkdownToWikitext(frame)
local data = mw.ext.externalData.getData{
local markdown = frame.args[1]
url = 'https://api.vaulthunters.gg/patch-notes?limit=1',
if not markdown then return "(No input received)" end
data = { markdown = 'text' }
return p._convert(markdown)
}
local markdown = data[1] and data[1].markdown or '(No data)'
return p.convertMarkdownToWikitext(markdown)
end
end


return p
return p

Revision as of 17:05, 24 July 2025

Documentation for this module may be created at Module:MarkdownToWikitext/doc

local p = {}

-- Utility: unescape common HTML entities and escaped unicode sequences
local function htmlUnescape(s)
	s = s:gsub("\\u003C", "<")
	s = s:gsub("\\u003E", ">")
	s = s:gsub("&lt;", "<")
	s = s:gsub("&gt;", ">")
	s = s:gsub("&amp;", "&")
	s = s:gsub("&quot;", '"')
	s = s:gsub("&#39;", "'")
	return s
end

-- Utility: strip inline HTML (iframes, br, etc)
local function stripHTML(s)
	-- Strip iframe blocks
	s = s:gsub("<iframe.-</iframe>", "")
	-- Strip all HTML tags
	s = s:gsub("<.->", "")
	return s
end

-- Main logic for Markdown → Wikitext conversion
function p._convert(markdown)
	if not markdown or markdown == "" then return "(No content)" end

	-- Unescape sequences
	markdown = htmlUnescape(markdown)
	markdown = markdown:gsub("\\n", "\n")     -- newline
	markdown = markdown:gsub("\\%-", "–")     -- dash
	markdown = markdown:gsub("\\!", "!")      -- exclamation
	markdown = markdown:gsub("\\_", "_")      -- underscores
	markdown = markdown:gsub("\\+", "+")      -- plus signs
	markdown = markdown:gsub("\\*", "*")      -- asterisks
	markdown = markdown:gsub("\\\\", "\\")    -- backslash

	-- Strip inline HTML
	markdown = stripHTML(markdown)

	-- Headings
	markdown = markdown:gsub("\n###### (.-)\n", "\n====== %1 ======\n")
	markdown = markdown:gsub("\n##### (.-)\n", "\n===== %1 =====\n")
	markdown = markdown:gsub("\n#### (.-)\n", "\n==== %1 ====\n")
	markdown = markdown:gsub("\n### (.-)\n", "\n=== %1 ===\n")
	markdown = markdown:gsub("\n## (.-)\n", "\n== %1 ==\n")
	markdown = markdown:gsub("\n# (.-)\n", "\n= %1 =\n")

	-- Bold and Italic
	markdown = markdown:gsub("%*%*%*(.-)%*%*%*", "'''''%1'''''")
	markdown = markdown:gsub("%*%*(.-)%*%*", "'''%1'''")
	markdown = markdown:gsub("%*(.-)%*", "''%1''")

	-- Nested lists: convert space-prefixed - to * with correct level
	markdown = markdown:gsub("\n([ ]*)%- ", function(indent)
		local level = math.floor(#indent / 2) + 1
		return "\n" .. string.rep("*", level) .. " "
	end)

	-- Links: [text](url) → [url text]
	markdown = markdown:gsub("%[(.-)%]%((.-)%)", "[%2 %1]")

	-- Normalize line endings
	markdown = markdown:gsub("\r", "")
	markdown = markdown:gsub("\n\n\n+", "\n\n") -- Collapse extra breaks
	markdown = markdown:gsub("^%s+", "") -- Trim leading space
	markdown = markdown:gsub("%s+$", "") -- Trim trailing space

	return markdown
end

-- Entry point: used from #invoke with raw markdown string
function p.convertMarkdownToWikitext(frame)
	local markdown = frame.args[1]
	if not markdown then return "(No input received)" end
	return p._convert(markdown)
end

return p