Jump to content

Module:LinkParser: Difference between revisions

minor rev // via Wikitext Extension for VSCode
// via Wikitext Extension for VSCode
Line 3: Line 3:


local p = {}
local p = {}
-- Cache for previously processed URLs (persists during a single page render)
local stripCache = {}


----------------------------------
----------------------------------
Line 8: Line 11:
----------------------------------
----------------------------------
function p.strip(link)
function p.strip(link)
-- Remove the protocol part: http://, https://, ftp://, etc.
-- Input validation
link = link:gsub("^%a+://", "")
if not link or link == "" then
-- Remove leading "www."
return link
link = link:gsub("^www%.", "")
end
-- Remove trailing "/"
link = link:gsub("/$", "")
-- Check cache first for previously processed URLs
return link
if stripCache[link] then
return stripCache[link]
end
-- Combine all operations into a single chain to reduce string allocations
local result = link:gsub("^%a+://", ""):gsub("^www%.", ""):gsub("/$", "")
-- Cache the result before returning
stripCache[link] = result
return result
end
end


Line 22: Line 34:
----------------------------------
----------------------------------
function p.render(frame)
function p.render(frame)
local args = frame:getParent().args
-- Input validation
if not frame or not frame.getParent then
return ""
end
local parent = frame:getParent()
if not parent or not parent.args then
return ""
end
local args = parent.args
local rawLink = args["link"] or ""
local rawLink = args["link"] or ""
if rawLink == "" then
-- Early return for empty links
if not rawLink or rawLink == "" then
return ""
return ""
end
end

Revision as of 21:32, 7 April 2025

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

-- Module:LinkParser
-- Parses links inserted by editors in templates, strips them of the protocol part, leading "www.", and any trailing "/", while otherwise printing exactly what was entered to minimize spoofing.

local p = {}

-- Cache for previously processed URLs (persists during a single page render)
local stripCache = {}

----------------------------------
-- Remove protocol, leading www., and trailing /
----------------------------------
function p.strip(link)
	-- Input validation
	if not link or link == "" then
		return link
	end
	
	-- Check cache first for previously processed URLs
	if stripCache[link] then
		return stripCache[link]
	end
	
	-- Combine all operations into a single chain to reduce string allocations
	local result = link:gsub("^%a+://", ""):gsub("^www%.", ""):gsub("/$", "")
	
	-- Cache the result before returning
	stripCache[link] = result
	return result
end

----------------------------------
-- Return a MediaWiki link with:
--   [ originalURL  strippedDisplayText ]
----------------------------------
function p.render(frame)
	-- Input validation
	if not frame or not frame.getParent then
		return ""
	end
	
	local parent = frame:getParent()
	if not parent or not parent.args then
		return ""
	end
	
	local args = parent.args
	local rawLink = args["link"] or ""
	
	-- Early return for empty links
	if not rawLink or rawLink == "" then
		return ""
	end

	-- Strip protocol, leading 'www.', and trailing '/'
	local displayText = p.strip(rawLink)

	-- Construct an external link of the form:
	--   [ rawLink displayText ]
	return string.format("[%s %s]", rawLink, displayText)
end

return p