Module:UtilsMarkup/Link

local p = {} local h = {}

local format = require("Module:UtilsMarkup/Format").format local utilsDPL = require("Module:UtilsDPL")

-- If backlink == false then this print an external link that looks like an internal one -- (useful for creating links that do not appear in Special:WhatLinksHere) function p.link(link, text, noBacklink) if not link then return "" end link = mw.text.trim(link) if link:find('http') then return h.externalLink(link, text) end if noBacklink then local pageExists = utilsDPL.pageExists(link) local url = tostring(mw.uri.fullUrl(link, { action = not pageExists and "edit" or nil, redlink = not pageExists and "1" or nil }))		local externalLink = h.externalLink(url, text or link) local formattedLink = format(externalLink, {			class = pageExists and "plainlinks" or "plainlinks new"		}) return formattedLink end local title = mw.title.new(link) if title.nsText == "File" or title.nsText == "Category" then link = ":" .. link if text == "" then text = title.text end end return h.internalLink(link, text) end

function h.internalLink(page, display) if not page then return "" end if not display then return ("%s"):format(page) end return ('%s'):format(page, display) end

function h.externalLink(page, display) if not page then return "" end if not display then return page end return ('[%s %s]'):format(page, display) end

p.Documentation = { link = { params = { {				name = "page", description = "The link target. Can be a page on the wiki or an external URL.", },			{				optional = true, name = "display", description = "The text to display for the link. Defaults to " },			{				optional = true, name = "noBacklink", description = "When, renders an internal link such that it does not register on the Special:WhatLinksHere of the target page. " .. "It also does not register on Special:WantedPages if the page doesn't exist." },		},		returns = "A string of wikitext that evaluates to a link. Will display a red link if the target is an internal link to a page that doesn't exist.", cases = { {				description = "Internal link", args = { "Princess Zelda" } },			{				description = "Piped link", args = { "Princess Zelda", "Zelda" }, },			{				description = "Category link", args = { "Category:Items" }, },			{				description = "Category link without prefix", args = { "Category:Items", "" } },			{				description = "Category link with display text", args = { "Category:Items in Breath of the Wild", "Items in Breath of the Wild" } },			{				description = "File link (to file description)", args = { "File:MM Deku Butler Artwork.png" }, },			{				description = "External link", args = { "https://www.mariowiki.com/Thwomp", "Thwomp" }, },			{				description = "Looks like an internal link, but it's actually an external link. This is to avoid Special:WhatLinksHere", args = { "Princess Zelda", "Zelda", true }, },			{				description = "Looks like an internal red link, but it's actually an external link. This is to avoid Special:WantedPages", args = { "Flippityfloppityfloo", "foobar", true }, },		},	}, }

return p