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
No edit summary
No edit summary
Line 1: Line 1:
local p = {}
local p = {}
local html = mw.html


function p._convert(markdown)
-- Helper to remove inline HTML tags (like iframes)
  if not markdown or markdown == "" then return "(No content)" end
local function stripHTML(input)
if not input then return '' end
input = input:gsub('<iframe.-</iframe>', '')
input = input:gsub('<[^>]->', '') -- Strip other tags like <b>, <img>, etc.
return input
end


  -- 1. Decode JSON-style escapes
-- Helper to trim whitespace
  markdown = markdown:gsub("\\u003C", "<")
local function trim(s)
                    :gsub("\\u003E", ">")
return (s:gsub("^%s*(.-)%s*$", "%1"))
                    :gsub("\\n", "\n")
end
                    :gsub("\\r", "")
                    :gsub("\\\"", '"')
                    :gsub("\\/", "/")
                    :gsub("\\%", "%%") -- preserve percent signs


  -- 2. Strip all inline HTML tags
-- Converts the raw JSON markdown-ish text into wikitext
  markdown = markdown:gsub("<iframe.-</iframe>", "")
local function convertToWikitext(data)
                    :gsub("<.->", "")
local output = {}


  -- 3. Normalize blank-lines around headers
-- Title
  markdown = markdown:gsub("\n#+ ", "\n%1%s") -- ensure line breaks before headings
if data.title then
table.insert(output, "== " .. data.title .. " ==")
end


  -- 4. Convert headings
-- Changes section
  for i = 6,1,-1 do
if data.changes then
    local hashes = string.rep("#", i)
local clean = stripHTML(data.changes)
    local equals = string.rep("=", i)
table.insert(output, "=== Changes ===")
    markdown = markdown:gsub("\n"..hashes.." (.-)\n", "\n"..equals.." %1 "..equals.."\n")
for line in clean:gmatch("[^\r\n]+") do
  end
table.insert(output, "* " .. trim(line))
end
end


  -- 5. Bold & Italics
-- Bug Fixes section
  markdown = markdown:gsub("%*%*%*(.-)%*%*%*", "'''''%1'''''")
if data.bug_fixes then
                    :gsub("%*%*(.-)%*%*", "'''%1'''")
local clean = stripHTML(data.bug_fixes)
                    :gsub("%*(.-)%*", "''%1''")
table.insert(output, "=== Bug Fixes ===")
for line in clean:gmatch("[^\r\n]+") do
table.insert(output, "* " .. trim(line))
end
end


  -- 6. Nested lists from spaces
return table.concat(output, "\n")
  markdown = markdown:gsub("(\n[ ]*)%- ", function(spaces)
end
    local level = math.floor(#spaces / 2) + 1
    return "\n" .. string.rep("*", level) .. " "
  end)


  -- 7. Links
-- Main entry point
  markdown = markdown:gsub("%[(.-)%]%((.-)%)", "[%2 %1]")
function p.main(frame)
local json = mw.ext.externalData.getJSON({
url = 'https://api.vaulthunters.gg/patch-notes?limit=1'
})


  -- 8. Clean up extra blank lines
if not json or not json[1] then
  markdown = markdown:gsub("\r", "")
return "Could not fetch patch notes."
                    :gsub("\n\n\n+", "\n\n")
end
                    :gsub("^%s+", ""):gsub("%s+$", "")
 
  return markdown
end


function p.convertMarkdownToWikitext(frame)
local data = json[1]
  local md = frame.args[1]
return convertToWikitext(data)
  if not md then return "(No input received)" end
  return p._convert(md)
end
end


return p
return p

Revision as of 17:10, 24 July 2025

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

local p = {}
local html = mw.html

-- Helper to remove inline HTML tags (like iframes)
local function stripHTML(input)
	if not input then return '' end
	input = input:gsub('<iframe.-</iframe>', '')
	input = input:gsub('<[^>]->', '') -- Strip other tags like <b>, <img>, etc.
	return input
end

-- Helper to trim whitespace
local function trim(s)
	return (s:gsub("^%s*(.-)%s*$", "%1"))
end

-- Converts the raw JSON markdown-ish text into wikitext
local function convertToWikitext(data)
	local output = {}

	-- Title
	if data.title then
		table.insert(output, "== " .. data.title .. " ==")
	end

	-- Changes section
	if data.changes then
		local clean = stripHTML(data.changes)
		table.insert(output, "=== Changes ===")
		for line in clean:gmatch("[^\r\n]+") do
			table.insert(output, "* " .. trim(line))
		end
	end

	-- Bug Fixes section
	if data.bug_fixes then
		local clean = stripHTML(data.bug_fixes)
		table.insert(output, "=== Bug Fixes ===")
		for line in clean:gmatch("[^\r\n]+") do
			table.insert(output, "* " .. trim(line))
		end
	end

	return table.concat(output, "\n")
end

-- Main entry point
function p.main(frame)
	local json = mw.ext.externalData.getJSON({
		url = 'https://api.vaulthunters.gg/patch-notes?limit=1'
	})

	if not json or not json[1] then
		return "Could not fetch patch notes."
	end

	local data = json[1]
	return convertToWikitext(data)
end

return p