Module:SocialMedia: Difference between revisions
Appearance
No edit summary |
No edit summary |
||
| Line 11: | Line 11: | ||
{ | { | ||
param = "facebook", | param = "facebook", | ||
icon = "File:SocialFacebookIcon. | icon = "File:SocialFacebookIcon.svg", | ||
prefix = "https://www.facebook.com/", | prefix = "https://www.facebook.com/", | ||
label = "Facebook" | label = "Facebook" | ||
| Line 17: | Line 17: | ||
{ | { | ||
param = "instagram", | param = "instagram", | ||
icon = "File:SocialInstagramIcon. | icon = "File:SocialInstagramIcon.svg", | ||
prefix = "https://www.instagram.com/", | prefix = "https://www.instagram.com/", | ||
label = "Instagram" | label = "Instagram" | ||
| Line 23: | Line 23: | ||
{ | { | ||
param = "linkedin", | param = "linkedin", | ||
icon = "File:SocialLinkedInIcon. | icon = "File:SocialLinkedInIcon.svg", | ||
prefix = "https://www.linkedin.com/", | prefix = "https://www.linkedin.com/", | ||
label = "LinkedIn" | label = "LinkedIn" | ||
| Line 29: | Line 29: | ||
{ | { | ||
param = "telegram", | param = "telegram", | ||
icon = "File:SocialTelegramIcon. | icon = "File:SocialTelegramIcon.svg", | ||
prefix = "https://t.me/", | prefix = "https://t.me/", | ||
label = "Telegram" | label = "Telegram" | ||
| Line 35: | Line 35: | ||
{ | { | ||
param = "threads", | param = "threads", | ||
icon = "File:SocialThreadsIcon. | icon = "File:SocialThreadsIcon.svg", | ||
prefix = "https://www.threads.net/", | prefix = "https://www.threads.net/", | ||
label = "Threads" | label = "Threads" | ||
| Line 41: | Line 41: | ||
{ | { | ||
param = "tiktok", | param = "tiktok", | ||
icon = "File:SocialTikTokIcon. | icon = "File:SocialTikTokIcon.svg", | ||
prefix = "https://www.tiktok.com/", | prefix = "https://www.tiktok.com/", | ||
label = "TikTok" | label = "TikTok" | ||
| Line 47: | Line 47: | ||
{ | { | ||
param = "whatsapp", | param = "whatsapp", | ||
icon = "File:SocialWhatsAppIcon. | icon = "File:SocialWhatsAppIcon.svg", | ||
prefix = "https://wa.me/", | prefix = "https://wa.me/", | ||
label = "WhatsApp" | label = "WhatsApp" | ||
| Line 54: | Line 54: | ||
-- Multiple aliases: the editor can supply "x" or "twitter" | -- Multiple aliases: the editor can supply "x" or "twitter" | ||
param = { "x", "twitter" }, | param = { "x", "twitter" }, | ||
icon = "File:SocialXIcon. | icon = "File:SocialXIcon.svg", | ||
prefix = "https://x.com/", | prefix = "https://x.com/", | ||
label = "X (Twitter)" | label = "X (Twitter)" | ||
| Line 60: | Line 60: | ||
{ | { | ||
param = "youtube", | param = "youtube", | ||
icon = "File:SocialYouTubeIcon. | icon = "File:SocialYouTubeIcon.svg", | ||
prefix = "https://www.youtube.com/", | prefix = "https://www.youtube.com/", | ||
label = "YouTube" | label = "YouTube" | ||
Revision as of 13:48, 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 pursposes
-- 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"
},
{
param = "whatsapp",
icon = "File:SocialWhatsAppIcon.svg",
prefix = "https://wa.me/",
label = "WhatsApp"
},
{
-- 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
-- param is a table of aliases
for _, alias in ipairs(param) do
local handle = args[alias]
if handle and handle ~= "" then
return handle
end
end
return nil
end
end
-- Helper function to build a clickable icon link with alt text for accessibility using wikitext syntax: [[File:Something.png|16px|alt=ALT_TEXT|link=URL]]
local function buildIconLink(iconFile, altText, url)
return string.format("[[%s|16px|alt=%s|link=%s]]", iconFile, altText, url)
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
-- Retrieve the handle from any of the defined param aliases
local handle = getUserHandle(args, platform.param)
if handle and handle ~= "" then
local url = platform.prefix .. handle
-- Build an icon link, passing label as alt text
local iconLink = buildIconLink(platform.icon, platform.label, url)
table.insert(icons, iconLink)
end
end
-- If no handles were provided, return nothing (empty string).
if #icons == 0 then
return ""
end
-- Otherwise, return a table row
local iconString = table.concat(icons, " ")
local row = table.concat({
"|-",
"| ", -- Label on the left
"| " .. iconString -- Icons on the right
}, "\n")
return row
end
return sf