Module:SocialMedia: Difference between revisions
Appearance
No edit summary Tag: Reverted |
No edit summary Tag: Manual revert |
||
| Line 1: | Line 1: | ||
-- Reusable social media component meant to be called from other modules | |||
local sf = {} | |||
local | |||
-- This section defines the social platforms; can be added or deleted without consequence | |||
-- param = handle for the platform; for "x", we also accept "twitter" for legacy purposes | |||
-- icon = wiki file for the icon | |||
-- prefix = URL prefix | |||
-- label = used for alt text/screen readers | |||
local socialPlatforms = { | local socialPlatforms = { | ||
{ | { | ||
param | param = "facebook", | ||
icon = "File:SocialFacebookIcon.svg", | |||
prefix | prefix = "https://www.facebook.com/", | ||
label | label = "Facebook" | ||
}, | }, | ||
{ | { | ||
param | param = "instagram", | ||
icon = "File:SocialInstagramIcon.svg", | |||
prefix | prefix = "https://www.instagram.com/", | ||
label | label = "Instagram" | ||
}, | }, | ||
{ | { | ||
param | param = "linkedin", | ||
icon = "File:SocialLinkedInIcon.svg", | |||
prefix | prefix = "https://www.linkedin.com/", | ||
label | label = "LinkedIn" | ||
}, | }, | ||
{ | { | ||
param | param = "telegram", | ||
icon = "File:SocialTelegramIcon.svg", | |||
prefix | prefix = "https://t.me/", | ||
label | label = "Telegram" | ||
}, | }, | ||
{ | { | ||
param | param = "threads", | ||
icon = "File:SocialThreadsIcon.svg", | |||
prefix | prefix = "https://www.threads.net/", | ||
label | label = "Threads" | ||
}, | }, | ||
{ | { | ||
param | param = "tiktok", | ||
icon = "File:SocialTikTokIcon.svg", | |||
prefix | prefix = "https://www.tiktok.com/", | ||
label | label = "TikTok" | ||
}, | }, | ||
{ | { | ||
param | -- Multiple aliases: the editor can supply "x" or "twitter" | ||
param = { "x", "twitter" }, | |||
prefix | icon = "File:SocialXIcon.svg", | ||
label | prefix = "https://x.com/", | ||
label = "X (Twitter)" | |||
}, | }, | ||
{ | { | ||
param | param = "youtube", | ||
icon = "File:SocialYouTubeIcon.svg", | |||
prefix | prefix = "https://www.youtube.com/", | ||
label | label = "YouTube" | ||
}, | }, | ||
} | } | ||
-- | -------------------------------------------------------------------------------- | ||
-- Helper to find the first non-empty user handle from a param or table of params | |||
-- If param is a single string (e.g. "facebook"), we do args["facebook"] | |||
-- If it's a table like { "x", "twitter" }, we check args["x"] then args["twitter"] | |||
-------------------------------------------------------------------------------- | |||
local function getUserHandle(args, param) | local function getUserHandle(args, param) | ||
if type(param) == "string" then | if type(param) == "string" then | ||
| Line 104: | Line 75: | ||
end | end | ||
end | end | ||
return nil | |||
end | end | ||
end | end | ||
-- | -- Build a clickable icon link (24px) with alt text for accessibility: | ||
function | -- [[File:Something.svg|24px|alt=ALT_TEXT|link=URL]] | ||
local | local function buildIconLink(iconFile, altText, url) | ||
return string.format("[[%s|24px|alt=%s|link=%s]]", iconFile, altText, url) | |||
end | |||
-- Build a single table row with colspan=2, containing a flex container for icons | |||
local function buildSocialRow(icons) | |||
local iconMarkup = table.concat({ | |||
'<div style="display: flex; flex-wrap: wrap; gap: 0.5em; margin-top: 0.2em;">', | |||
table.concat(icons, ""), | |||
'</div>' | |||
}, "\n") | |||
return table.concat({ | |||
"|-", | |||
'| colspan="2" | ' .. iconMarkup | |||
}, "\n") | |||
end | |||
-- Main render function, takes a table of arguments (handles), returns wiki markup with social icons | |||
function sf.render(args) | |||
local icons = {} | |||
for _, platform in ipairs(socialPlatforms) do | for _, platform in ipairs(socialPlatforms) do | ||
local handle = getUserHandle(args, platform.param) | local handle = getUserHandle(args, platform.param) | ||
if handle and handle ~= "" then | if handle and handle ~= "" then | ||
local url = platform.prefix .. handle | local url = platform.prefix .. handle | ||
local | local iconLink = buildIconLink(platform.icon, platform.label, url) | ||
table.insert(icons, iconLink) | |||
end | end | ||
end | end | ||
if | -- If no handles were provided, return nothing. | ||
if #icons == 0 then | |||
return "" | return "" | ||
end | end | ||
-- Return a row that fits nicely in the template, with a flex container for icons | |||
return buildSocialRow(icons) | |||
) | |||
end | end | ||
return | return sf | ||
Revision as of 16:40, 7 February 2025
Documentation for this module may be created at Module:SocialMedia/doc
-- Reusable social media component meant to be called from other modules
local sf = {}
-- This section defines the social platforms; can be added or deleted without consequence
-- param = handle for the platform; for "x", we also accept "twitter" for legacy purposes
-- icon = wiki file for the icon
-- prefix = URL prefix
-- label = used for alt text/screen readers
local socialPlatforms = {
{
param = "facebook",
icon = "File:SocialFacebookIcon.svg",
prefix = "https://www.facebook.com/",
label = "Facebook"
},
{
param = "instagram",
icon = "File:SocialInstagramIcon.svg",
prefix = "https://www.instagram.com/",
label = "Instagram"
},
{
param = "linkedin",
icon = "File:SocialLinkedInIcon.svg",
prefix = "https://www.linkedin.com/",
label = "LinkedIn"
},
{
param = "telegram",
icon = "File:SocialTelegramIcon.svg",
prefix = "https://t.me/",
label = "Telegram"
},
{
param = "threads",
icon = "File:SocialThreadsIcon.svg",
prefix = "https://www.threads.net/",
label = "Threads"
},
{
param = "tiktok",
icon = "File:SocialTikTokIcon.svg",
prefix = "https://www.tiktok.com/",
label = "TikTok"
},
{
-- Multiple aliases: the editor can supply "x" or "twitter"
param = { "x", "twitter" },
icon = "File:SocialXIcon.svg",
prefix = "https://x.com/",
label = "X (Twitter)"
},
{
param = "youtube",
icon = "File:SocialYouTubeIcon.svg",
prefix = "https://www.youtube.com/",
label = "YouTube"
},
}
--------------------------------------------------------------------------------
-- Helper to find the first non-empty user handle from a param or table of params
-- If param is a single string (e.g. "facebook"), we do args["facebook"]
-- If it's a table like { "x", "twitter" }, we check args["x"] then args["twitter"]
--------------------------------------------------------------------------------
local function getUserHandle(args, param)
if type(param) == "string" then
return args[param]
else
for _, alias in ipairs(param) do
local handle = args[alias]
if handle and handle ~= "" then
return handle
end
end
return nil
end
end
-- Build a clickable icon link (24px) with alt text for accessibility:
-- [[File:Something.svg|24px|alt=ALT_TEXT|link=URL]]
local function buildIconLink(iconFile, altText, url)
return string.format("[[%s|24px|alt=%s|link=%s]]", iconFile, altText, url)
end
-- Build a single table row with colspan=2, containing a flex container for icons
local function buildSocialRow(icons)
local iconMarkup = table.concat({
'<div style="display: flex; flex-wrap: wrap; gap: 0.5em; margin-top: 0.2em;">',
table.concat(icons, ""),
'</div>'
}, "\n")
return table.concat({
"|-",
'| colspan="2" | ' .. iconMarkup
}, "\n")
end
-- Main render function, takes a table of arguments (handles), returns wiki markup with social icons
function sf.render(args)
local icons = {}
for _, platform in ipairs(socialPlatforms) do
local handle = getUserHandle(args, platform.param)
if handle and handle ~= "" then
local url = platform.prefix .. handle
local iconLink = buildIconLink(platform.icon, platform.label, url)
table.insert(icons, iconLink)
end
end
-- If no handles were provided, return nothing.
if #icons == 0 then
return ""
end
-- Return a row that fits nicely in the template, with a flex container for icons
return buildSocialRow(icons)
end
return sf