Module:SocialMedia: Difference between revisions
No edit summary Tag: Reverted |
No edit summary Tag: Reverted |
||
| Line 47: | Line 47: | ||
} | } | ||
-------------------------------------------------------------------------------- | |||
-- | -- The social platforms table starts here, referencing both the user param name | ||
-- | -- (e.g. "facebook") and a key (e.g. "facebookSVG") that matches our top-of-module | ||
-- | -- svgIcons table. We'll rely on an external CSS for styling each inline <svg>. | ||
-- | -------------------------------------------------------------------------------- | ||
-- | |||
local socialPlatforms = { | local socialPlatforms = { | ||
{ | { | ||
param | param = "facebook", | ||
inlineKey = "facebook", -- corresponds to svgIcons["facebook"] | |||
prefix = "https://www.facebook.com/", | prefix = "https://www.facebook.com/", | ||
label | label = "Facebook" | ||
}, | }, | ||
{ | { | ||
param | param = "instagram", | ||
inlineKey = "instagram", | |||
prefix = "https://www.instagram.com/", | prefix = "https://www.instagram.com/", | ||
label | label = "Instagram" | ||
}, | }, | ||
{ | { | ||
param | param = "linkedin", | ||
inlineKey = "linkedin", | |||
prefix = "https://www.linkedin.com/", | prefix = "https://www.linkedin.com/", | ||
label | label = "LinkedIn" | ||
}, | }, | ||
{ | { | ||
param | param = "telegram", | ||
inlineKey = "telegram", | |||
prefix = "https://t.me/", | prefix = "https://t.me/", | ||
label | label = "Telegram" | ||
}, | }, | ||
{ | { | ||
param | param = "threads", | ||
inlineKey = "threads", | |||
prefix = "https://www.threads.net/", | prefix = "https://www.threads.net/", | ||
label | label = "Threads" | ||
}, | }, | ||
{ | { | ||
param | param = "tiktok", | ||
inlineKey = "tiktok", | |||
prefix = "https://www.tiktok.com/", | prefix = "https://www.tiktok.com/", | ||
label | label = "TikTok" | ||
}, | }, | ||
{ | { | ||
-- | -- Accepts either |x= or |twitter= from the user, but we'll use the same inline SVG | ||
param | param = { "x", "twitter" }, | ||
inlineKey = "twitter", | |||
prefix = "https://x.com/", | prefix = "https://x.com/", | ||
label | label = "X (Twitter)" | ||
}, | }, | ||
{ | { | ||
param | param = "youtube", | ||
inlineKey = "youtube", | |||
prefix = "https://www.youtube.com/", | prefix = "https://www.youtube.com/", | ||
label | label = "YouTube" | ||
}, | }, | ||
} | } | ||
-------------------------------------------------------------------------------- | -------------------------------------------------------------------------------- | ||
-- | -- Read the user's handle from the template args. If 'param' is a table of aliases, | ||
-- | -- we check each one in order (e.g. "x", then "twitter"). | ||
-------------------------------------------------------------------------------- | -------------------------------------------------------------------------------- | ||
local function getUserHandle(args, param) | local function getUserHandle(args, param) | ||
| Line 124: | Line 122: | ||
end | end | ||
-- | -------------------------------------------------------------------------------- | ||
-- [ | -- Fetch the inline SVG content from our top-of-module svgIcons table (not shown here), | ||
local function | -- using 'inlineKey'. If there's no match, we return an empty string. | ||
return string.format(" | -------------------------------------------------------------------------------- | ||
local function getInlineSVG(inlineKey, svgIcons) | |||
return svgIcons[inlineKey] or "" | |||
end | |||
-------------------------------------------------------------------------------- | |||
-- Build an individual <a>…</a> wrapping the inline <svg>. | |||
-- 'platform.label' becomes the alt/title, 'handle' is appended to the prefix, | |||
-- and 'svgIcons' is the table of actual SVG code defined at the top of the module. | |||
-------------------------------------------------------------------------------- | |||
local function buildIcon(platform, handle, svgIcons) | |||
local iconSVG = getInlineSVG(platform.inlineKey, svgIcons) | |||
local fullURL = platform.prefix .. handle | |||
local altText = platform.label | |||
-- We wrap the raw SVG code in an <a> so it links to the social profile: | |||
-- Add classes or other attributes as needed for external CSS styling. | |||
return string.format( | |||
'<a href="%s" class="social-icon" title="%s">%s</a>', | |||
fullURL, altText, iconSVG | |||
) | |||
end | end | ||
-- Build a single table row with | -------------------------------------------------------------------------------- | ||
-- Build a single table row (row + cell) that contains a flex container with the icons. | |||
-- We rely on an external CSS (e.g., "display:flex;" in a .social-icons class). | |||
-------------------------------------------------------------------------------- | |||
local function buildSocialRow(icons) | local function buildSocialRow(icons) | ||
local iconMarkup = table.concat({ | local iconMarkup = table.concat({ | ||
'<div | '<div class="social-icons">', | ||
table.concat(icons, ""), | table.concat(icons, ""), | ||
'</div>' | '</div>' | ||
| Line 144: | Line 165: | ||
end | end | ||
-- Main render function | -------------------------------------------------------------------------------- | ||
function | -- Main render function. We expect the caller to pass 'args' (template arguments) | ||
-- and 'svgIcons' (the table of inline SVG data at the top of the module). | |||
-------------------------------------------------------------------------------- | |||
function m.render(args, svgIcons) | |||
local icons = {} | local icons = {} | ||
| Line 151: | Line 175: | ||
local handle = getUserHandle(args, platform.param) | local handle = getUserHandle(args, platform.param) | ||
if handle and handle ~= "" then | if handle and handle ~= "" then | ||
local iconLink = buildIcon(platform, handle, svgIcons) | |||
local iconLink = | |||
table.insert(icons, iconLink) | table.insert(icons, iconLink) | ||
end | end | ||
end | end | ||
if #icons == 0 then | if #icons == 0 then | ||
return "" | return "" | ||
end | end | ||
-- Return a row | -- Return a table row with a cell that contains our flex container of icons | ||
return buildSocialRow(icons) | return buildSocialRow(icons) | ||
end | end | ||
return | return m | ||